@@ -55,6 +55,7 @@ const usePeriodicCallback = (
55
55
const Matchmaking = ( ) => {
56
56
const router = useRouter ( ) ;
57
57
const [ isMatching , setIsMatching ] = useState < boolean > ( false ) ;
58
+ const [ matchHash , setMatchHash ] = useState < string > ( "" ) ;
58
59
const { difficulties, topicList } = useQuestionFilter ( ) ;
59
60
const [ difficultyFilter , setDifficultyFilter ] = useState < string > (
60
61
Difficulty . Easy
@@ -86,12 +87,16 @@ const Matchmaking = () => {
86
87
return matchRequest ;
87
88
} ;
88
89
90
+ // TODO: Canceling the match should propagate a cancellation signal
91
+ // Currently, we can actually match yourself rn due to this change
92
+ // This indicates to me that 1 users can match on a cancellation
89
93
const handleMatch = async ( ) => {
90
94
if ( ! isMatching ) {
91
95
setIsMatching ( true ) ;
92
96
93
97
// start 30s timeout
94
98
timeout . current = setTimeout ( ( ) => {
99
+ setMatchHash ( "" ) ;
95
100
setIsMatching ( false ) ;
96
101
console . log ( "Match request timed out after 30s" ) ;
97
102
} , TIMEOUT_MILLISECONDS ) ;
@@ -103,22 +108,25 @@ const Matchmaking = () => {
103
108
104
109
// send match request
105
110
const status = await findMatch ( matchRequest ) ;
106
- if ( status . error ) {
111
+ if ( isError ( status ) ) {
107
112
stopTimer ( ) ;
108
113
console . log ( "Failed to find match. Cancel matching." ) ;
114
+ setMatchHash ( "" ) ;
109
115
setIsMatching ( false ) ;
110
116
return ;
111
117
}
118
+ setMatchHash ( status . match_code ) ;
112
119
console . log ( `Started finding match.` ) ;
113
120
} else {
121
+ setMatchHash ( "" ) ;
114
122
stopTimer ( ) ;
115
123
setIsMatching ( false ) ;
116
124
console . log ( "User stopped matching" ) ;
117
125
}
118
126
} ;
119
127
120
128
const queryResource = async ( ) => {
121
- const res = await checkMatchStatus ( userid ) ;
129
+ const res = await checkMatchStatus ( matchHash ) ;
122
130
if ( isError ( res ) ) {
123
131
// for now 404 means no match found so dont stop matching on error, let request timeout
124
132
return ;
@@ -128,17 +136,8 @@ const Matchmaking = () => {
128
136
// TODO: iron out what is in a match response and sync up with collab service rooms
129
137
const matchRes : MatchResponse = res as MatchResponse ;
130
138
console . log ( "Match found!" ) ;
131
- // display in a popup for now
132
- // const message = `Room ID: ${matchRes.data.roomId}
133
- // User1: ${matchRes.data.user1}
134
- // User2: ${matchRes.data.user2}
135
- // Question: ${matchRes.data.questionId}
136
- // `;
137
- const message = "Match found! Redirecting to collab page..." ;
138
- window . alert ( message ) ;
139
- // redirect to question page
140
139
router . push (
141
- `/questions/${ matchRes . data . questionId } /${ matchRes . data . roomId } `
140
+ `/questions/${ matchRes . data . questionId } /${ matchRes . data . roomId } ?match= ${ matchHash } `
142
141
) ;
143
142
} ;
144
143
@@ -151,8 +150,8 @@ const Matchmaking = () => {
151
150
Add Question
152
151
</ PeerprepButton >
153
152
< div className = "flex flex-row items-center space-x-4" >
154
- < PeerprepButton onClick = { handleMatch } >
155
- { isMatching ? "Cancel Match" : " Find Match"}
153
+ < PeerprepButton onClick = { handleMatch } disabled = { ! isMatching && matchHash !== "" } >
154
+ { isMatching ? "Cancel Match" : matchHash === "" ? " Find Match" : "Redirecting... "}
156
155
</ PeerprepButton >
157
156
{ ! isMatching && (
158
157
< PeerprepDropdown
0 commit comments