1
1
import * as React from 'react' ;
2
2
import { Box } from "@mui/material" ;
3
3
import Button from '@mui/material/Button' ;
4
- import InputLabel from '@mui/material/InputLabel' ;
5
- import MenuItem from '@mui/material/MenuItem' ;
6
- import FormControl from '@mui/material/FormControl' ;
7
- import Select , { SelectChangeEvent } from '@mui/material/Select' ;
8
- import { useNavigate } from 'react-router-dom' ; // Import useNavigate for redirection
4
+ import { useNavigate } from 'react-router-dom' ;
9
5
import socket from './socket' ;
10
6
import { getAllQuestions } from '../../api/questions/data' ;
11
- import { useAuth } from '../../auth/auth.context'
7
+ import { useAuth } from '../../auth/auth.context' ;
12
8
import { sha256 } from "js-sha256" ;
13
-
9
+ import Question from '../Questions/Question' ;
14
10
15
11
const style = {
16
12
position : 'absolute' as 'absolute' ,
@@ -32,125 +28,94 @@ const style = {
32
28
const MatchingForm = React . forwardRef ( function MatchingForm ( ) {
33
29
const [ difficulty , setDifficulty ] = React . useState ( 'Easy' ) ;
34
30
const [ category , setCategory ] = React . useState ( 'Strings' ) ;
35
- const [ isMatching , setIsMatching ] = React . useState ( false ) ; // Track matching state
36
- const navigate = useNavigate ( ) ; // Get navigate for redirection
31
+ const [ categoryList , setCategoryList ] = React . useState < string [ ] > ( [ ] ) ;
32
+ const [ isMatching , setIsMatching ] = React . useState ( false ) ;
33
+ const navigate = useNavigate ( ) ;
37
34
const { user } = useAuth ( ) ;
38
- const userEmail = user ?. email
39
- const handleDiffChange = ( event : SelectChangeEvent ) => {
40
-
41
- setDifficulty ( event . target . value ) ;
42
-
43
- } ;
44
-
45
- const handleCatChange = ( event : SelectChangeEvent ) => {
46
-
47
- setCategory ( event . target . value ) ;
48
-
49
- } ;
35
+ const userEmail = user ?. email ;
50
36
51
37
const handleConnect = ( ) => {
52
38
const preferences = {
53
39
userEmail,
54
40
difficulty,
55
41
category,
56
42
} ;
57
-
58
- // Emit the startMatching event to the server with user preferences
59
43
socket . emit ( 'startMatching' , preferences ) ;
60
-
61
- // Set matching state to true when attempting to match
62
44
setIsMatching ( true ) ;
63
45
} ;
64
46
65
-
66
47
const generateConsistentRandomIndex = ( seed : any , arrayLength : number ) => {
67
- // This is a simple example. In real-world scenarios, consider a more complex and predictable pseudo-random function
68
48
return seed % arrayLength ;
69
49
}
70
- // Handle the "matchFound" event from the server
50
+
71
51
const getQuestions = async ( seed : any ) => {
72
52
const questions = await getAllQuestions ( ) ;
73
- console . log ( difficulty )
74
- console . log ( category )
75
53
const filteredQuestions = questions . data . filter ( ( q : any ) => {
76
- // console.log(difficulty);
77
- // console.log(q.difficulty);
78
- return q . categories . includes ( category ) && q . difficulty === difficulty
79
-
80
- } ) ;
81
-
54
+ return q . categories . includes ( category ) && q . difficulty === difficulty
55
+ } ) ;
56
+
82
57
const randomIndex = generateConsistentRandomIndex ( seed , filteredQuestions . length )
83
- console . log ( randomIndex )
84
58
const selectedQuestion = filteredQuestions [ randomIndex ] ;
85
- console . log ( selectedQuestion )
86
59
if ( ! selectedQuestion ) {
87
60
return 1
88
61
}
89
62
const selectedId = selectedQuestion . id ;
90
63
return selectedId
91
64
}
65
+
66
+ React . useEffect ( ( ) => {
67
+ async function getCategories ( ) {
68
+ const questionsData = await getAllQuestions ( ) as { data : Question [ ] } ;
69
+ const allCategories = questionsData . data . reduce ( ( acc : string [ ] , question : Question ) => {
70
+ return [ ...acc , ...question . categories ] ;
71
+ } , [ ] ) ;
72
+ const uniqueCategories = Array . from ( new Set ( allCategories ) ) ;
73
+ setCategoryList ( uniqueCategories ) ;
74
+ }
75
+ getCategories ( ) ;
76
+ } , [ ] ) ;
77
+
92
78
React . useEffect ( ( ) => {
93
79
socket . on ( 'matchFound' , async ( matchedUserPreferences ) => {
94
- // Handle the matched user's preferences here
95
80
console . log ( 'Match Found:' , matchedUserPreferences ) ;
96
81
const seed = matchedUserPreferences . seed ;
97
82
const matchedUser = matchedUserPreferences . matchedUserPreferences ;
98
- console . log ( seed )
99
- setIsMatching ( false ) ; // Set matching state to false
100
- // Redirect to the question page with the code editor
83
+ setIsMatching ( false ) ;
101
84
const qId = await getQuestions ( seed ) ;
102
85
const hashedEmailOne = sha256 ( userEmail || "" ) ;
103
86
const hashedEmailTwo = sha256 ( matchedUser . userEmail )
104
- navigate ( `/collab/question/${ qId } /${ hashedEmailOne } /${ hashedEmailTwo } ` ) ; // Update the route as needed
105
-
87
+ navigate ( `/collab/question/${ qId } /${ hashedEmailOne } /${ hashedEmailTwo } ` ) ;
106
88
} ) ;
107
89
108
- // Clean up the event listener when the component unmounts
109
90
return ( ) => {
110
91
socket . off ( 'matchFound' ) ;
111
92
} ;
112
- } , [ ] ) ;
93
+ } , [ difficulty , category , userEmail , navigate ] ) ;
113
94
114
95
return (
115
96
< Box sx = { style } >
116
97
< h2 > < center > Please select a difficulty and question category.</ center > </ h2 >
117
98
< h4 > < center > We will attempt to connect you with a user who chose the same options as you within 30 seconds.</ center > </ h4 >
118
- < FormControl sx = { { mt : 1 , width : '100%' } } >
119
- < InputLabel id = "demo-simple-select-helper-label" > Difficulty</ InputLabel >
120
- < Select
121
- labelId = "demo-simple-select-helper-label"
122
- id = "demo-simple-select-helper"
123
- value = { difficulty }
124
- label = "Difficulty"
125
-
126
- onChange = { handleDiffChange }
127
- >
128
- < MenuItem value = "" >
129
- < em > None</ em >
130
- </ MenuItem >
131
- < MenuItem value = { 'Easy' } > Easy</ MenuItem >
132
- < MenuItem value = { 'Medium' } > Medium</ MenuItem >
133
- < MenuItem value = { 'Hard' } > Hard</ MenuItem >
134
- </ Select >
135
- </ FormControl >
136
- < FormControl sx = { { mt : 1 , mb : 1 , width : '100%' } } >
137
- < InputLabel id = "demo-simple-select-helper-label" > Category</ InputLabel >
138
- < Select
139
- labelId = "demo-simple-select-helper-label"
140
- id = "demo-simple-select-helper"
141
- value = { category }
142
- label = "Category"
143
- onChange = { handleCatChange }
144
- >
145
- < MenuItem value = "" >
146
- < em > None</ em >
147
- </ MenuItem >
148
- < MenuItem value = { 'Strings' } > Strings</ MenuItem >
149
- < MenuItem value = { 'Algorithms' } > Algorithms</ MenuItem >
150
- </ Select >
151
- </ FormControl >
99
+ < div >
100
+ < label htmlFor = "difficulty" > Difficulty:</ label >
101
+ < select id = "difficulty" value = { difficulty } onChange = { e => setDifficulty ( e . target . value ) } >
102
+ < option value = "" > None</ option >
103
+ < option value = "Easy" > Easy</ option >
104
+ < option value = "Medium" > Medium</ option >
105
+ < option value = "Hard" > Hard</ option >
106
+ </ select >
107
+ </ div >
108
+ < div >
109
+ < label htmlFor = "category" > Category:</ label >
110
+ < select id = "category" value = { category } onChange = { e => setCategory ( e . target . value ) } >
111
+ < option value = "" > None</ option >
112
+ { categoryList . map ( cat => (
113
+ < option key = { cat } value = { cat } > { cat } </ option >
114
+ ) ) }
115
+ </ select >
116
+ </ div >
152
117
{ isMatching ? (
153
- < div > Loading...</ div > // Show loading spinner
118
+ < div > Loading...</ div >
154
119
) : (
155
120
< Button sx = { { mt : '5%' } } variant = "contained" onClick = { handleConnect } >
156
121
Connect
0 commit comments