From bd0c6ab1e5c65de232c574ff147f014b292e1bad Mon Sep 17 00:00:00 2001 From: B-lovedth Date: Mon, 23 Oct 2023 15:31:07 +0100 Subject: [PATCH 1/3] Refactor-Fix: Assessment History Page code Refactor --- http/userTakenAssessment.ts | 4 ++-- modules/assessment/component/History.tsx | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/http/userTakenAssessment.ts b/http/userTakenAssessment.ts index 96fff94d3..027bb6c0c 100644 --- a/http/userTakenAssessment.ts +++ b/http/userTakenAssessment.ts @@ -1,7 +1,7 @@ import axios from 'axios'; import $http from './axios'; -const assessmentBaseUrl = `https://assessment.cofucan.tech/api/v1`; +const assessmentBaseUrl = `https://hng-stage-9.onrender.com/api/v1`; export const fetchAssessmentHistory = async (token: string) => { try { @@ -10,7 +10,7 @@ export const fetchAssessmentHistory = async (token: string) => { token: token, }, }); - return res.data; + return res.data.data; } catch (error) { console.error('Error Assessment History:', error); throw error; diff --git a/modules/assessment/component/History.tsx b/modules/assessment/component/History.tsx index 4bacfb65b..695232ae7 100644 --- a/modules/assessment/component/History.tsx +++ b/modules/assessment/component/History.tsx @@ -39,7 +39,7 @@ const History: React.FC = () => { } = useQuery(['assessmentsHistory'], () => fetchAssessmentHistory(tokenRef.current as string)); console.log('history', assessmentsHistory); - const assessments = assessmentsHistory; + React.useEffect(() => { tokenRef.current = localStorage.getItem('zpt'); @@ -71,8 +71,7 @@ const History: React.FC = () => { setCurrentPage(1); }; - const filteredAssessments = assessments - ?.filter( + const filteredAssessments = assessmentsHistory?.filter( (assessment: Assessment) => assessment.assessment_name?.toLowerCase().includes(searchTerm.toLowerCase()) && (selectedLevel === 'All' ? true : assessment.badge_name?.toLowerCase() === selectedLevel), From 682dd891240138eb10d7ee80fc5b9cb56f7066e6 Mon Sep 17 00:00:00 2001 From: B-lovedth Date: Mon, 23 Oct 2023 22:20:31 +0100 Subject: [PATCH 2/3] Refactor-Fix: Update Overview page --- pages/assessments/overview/index.tsx | 79 +++++++++++++++++++--------- 1 file changed, 54 insertions(+), 25 deletions(-) diff --git a/pages/assessments/overview/index.tsx b/pages/assessments/overview/index.tsx index 062fb3c5b..b9b22866f 100644 --- a/pages/assessments/overview/index.tsx +++ b/pages/assessments/overview/index.tsx @@ -15,6 +15,7 @@ import { TimerStart } from 'iconsax-react'; import { withUserAuth } from '../../../helpers/withAuth'; import Head from 'next/head'; import Loader from '@ui/Loader'; +import { useQuery } from '@tanstack/react-query'; export interface Question { answer_id: number; @@ -56,7 +57,6 @@ function AssessmentOverview() { useEffect(() => { tokenRef.current = localStorage.getItem('zpt'); - handleGetSession(); }, []); useEffect(() => { @@ -85,21 +85,6 @@ function AssessmentOverview() { setTimeFunction(); }, [duration, minute, second]); - const handleGetSession = async () => { - const token = tokenRef.current; - try { - const res = await fetchUserAssessmentSession(token as string); - setResult(res); - if (!res) { - setIsLoading(false); - throw new Error('Network response was not ok'); - } else { - setResult(res); - setIsLoading(false); - } - } catch (error) {} - }; - function sortQuestionsByQuestionNo(input: QuestionArrays | undefined): Question[] { if (!input) return []; // Concatenate the 'answered_questions' and 'unanswered_questions' arrays @@ -130,13 +115,57 @@ function AssessmentOverview() { console.error('Error submitting assessment:', error); } }; - - return ( + const {data:sessionData,isLoading:sessionIsLoading,isError} = useQuery(['assessment'], () => fetchUserAssessmentSession(tokenRef.current as string), { + notifyOnChangeProps: ['data',"isError","isLoading"], + }) + console.log("data",sessionData) + console.log("isLoading",sessionIsLoading) + console.log("isError",isError) + if (isError) { + return ( + <> + + + Assessment | Overview + + + + + +
+
+

OOPS!

+

something went wrong

+
+
+
+ + ); + } + else + {return ( <> - Assessment | Overview + Overview - + {isTimeOut && ( - {isLoading ? ( + {sessionIsLoading ? (
@@ -165,13 +194,13 @@ function AssessmentOverview() { />
-

+

{(minute !== null || undefined) && (second !== null || undefined) ? ( setTimeUp(true)} minutes={minute} seconds={second} /> ) : ( - - : - - )} -

+
@@ -179,7 +208,7 @@ function AssessmentOverview() {
- {sortQuestionsByQuestionNo(result)?.map((item) => ( + {sessionData?.data?.length>1 && sortQuestionsByQuestionNo(sessionData)?.map((item) => ( - ); + );} } export default withUserAuth(AssessmentOverview); From 079588b0590ea753bb4b2e6d3339356812900e1c Mon Sep 17 00:00:00 2001 From: B-lovedth Date: Tue, 24 Oct 2023 20:44:25 +0100 Subject: [PATCH 3/3] Refactor-fix: Fixed logic for questions page --- http/userTakenAssessment.ts | 4 +-- .../assessments/take-test/questions/index.tsx | 28 +++++++++++++------ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/http/userTakenAssessment.ts b/http/userTakenAssessment.ts index 027bb6c0c..145a645a9 100644 --- a/http/userTakenAssessment.ts +++ b/http/userTakenAssessment.ts @@ -73,13 +73,13 @@ export const fetchUserTakenAssessment = async (token: string, id: any) => { export const fetchUserAssessmentSession = async (token: string) => { try { - const res = await axios.get(`${assessmentBaseUrl}/assessments/session/`, { + const res = await axios.get(`${assessmentBaseUrl}/assessments/session`, { headers: { 'Content-Type': 'application/json', token: token, }, }); - return res.data.data; + return res.data; } catch (error) { console.error('Error fetching user taken assessment:', error); throw new Error('Failed to fetch user taken assessment'); diff --git a/pages/assessments/take-test/questions/index.tsx b/pages/assessments/take-test/questions/index.tsx index 48d010963..3396ccaeb 100644 --- a/pages/assessments/take-test/questions/index.tsx +++ b/pages/assessments/take-test/questions/index.tsx @@ -98,10 +98,10 @@ const Questions: React.FC = () => { function sortQuestionsByQuestionNo(input: QuestionArrays | undefined): Question[] { if (!input) return []; // Concatenate the 'answered_questions' and 'unanswered_questions' arrays - const allQuestions = input.answered_questions.concat(input.unanswered_questions); + const allQuestions = input.answered_questions?.concat(input.unanswered_questions); // Sort the combined array based on 'question_no' - return allQuestions.sort((a, b) => a.question_no - b.question_no); + return allQuestions?.sort((a, b) => a.question_no - b.question_no); } const handleUserAnswerClick = async (question_id: number, user_answer_id: number, answer_text: string) => { const token = tokenRef.current; @@ -123,8 +123,16 @@ const Questions: React.FC = () => { useEffect(() => { if (!sessionLoading && !sessionIsErrorr && session?.length !== 0 && !sessionError) { const assessmentData = sortQuestionsByQuestionNo(session); - setStoredAssessment(assessmentData); - setIsLoading(false); + if ( + session.data?.questions?.length === 0 || + session?.data?.length <= 1 || + session?.data?.questions === undefined + ) { + setStoredAssessment(newQuestions?.data?.questions); + } else { + setStoredAssessment(assessmentData); + } + setIsLoading(sessionLoading); setIsError(false); } else if (!newIsLoading && !newIsError && newQuestions?.data?.questions?.length !== 0 && !newError) { setStoredAssessment(newQuestions?.data?.questions); @@ -133,11 +141,12 @@ const Questions: React.FC = () => { } else { setIsError(true); } - setAssessmentData(assessment); + setAssessmentData(assessment?.data); setDuration(assessmentData?.duration_minutes as number); console.log(newIsLoading, newIsError, newQuestions?.data?.questions, newError); console.log(sessionLoading, sessionIsErrorr, session, sessionError); console.log(assessmentData); + console.log('stored', storedAssessment); }, [ newQuestions?.data, newIsLoading, @@ -150,13 +159,14 @@ const Questions: React.FC = () => { newError, assessmentData?.duration_minutes, assessmentData, + storedAssessment, ]); useEffect(() => { const setTimeFunction = () => { if (typeof window !== 'undefined' && window.localStorage) { const minuteString = localStorage.getItem('minute'); const secondString = localStorage.getItem('second'); - const minuteInt = minuteString !== null ? parseInt(minuteString, 10) : 30; + const minuteInt = minuteString !== null ? parseInt(minuteString, 10) : duration as number; const secondInt = secondString !== null ? parseInt(secondString, 10) : 0; setMinute(minuteInt); setSecond(secondInt); @@ -200,7 +210,7 @@ const Questions: React.FC = () => { getItemWithExpiry('duration'); }, [duration, minute, second]); - if (isError) { + if (newIsError) { return ( <> @@ -274,7 +284,7 @@ const Questions: React.FC = () => { - {isLoading ? ( + {newIsLoading ? (
@@ -300,7 +310,7 @@ const Questions: React.FC = () => {
    - {storedAssessment.map((question: any, index: number) => ( + {storedAssessment?.map((question: any, index: number) => (
  • Question {storedAssessment.indexOf(question) + 1} of {storedAssessment?.length}