@@ -5,7 +5,8 @@ import InputLabel from '@mui/material/InputLabel';
5
5
import MenuItem from '@mui/material/MenuItem' ;
6
6
import FormControl from '@mui/material/FormControl' ;
7
7
import Select , { SelectChangeEvent } from '@mui/material/Select' ;
8
-
8
+ import { useNavigate } from 'react-router-dom' ; // Import useNavigate for redirection
9
+ import socket from './socket' ;
9
10
10
11
const style = {
11
12
position : 'absolute' as 'absolute' ,
@@ -27,15 +28,46 @@ const style = {
27
28
const MatchingForm = React . forwardRef ( function MatchingForm ( ) {
28
29
const [ difficulty , setDifficulty ] = React . useState ( '' ) ;
29
30
const [ category , setCategory ] = React . useState ( '' ) ;
31
+ const [ isMatching , setIsMatching ] = React . useState ( false ) ; // Track matching state
32
+ const navigate = useNavigate ( ) ; // Get navigate for redirection
33
+
30
34
const handleDiffChange = ( event : SelectChangeEvent ) => {
31
35
setDifficulty ( event . target . value ) ;
32
36
} ;
37
+
33
38
const handleCatChange = ( event : SelectChangeEvent ) => {
34
39
setCategory ( event . target . value ) ;
35
40
} ;
41
+
36
42
const handleConnect = ( ) => {
37
- console . log ( 'connecting...' )
38
- }
43
+ const preferences = {
44
+ difficulty,
45
+ category,
46
+ } ;
47
+
48
+ // Emit the startMatching event to the server with user preferences
49
+ socket . emit ( 'startMatching' , preferences ) ;
50
+
51
+ // Set matching state to true when attempting to match
52
+ setIsMatching ( true ) ;
53
+ } ;
54
+
55
+ // Handle the "matchFound" event from the server
56
+ React . useEffect ( ( ) => {
57
+ socket . on ( 'matchFound' , ( matchedUserPreferences ) => {
58
+ // Handle the matched user's preferences here
59
+ console . log ( 'Match Found:' , matchedUserPreferences ) ;
60
+ setIsMatching ( false ) ; // Set matching state to false
61
+ // Redirect to the question page with the code editor
62
+ navigate ( '/question' ) ; // Update the route as needed
63
+ } ) ;
64
+
65
+ // Clean up the event listener when the component unmounts
66
+ return ( ) => {
67
+ socket . off ( 'matchFound' ) ;
68
+ } ;
69
+ } , [ navigate ] ) ;
70
+
39
71
return (
40
72
< Box sx = { style } >
41
73
< h2 > < center > Please select a difficulty and question category.</ center > </ h2 >
@@ -56,7 +88,6 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
56
88
< MenuItem value = { 'Medium' } > Medium</ MenuItem >
57
89
< MenuItem value = { 'Hard' } > Hard</ MenuItem >
58
90
</ Select >
59
- { /* <FormHelperText>Difficulty of Question</FormHelperText> */ }
60
91
</ FormControl >
61
92
< FormControl sx = { { mt : 1 , mb : 1 , width : '100%' } } >
62
93
< InputLabel id = "demo-simple-select-helper-label" > Category</ InputLabel >
@@ -73,13 +104,16 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
73
104
< MenuItem value = { 'Algo' } > Algo</ MenuItem >
74
105
< MenuItem value = { 'ML' } > ML</ MenuItem >
75
106
</ Select >
76
- { /* <FormHelperText>Difficulty of Question</FormHelperText> */ }
77
107
</ FormControl >
78
- < Button sx = { { mt : '5%' } } variant = "contained" onClick = { handleConnect } >
79
- Connect
80
- </ Button >
108
+ { isMatching ? (
109
+ < div > Loading...</ div > // Show loading spinner
110
+ ) : (
111
+ < Button sx = { { mt : '5%' } } variant = "contained" onClick = { handleConnect } >
112
+ Connect
113
+ </ Button >
114
+ ) }
81
115
</ Box >
82
116
) ;
83
117
} ) ;
84
118
85
- export default MatchingForm
119
+ export default MatchingForm ;
0 commit comments