diff --git a/app/modes/page.tsx b/app/modes/page.tsx index b94fe31..4bbd6be 100644 --- a/app/modes/page.tsx +++ b/app/modes/page.tsx @@ -19,7 +19,7 @@ const Modes: NextPage<{ searchParams: { url: string; name: string } }> = ({ = ({ void; + setSavedAnswers: (data: any[]) => void; + savedAnswers: any; }; -const useResults = (data: ResultsData): ResultsHook => { +const useResults = (questions: Question[]): ResultsHook => { const [points, setPoints] = useState(0); + const [savedAnswers, setSavedAnswers] = useState([]); - const countPoints = (data: ResultsData) => { + const countPoints = () => { let points = 0; - for (let i = 0; i < data.questions?.length; i++) { - if (!data.answers[i] || !data.questions[i]) continue; - const noOfAnswers = data.questions[i]?.options - ? data.questions[i]?.options?.filter((el: any) => el.isAnswer === true) + for (let i = 0; i < questions?.length; i++) { + if (!savedAnswers[i] || !questions[i]) continue; + const noOfAnswers = questions[i]?.options + ? questions[i]?.options?.filter((el: any) => el.isAnswer === true) .length : 0; if (noOfAnswers > 1) { @@ -27,10 +24,10 @@ const useResults = (data: ResultsData): ResultsHook => { const pointRaise = Math.round((1 / noOfAnswers) * 100) / 100; let isOneBad = false; let pointRaisedCount = 0; - if (Array.isArray(data.answers[i])) { - for (const answer of data.answers[i] ?? []) { + if (Array.isArray(savedAnswers[i])) { + for (const answer of savedAnswers[i] ?? []) { if ( - data.questions[i]?.options + questions[i]?.options ?.filter((el: any) => el.isAnswer === true) .some((el: any) => el.text === answer) ) { @@ -38,7 +35,7 @@ const useResults = (data: ResultsData): ResultsHook => { pointRaisedCount = pointRaisedCount + 1; } if ( - data.questions[i]?.options + questions[i]?.options ?.filter((el: any) => el.isAnswer === false) .some((el: any) => el.text === answer) ) { @@ -55,11 +52,10 @@ const useResults = (data: ResultsData): ResultsHook => { (partialPoints > 0 ? Math.round(partialPoints * 100) / 100 : 0); points = Math.round(points * 100) / 100; } - } else if (noOfAnswers === 1 && !Array.isArray(data.answers[i])) { + } else if (noOfAnswers === 1 && !Array.isArray(savedAnswers[i])) { if ( - data.questions[i]?.options?.filter( - (el: any) => el.isAnswer === true, - )[0]?.text === data.answers[i] + questions[i]?.options?.filter((el: any) => el.isAnswer === true)[0] + ?.text === savedAnswers[i] ) { points = Math.round((points + 1) * 100) / 100; } @@ -68,9 +64,17 @@ const useResults = (data: ResultsData): ResultsHook => { setPoints(points); }; + // Trigger countPoints when savedAnswers is updated + useEffect(() => { + if (savedAnswers.length === questions.length) { + countPoints(); + } + }, [savedAnswers]); // Depend on savedAnswers to trigger the effect + return { points: points, - reCount: countPoints, + setSavedAnswers: setSavedAnswers, + savedAnswers: savedAnswers, }; }; diff --git a/lib/graphql/resolvers.tsx b/lib/graphql/resolvers.tsx index 34b15b2..6098e8b 100644 --- a/lib/graphql/resolvers.tsx +++ b/lib/graphql/resolvers.tsx @@ -24,8 +24,8 @@ const resolvers = { { dataSources }: { dataSources: any }, ) => { const response = await fetchQuestions(link); - const shuffled = response?.sort(() => 0.5 - Math.random()); - return shuffled?.slice(0, range); + // const shuffled = response?.sort(() => 0.5 - Math.random()); + return response?.slice(0, range); }, }, };