@@ -7,6 +7,10 @@ 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
9
import socket from './socket' ;
10
+ import { getAllQuestions } from '../../api/questions/data' ;
11
+ import { useAuth } from '../../auth/auth.context'
12
+ import { sha256 } from "js-sha256" ;
13
+
10
14
11
15
const style = {
12
16
position : 'absolute' as 'absolute' ,
@@ -26,21 +30,27 @@ const style = {
26
30
} ;
27
31
28
32
const MatchingForm = React . forwardRef ( function MatchingForm ( ) {
29
- const [ difficulty , setDifficulty ] = React . useState ( '' ) ;
30
- const [ category , setCategory ] = React . useState ( '' ) ;
33
+ const [ difficulty , setDifficulty ] = React . useState ( 'Easy ' ) ;
34
+ const [ category , setCategory ] = React . useState ( 'Strings ' ) ;
31
35
const [ isMatching , setIsMatching ] = React . useState ( false ) ; // Track matching state
32
36
const navigate = useNavigate ( ) ; // Get navigate for redirection
33
-
37
+ const { user } = useAuth ( ) ;
38
+ const userEmail = user ?. email
34
39
const handleDiffChange = ( event : SelectChangeEvent ) => {
40
+
35
41
setDifficulty ( event . target . value ) ;
42
+
36
43
} ;
37
44
38
45
const handleCatChange = ( event : SelectChangeEvent ) => {
46
+
39
47
setCategory ( event . target . value ) ;
48
+
40
49
} ;
41
50
42
51
const handleConnect = ( ) => {
43
52
const preferences = {
53
+ userEmail,
44
54
difficulty,
45
55
category,
46
56
} ;
@@ -52,21 +62,54 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
52
62
setIsMatching ( true ) ;
53
63
} ;
54
64
65
+
66
+ 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
+ return seed % arrayLength ;
69
+ }
55
70
// Handle the "matchFound" event from the server
71
+ const getQuestions = async ( seed : any ) => {
72
+ const questions = await getAllQuestions ( ) ;
73
+ console . log ( difficulty )
74
+ console . log ( category )
75
+ 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
+
82
+ const randomIndex = generateConsistentRandomIndex ( seed , filteredQuestions . length )
83
+ console . log ( randomIndex )
84
+ const selectedQuestion = filteredQuestions [ randomIndex ] ;
85
+ console . log ( selectedQuestion )
86
+ if ( ! selectedQuestion ) {
87
+ return 1
88
+ }
89
+ const selectedId = selectedQuestion . id ;
90
+ return selectedId
91
+ }
56
92
React . useEffect ( ( ) => {
57
- socket . on ( 'matchFound' , ( matchedUserPreferences ) => {
93
+ socket . on ( 'matchFound' , async ( matchedUserPreferences ) => {
58
94
// Handle the matched user's preferences here
59
95
console . log ( 'Match Found:' , matchedUserPreferences ) ;
96
+ const seed = matchedUserPreferences . seed ;
97
+ const matchedUser = matchedUserPreferences . matchedUserPreferences ;
98
+ console . log ( seed )
60
99
setIsMatching ( false ) ; // Set matching state to false
61
100
// Redirect to the question page with the code editor
62
- navigate ( '/question' ) ; // Update the route as needed
101
+ const qId = await getQuestions ( seed ) ;
102
+ const hashedEmailOne = sha256 ( userEmail || "" ) ;
103
+ const hashedEmailTwo = sha256 ( matchedUser . userEmail )
104
+ navigate ( `/collab/question/${ qId } /${ hashedEmailOne } /${ hashedEmailTwo } ` ) ; // Update the route as needed
105
+
63
106
} ) ;
64
107
65
108
// Clean up the event listener when the component unmounts
66
109
return ( ) => {
67
110
socket . off ( 'matchFound' ) ;
68
111
} ;
69
- } , [ navigate ] ) ;
112
+ } , [ ] ) ;
70
113
71
114
return (
72
115
< Box sx = { style } >
@@ -79,6 +122,7 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
79
122
id = "demo-simple-select-helper"
80
123
value = { difficulty }
81
124
label = "Difficulty"
125
+
82
126
onChange = { handleDiffChange }
83
127
>
84
128
< MenuItem value = "" >
@@ -101,8 +145,8 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
101
145
< MenuItem value = "" >
102
146
< em > None</ em >
103
147
</ MenuItem >
104
- < MenuItem value = { 'Algo ' } > Algo </ MenuItem >
105
- < MenuItem value = { 'ML ' } > ML </ MenuItem >
148
+ < MenuItem value = { 'Strings ' } > Strings </ MenuItem >
149
+ < MenuItem value = { 'Algorithms ' } > Algorithms </ MenuItem >
106
150
</ Select >
107
151
</ FormControl >
108
152
{ isMatching ? (
0 commit comments