Skip to content

Commit a2f9baa

Browse files
authored
Merge pull request #72 from ruiqi7/feature/matching-websocket
Fix match bug on account change
2 parents 45e38b5 + 40af7b9 commit a2f9baa

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

frontend/src/contexts/MatchContext.tsx

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,7 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
100100
}
101101
const { user } = auth;
102102

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);
113104
const [matchCriteria, setMatchCriteria] = useState<MatchCriteria | null>(
114105
null
115106
);
@@ -118,6 +109,18 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
118109
const [matchPending, setMatchPending] = useState<boolean>(false);
119110
const [loading, setLoading] = useState<boolean>(true);
120111

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+
121124
useEffect(() => {
122125
if (
123126
!matchUser?.id ||
@@ -233,14 +236,7 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
233236

234237
const initMatchRequestListeners = () => {
235238
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);
244240
});
245241

246242
matchSocket.on(MatchEvents.MATCH_REQUEST_EXISTS, () => {
@@ -254,14 +250,7 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
254250

255251
const initMatchingListeners = () => {
256252
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);
265254
});
266255
};
267256

@@ -277,14 +266,7 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
277266
});
278267

279268
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);
288270
});
289271

290272
matchSocket.on(MatchEvents.MATCH_REQUEST_ERROR, () => {
@@ -299,6 +281,21 @@ const MatchProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
299281
});
300282
};
301283

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+
302299
const findMatch = (
303300
complexities: string[],
304301
categories: string[],

0 commit comments

Comments
 (0)