From facf30122baae802eeb30d389c013f726140d625 Mon Sep 17 00:00:00 2001 From: Gabriel Goh <77230723+gycgabriel@users.noreply.github.com> Date: Tue, 31 Oct 2023 17:08:24 +0800 Subject: [PATCH] Add call to useMatchmaking --- frontend/src/hooks/useMatchmaking.tsx | 6 ++++++ frontend/src/pages/room/[id].tsx | 21 ++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/frontend/src/hooks/useMatchmaking.tsx b/frontend/src/hooks/useMatchmaking.tsx index e3f1582e..472b0fba 100644 --- a/frontend/src/hooks/useMatchmaking.tsx +++ b/frontend/src/hooks/useMatchmaking.tsx @@ -1,5 +1,6 @@ import { useContext } from "react"; import { MatchmakingContext } from "../providers/MatchmakingProvider"; +import { getMatchByRoomid } from "@/pages/api/matchHandler"; export function useMatchmaking() { const context = useContext(MatchmakingContext); @@ -8,3 +9,8 @@ export function useMatchmaking() { } return context; } + +export const getQuestionIdFromMatch = async (roomId: string) => { + const match = await getMatchByRoomid(roomId); + return match?.questionId; +}; diff --git a/frontend/src/pages/room/[id].tsx b/frontend/src/pages/room/[id].tsx index 2e1d7138..adf6db88 100644 --- a/frontend/src/pages/room/[id].tsx +++ b/frontend/src/pages/room/[id].tsx @@ -7,6 +7,7 @@ import { useRouter } from "next/router"; import VideoRoom from "../../components/room/video-room"; import { Question } from "../../types/QuestionTypes"; import { useQuestions } from "@/hooks/useQuestions"; +import { getQuestionIdFromMatch } from "@/hooks/useMatchmaking"; export default function Room() { const router = useRouter(); @@ -38,17 +39,19 @@ export default function Room() { const { fetchQuestion } = useQuestions(); async function getQuestionId() { - return "1"; // todo + return getQuestionIdFromMatch(roomId); // todo } - getQuestionId().then((questionId) => - fetchQuestion(questionId).then((fetchQuestion) => { - if (fetchQuestion != null) { - question = fetchQuestion; - setQuestionId(question.id); - } - }) - ); + getQuestionId().then((questionId) => { + if (questionId != null) { + fetchQuestion(questionId).then((fetchQuestion) => { + if (fetchQuestion != null) { + question = fetchQuestion; + setQuestionId(question.id); + } + }); + } + }); if (!router.isReady) return null;