Skip to content

Commit 7ce5f73

Browse files
committed
Fix matching form
1 parent e4a6b1a commit 7ce5f73

File tree

1 file changed

+46
-81
lines changed

1 file changed

+46
-81
lines changed

frontend/src/components/MatchingService/MatchingForm.tsx

Lines changed: 46 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import * as React from 'react';
22
import { Box } from "@mui/material";
33
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';
95
import socket from './socket';
106
import { getAllQuestions } from '../../api/questions/data';
11-
import { useAuth } from '../../auth/auth.context'
7+
import { useAuth } from '../../auth/auth.context';
128
import { sha256 } from "js-sha256";
13-
9+
import Question from '../Questions/Question';
1410

1511
const style = {
1612
position: 'absolute' as 'absolute',
@@ -32,125 +28,94 @@ const style = {
3228
const MatchingForm = React.forwardRef(function MatchingForm() {
3329
const [difficulty, setDifficulty] = React.useState('Easy');
3430
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();
3734
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;
5036

5137
const handleConnect = () => {
5238
const preferences = {
5339
userEmail,
5440
difficulty,
5541
category,
5642
};
57-
58-
// Emit the startMatching event to the server with user preferences
5943
socket.emit('startMatching', preferences);
60-
61-
// Set matching state to true when attempting to match
6244
setIsMatching(true);
6345
};
6446

65-
6647
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
6848
return seed % arrayLength;
6949
}
70-
// Handle the "matchFound" event from the server
50+
7151
const getQuestions = async (seed: any) => {
7252
const questions = await getAllQuestions();
73-
console.log(difficulty)
74-
console.log(category)
7553
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+
8257
const randomIndex = generateConsistentRandomIndex(seed, filteredQuestions.length)
83-
console.log(randomIndex)
8458
const selectedQuestion = filteredQuestions[randomIndex];
85-
console.log(selectedQuestion)
8659
if (!selectedQuestion) {
8760
return 1
8861
}
8962
const selectedId = selectedQuestion.id;
9063
return selectedId
9164
}
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+
9278
React.useEffect(() => {
9379
socket.on('matchFound', async (matchedUserPreferences) => {
94-
// Handle the matched user's preferences here
9580
console.log('Match Found:', matchedUserPreferences);
9681
const seed = matchedUserPreferences.seed;
9782
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);
10184
const qId = await getQuestions(seed);
10285
const hashedEmailOne = sha256(userEmail || "");
10386
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}`);
10688
});
10789

108-
// Clean up the event listener when the component unmounts
10990
return () => {
11091
socket.off('matchFound');
11192
};
112-
}, []);
93+
}, [difficulty, category, userEmail, navigate]);
11394

11495
return (
11596
<Box sx={style}>
11697
<h2><center>Please select a difficulty and question category.</center></h2>
11798
<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>
152117
{isMatching ? (
153-
<div>Loading...</div> // Show loading spinner
118+
<div>Loading...</div>
154119
) : (
155120
<Button sx={{ mt: '5%' }} variant="contained" onClick={handleConnect}>
156121
Connect

0 commit comments

Comments
 (0)