-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,466 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
frontend-service/components/hooks/useQuestionDifficulties.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
const useQuestionDifficulties = () => { | ||
const [difficulties, setDifficulties] = useState<string[]>([]); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchDifficulties = async () => { | ||
try { | ||
const response = await fetch("http://localhost:8080/api/questions/difficulties"); | ||
if (!response.ok) { | ||
throw new Error("Failed to fetch difficulties"); | ||
} | ||
const data = await response.json(); | ||
if (!Array.isArray(data)) { | ||
throw new Error("Expected an array of difficulties"); | ||
} | ||
setDifficulties(data); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
setError(error.message); | ||
} else { | ||
setError("An unknown error occurred"); | ||
} | ||
} | ||
}; | ||
fetchDifficulties(); | ||
}, []); | ||
|
||
return { difficulties, error }; | ||
}; | ||
|
||
export default useQuestionDifficulties; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
const useQuestionTopics = () => { | ||
const [topics, setTopics] = useState<string[]>([]); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
const fetchTopics = async () => { | ||
try { | ||
const response = await fetch("http://localhost:8080/api/questions/topics"); | ||
if (!response.ok) { | ||
throw new Error("Failed to fetch topics"); | ||
} | ||
const data = await response.json(); | ||
if (!Array.isArray(data)) { | ||
throw new Error("Expected an array of topics"); | ||
} | ||
setTopics(data); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
setError(error.message); | ||
} else { | ||
setError("An unknown error occurred"); | ||
} | ||
} | ||
}; | ||
fetchTopics(); | ||
}, []); | ||
|
||
return { topics, error }; | ||
}; | ||
|
||
export default useQuestionTopics; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React, { useState } from "react"; | ||
import useQuestionDifficulties from "../../../components/hooks/useQuestionDifficulties"; | ||
import useQuestionTopics from "../../../components/hooks/useQuestionTopics"; | ||
|
||
const MockMatch: React.FC = () => { | ||
const { difficulties } = useQuestionDifficulties(); | ||
const { topics } = useQuestionTopics(); | ||
|
||
const [selectedDifficulty, setSelectedDifficulty] = useState(""); | ||
const [selectedTopic, setSelectedTopic] = useState(""); | ||
|
||
const handleDifficultyChange = (event: React.ChangeEvent<HTMLSelectElement>) => { | ||
setSelectedDifficulty(event.target.value); | ||
}; | ||
|
||
const handleTopicChange = (event: React.ChangeEvent<HTMLSelectElement>) => { | ||
setSelectedTopic(event.target.value); | ||
}; | ||
|
||
const handleMatchMe = async () => { | ||
if (!selectedDifficulty || !selectedTopic) { | ||
alert("Please select a difficulty and topic!"); | ||
return; | ||
} | ||
|
||
try { | ||
const response = await fetch("http://localhost:3002/match-user", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ difficulty: selectedDifficulty, topic: selectedTopic }), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error("Error matching"); | ||
} | ||
|
||
const data = await response.json(); | ||
alert(`Matched with ${data.opponentUsername}!`); | ||
} catch (error) { | ||
alert("Error matching"); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h2>MockMatch</h2> | ||
<label> | ||
Difficulty: | ||
<select value={selectedDifficulty} onChange={handleDifficultyChange}> | ||
<option value="">Select difficulty</option> | ||
{difficulties.map((difficulty) => ( | ||
<option key={difficulty} value={difficulty}> | ||
{difficulty} | ||
</option> | ||
))} | ||
</select> | ||
</label> | ||
<br /> | ||
<label> | ||
Topic: | ||
<select value={selectedTopic} onChange={handleTopicChange}> | ||
<option value="">Select topic</option> | ||
{topics.map((topic) => ( | ||
<option key={topic} value={topic}> | ||
{topic} | ||
</option> | ||
))} | ||
</select> | ||
</label> | ||
<br /> | ||
<button onClick={handleMatchMe}>Match me!</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default MockMatch; | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import express from 'express'; | ||
import router from "./routes/matching-route"; | ||
|
||
const app = express(); | ||
|
||
app.use(router); | ||
|
||
app.listen(3002, () => { | ||
console.log("Server started on port 3002"); | ||
}) |
Oops, something went wrong.