@@ -100,16 +100,7 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
100
100
}
101
101
const { user } = auth ;
102
102
103
- const [ matchUser ] = useState < MatchUser | null > (
104
- user
105
- ? {
106
- id : user . id ,
107
- username : user . username ,
108
- profile : user . profilePictureUrl ,
109
- }
110
- : null
111
- ) ;
112
-
103
+ const [ matchUser , setMatchUser ] = useState < MatchUser | null > ( null ) ;
113
104
const [ matchCriteria , setMatchCriteria ] = useState < MatchCriteria | null > (
114
105
null
115
106
) ;
@@ -118,6 +109,18 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
118
109
const [ matchPending , setMatchPending ] = useState < boolean > ( false ) ;
119
110
const [ loading , setLoading ] = useState < boolean > ( true ) ;
120
111
112
+ useEffect ( ( ) => {
113
+ if ( user ) {
114
+ setMatchUser ( {
115
+ id : user . id ,
116
+ username : user . username ,
117
+ profile : user . profilePictureUrl ,
118
+ } ) ;
119
+ } else {
120
+ setMatchUser ( null ) ;
121
+ }
122
+ } , [ user ] ) ;
123
+
121
124
useEffect ( ( ) => {
122
125
if (
123
126
! matchUser ?. id ||
@@ -233,14 +236,7 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
233
236
234
237
const initMatchRequestListeners = ( ) => {
235
238
matchSocket . on ( MatchEvents . MATCH_FOUND , ( { matchId, user1, user2 } ) => {
236
- setMatchId ( matchId ) ;
237
- if ( matchUser ?. id === user1 . id ) {
238
- setPartner ( user2 ) ;
239
- } else {
240
- setPartner ( user1 ) ;
241
- }
242
- setMatchPending ( true ) ;
243
- appNavigate ( MatchPaths . MATCHED ) ;
239
+ handleMatchFound ( matchId , user1 , user2 ) ;
244
240
} ) ;
245
241
246
242
matchSocket . on ( MatchEvents . MATCH_REQUEST_EXISTS , ( ) => {
@@ -254,14 +250,7 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
254
250
255
251
const initMatchingListeners = ( ) => {
256
252
matchSocket . on ( MatchEvents . MATCH_FOUND , ( { matchId, user1, user2 } ) => {
257
- setMatchId ( matchId ) ;
258
- if ( matchUser ?. id === user1 . id ) {
259
- setPartner ( user2 ) ;
260
- } else {
261
- setPartner ( user1 ) ;
262
- }
263
- setMatchPending ( true ) ;
264
- appNavigate ( MatchPaths . MATCHED ) ;
253
+ handleMatchFound ( matchId , user1 , user2 ) ;
265
254
} ) ;
266
255
} ;
267
256
@@ -277,14 +266,7 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
277
266
} ) ;
278
267
279
268
matchSocket . on ( MatchEvents . MATCH_FOUND , ( { matchId, user1, user2 } ) => {
280
- setMatchId ( matchId ) ;
281
- if ( matchUser ?. id === user1 . id ) {
282
- setPartner ( user2 ) ;
283
- } else {
284
- setPartner ( user1 ) ;
285
- }
286
- setMatchPending ( true ) ;
287
- appNavigate ( MatchPaths . MATCHED ) ;
269
+ handleMatchFound ( matchId , user1 , user2 ) ;
288
270
} ) ;
289
271
290
272
matchSocket . on ( MatchEvents . MATCH_REQUEST_ERROR , ( ) => {
@@ -299,6 +281,21 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
299
281
} ) ;
300
282
} ;
301
283
284
+ const handleMatchFound = (
285
+ matchId : string ,
286
+ user1 : MatchUser ,
287
+ user2 : MatchUser
288
+ ) => {
289
+ setMatchId ( matchId ) ;
290
+ if ( matchUser ?. id === user1 . id ) {
291
+ setPartner ( user2 ) ;
292
+ } else {
293
+ setPartner ( user1 ) ;
294
+ }
295
+ setMatchPending ( true ) ;
296
+ appNavigate ( MatchPaths . MATCHED ) ;
297
+ } ;
298
+
302
299
const findMatch = (
303
300
complexities : string [ ] ,
304
301
categories : string [ ] ,
0 commit comments