@@ -8,8 +8,10 @@ import { useAuth } from "@/app/auth/auth-context";
8
8
import { joinMatchQueue } from "@/lib/join-match-queue" ;
9
9
import { leaveMatchQueue } from "@/lib/leave-match-queue" ;
10
10
import { subscribeMatch } from "@/lib/subscribe-match" ;
11
+ import { useRouter } from "next/navigation" ;
11
12
12
13
export default function FindMatch ( ) {
14
+ const router = useRouter ( ) ;
13
15
const [ selectedDifficulty , setSelectedDifficulty ] = useState < string > ( "" ) ;
14
16
const [ selectedTopic , setSelectedTopic ] = useState < string > ( "" ) ;
15
17
const [ isSearching , setIsSearching ] = useState < boolean > ( false ) ;
@@ -62,6 +64,8 @@ export default function FindMatch() {
62
64
return ;
63
65
}
64
66
67
+ let isMatched = false ;
68
+
65
69
const response = await joinMatchQueue (
66
70
auth . token ,
67
71
auth ?. user ?. id ,
@@ -70,12 +74,41 @@ export default function FindMatch() {
70
74
) ;
71
75
switch ( response . status ) {
72
76
case 201 :
73
- toast ( {
74
- title : "Matched" ,
75
- description : "Successfully matched" ,
76
- variant : "success" ,
77
- } ) ;
78
- return ;
77
+ const initialResponseData = await response . json ( ) ;
78
+ let responseData ;
79
+ if ( typeof initialResponseData === "string" ) {
80
+ try {
81
+ responseData = JSON . parse ( initialResponseData ) ;
82
+ } catch ( error ) {
83
+ toast ( {
84
+ title : "Error" ,
85
+ description : "Unexpected error occured, please try again" ,
86
+ variant : "destructive" ,
87
+ } ) ;
88
+ return ;
89
+ }
90
+ } else {
91
+ responseData = initialResponseData ;
92
+ }
93
+
94
+ if ( responseData . room_id ) {
95
+ isMatched = true ;
96
+ const roomId = responseData . room_id ;
97
+ toast ( {
98
+ title : "Matched" ,
99
+ description : "Successfully matched" ,
100
+ variant : "success" ,
101
+ } ) ;
102
+ router . push ( `/collaboration/${ roomId } ` ) ;
103
+ } else {
104
+ toast ( {
105
+ title : "Error" ,
106
+ description : "Room ID not found" ,
107
+ variant : "destructive" ,
108
+ } ) ;
109
+ }
110
+ break ;
111
+
79
112
case 202 :
80
113
case 304 :
81
114
setIsSearching ( true ) ;
@@ -87,24 +120,55 @@ export default function FindMatch() {
87
120
const queueTimeout = setTimeout ( ( ) => {
88
121
handleCancel ( true ) ;
89
122
} , waitTimeout ) ;
90
- ws . onmessage = ( ) => {
91
- setIsSearching ( false ) ;
92
- clearTimeout ( queueTimeout ) ;
93
- toast ( {
94
- title : "Matched" ,
95
- description : "Successfully matched" ,
96
- variant : "success" ,
97
- } ) ;
98
- ws . onclose = ( ) => null ;
123
+
124
+ ws . onmessage = ( event ) => {
125
+ let responseData ;
126
+
127
+ try {
128
+ responseData = JSON . parse ( event . data ) ;
129
+ if ( typeof responseData === "string" ) {
130
+ responseData = JSON . parse ( responseData ) ;
131
+ }
132
+ } catch ( error ) {
133
+ toast ( {
134
+ title : "Error" ,
135
+ description : "Unexpected error occured, please try again" ,
136
+ variant : "destructive" ,
137
+ } ) ;
138
+ return ;
139
+ }
140
+
141
+ const roomId = responseData . room_id ;
142
+ if ( roomId ) {
143
+ isMatched = true ;
144
+ setIsSearching ( false ) ;
145
+ clearTimeout ( queueTimeout ) ;
146
+ toast ( {
147
+ title : "Matched" ,
148
+ description : "Successfully matched" ,
149
+ variant : "success" ,
150
+ } ) ;
151
+ router . push ( `/collaboration/${ roomId } ` ) ;
152
+ } else {
153
+ toast ( {
154
+ title : "Error" ,
155
+ description : "Room ID not found" ,
156
+ variant : "destructive" ,
157
+ } ) ;
158
+ }
99
159
} ;
160
+
100
161
ws . onclose = ( ) => {
101
162
setIsSearching ( false ) ;
102
163
clearTimeout ( queueTimeout ) ;
103
- toast ( {
104
- title : "Matching Stopped" ,
105
- description : "Matching has been stopped" ,
106
- variant : "destructive" ,
107
- } ) ;
164
+ if ( ! isMatched ) {
165
+ // Only show this toast if no match was made
166
+ toast ( {
167
+ title : "Matching Stopped" ,
168
+ description : "Matching has been stopped" ,
169
+ variant : "destructive" ,
170
+ } ) ;
171
+ }
108
172
} ;
109
173
return ;
110
174
default :
0 commit comments