@@ -4,13 +4,17 @@ import { MatchForm } from "@/components/matching/matching-form";
4
4
import { SearchProgress } from "@/components/matching/search-progress" ;
5
5
import { SelectionSummary } from "@/components/matching/selection-summary" ;
6
6
import { useToast } from "@/components/hooks/use-toast" ;
7
+ import { useAuth } from "@/app/auth/auth-context" ;
8
+ import { joinMatchQueue } from "@/lib/join-match-queue" ;
7
9
8
10
export default function FindMatch ( ) {
9
11
const [ selectedDifficulty , setSelectedDifficulty ] = useState < string > ( "" ) ;
10
12
const [ selectedTopic , setSelectedTopic ] = useState < string > ( "" ) ;
11
13
const [ isSearching , setIsSearching ] = useState < boolean > ( false ) ;
12
14
const [ waitTime , setWaitTime ] = useState < number > ( 0 ) ;
15
+ const [ websocket , setWebsocket ] = useState < WebSocket > ( ) ;
13
16
const { toast } = useToast ( ) ;
17
+ const auth = useAuth ( ) ;
14
18
15
19
useEffect ( ( ) => {
16
20
let interval : NodeJS . Timeout | undefined ;
@@ -24,15 +28,78 @@ export default function FindMatch() {
24
28
return ( ) => clearInterval ( interval ) ;
25
29
} , [ isSearching ] ) ;
26
30
27
- const handleSearch = ( ) => {
28
- if ( selectedDifficulty && selectedTopic ) {
29
- setIsSearching ( true ) ;
30
- } else {
31
+ useEffect ( ( ) => {
32
+ return ( ) => {
33
+ if ( websocket ) {
34
+ websocket . close ( ) ;
35
+ }
36
+ } ;
37
+ } , [ websocket ] ) ;
38
+
39
+ const handleSearch = async ( ) => {
40
+ if ( ! selectedDifficulty || ! selectedTopic ) {
31
41
toast ( {
32
42
title : "Invalid Selection" ,
33
43
description : "Please select both a difficulty level and a topic" ,
34
44
variant : "destructive" ,
35
45
} ) ;
46
+ return ;
47
+ }
48
+
49
+ if ( ! auth || ! auth . token ) {
50
+ toast ( {
51
+ title : "Access denied" ,
52
+ description : "No authentication token found" ,
53
+ variant : "destructive" ,
54
+ } ) ;
55
+ return ;
56
+ }
57
+
58
+ if ( ! auth . user ) {
59
+ toast ( {
60
+ title : "Access denied" ,
61
+ description : "Not logged in" ,
62
+ variant : "destructive" ,
63
+ } ) ;
64
+ return ;
65
+ }
66
+
67
+ const response = await joinMatchQueue (
68
+ auth . token ,
69
+ auth ?. user ?. id ,
70
+ selectedTopic ,
71
+ selectedDifficulty
72
+ ) ;
73
+ switch ( response . status ) {
74
+ case 200 :
75
+ toast ( {
76
+ title : "Matched" ,
77
+ description : "Successfully matched" ,
78
+ variant : "success" ,
79
+ } ) ;
80
+ return ;
81
+ case 202 :
82
+ setIsSearching ( true ) ;
83
+ const ws = new WebSocket (
84
+ `ws://localhost:6969/match/subscribe/${ auth ?. user ?. id } /${ selectedTopic } /${ selectedDifficulty } `
85
+ ) ;
86
+ ws . onmessage = ( ) => {
87
+ setIsSearching ( false ) ;
88
+ toast ( {
89
+ title : "Matched" ,
90
+ description : "Successfully matched" ,
91
+ variant : "success" ,
92
+ } ) ;
93
+ } ;
94
+ setWebsocket ( ws ) ;
95
+ return ;
96
+ default :
97
+ toast ( {
98
+ title : "Unknown Error" ,
99
+ description : "An unexpected error has occured" ,
100
+ variant : "destructive" ,
101
+ } ) ;
102
+ return ;
36
103
}
37
104
} ;
38
105
0 commit comments