From cbe758349c0ede791d2bd4194827dabd980324b7 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Sun, 3 Sep 2023 12:14:20 +0100 Subject: [PATCH] Add reset progress functionality --- src/components/Questions/QuestionsList.tsx | 92 +++++++++++++++---- .../Questions/QuestionsProgress.tsx | 8 +- 2 files changed, 83 insertions(+), 17 deletions(-) diff --git a/src/components/Questions/QuestionsList.tsx b/src/components/Questions/QuestionsList.tsx index 3f219bbdc..e8ce4b740 100644 --- a/src/components/Questions/QuestionsList.tsx +++ b/src/components/Questions/QuestionsList.tsx @@ -11,10 +11,12 @@ import { useToast } from '../../hooks/use-toast'; type UserQuestionProgress = { know: string[]; - didNotKnow: string[]; - skipped: string[]; + dontKnow: string[]; + skip: string[]; }; +type ProgressType = keyof UserQuestionProgress; + type QuestionsListProps = { groupId: string; questions: QuestionType[]; @@ -70,14 +72,14 @@ export function QuestionsList(props: QuestionsListProps) { setUserProgress(userProgress); const knownQuestions = userProgress?.know || []; - const didNotKnowQuestions = userProgress?.didNotKnow || []; - const skippedQuestions = userProgress?.skipped || []; + const didNotKnowQuestions = userProgress?.dontKnow || []; + const skipQuestions = userProgress?.skip || []; const pendingQuestions = unshuffledQuestions.filter((question) => { return ( !knownQuestions.includes(question.id) && !didNotKnowQuestions.includes(question.id) && - !skippedQuestions.includes(question.id) + !skipQuestions.includes(question.id) ); }); @@ -88,20 +90,75 @@ export function QuestionsList(props: QuestionsListProps) { setIsLoading(false); } + async function resetProgress(type: ProgressType | 'all' = 'all') { + let knownQuestions = userProgress?.know || []; + let didNotKnowQuestions = userProgress?.dontKnow || []; + let skipQuestions = userProgress?.skip || []; + + if (!isLoggedIn()) { + if (type === 'know') { + knownQuestions = []; + } else if (type === 'dontKnow') { + didNotKnowQuestions = []; + } else if (type === 'skip') { + skipQuestions = []; + } else if (type === 'all') { + knownQuestions = []; + didNotKnowQuestions = []; + skipQuestions = []; + } + } else { + setIsLoading(true); + + const { response, error } = await httpPut( + `/v1-reset-question-progress/${groupId}`, + { + type, + } + ); + + if (error) { + toast.error(error.message || 'Error resetting progress'); + return; + } + + knownQuestions = response?.know || []; + didNotKnowQuestions = response?.dontKnow || []; + skipQuestions = response?.skip || []; + } + + const pendingQuestions = unshuffledQuestions.filter((question) => { + return ( + !knownQuestions.includes(question.id) && + !didNotKnowQuestions.includes(question.id) && + !skipQuestions.includes(question.id) + ); + }); + + setUserProgress({ + know: knownQuestions, + dontKnow: didNotKnowQuestions, + skip: skipQuestions, + }); + + setPendingQuestions(pendingQuestions.sort(() => Math.random() - 0.5)); + setIsLoading(false); + } + async function updateQuestionStatus( - status: 'know' | 'dontKnow' | 'skip', + status: ProgressType, questionId: string ) { setIsLoading(true); - let newProgress = userProgress || { know: [], didNotKnow: [], skipped: [] }; + let newProgress = userProgress || { know: [], dontKnow: [], skip: [] }; if (!isLoggedIn()) { if (status === 'know') { newProgress.know.push(questionId); } else if (status == 'dontKnow') { - newProgress.didNotKnow.push(questionId); + newProgress.dontKnow.push(questionId); } else if (status == 'skip') { - newProgress.skipped.push(questionId); + newProgress.skip.push(questionId); } } else { const { response, error } = await httpPut( @@ -130,10 +187,10 @@ export function QuestionsList(props: QuestionsListProps) { loadQuestions().then(() => null); }, [unshuffledQuestions]); - const knownCount = userProgress?.know.length || 0; - const didNotKnowCount = userProgress?.didNotKnow.length || 0; - const skippedCount = userProgress?.skipped.length || 0; - const hasProgress = knownCount > 0 || didNotKnowCount > 0 || skippedCount > 0; + const knowCount = userProgress?.know.length || 0; + const dontKnowCount = userProgress?.dontKnow.length || 0; + const skipCount = userProgress?.skip.length || 0; + const hasProgress = knowCount > 0 || dontKnowCount > 0 || skipCount > 0; const currQuestion = pendingQuestions[0]; @@ -147,12 +204,15 @@ export function QuestionsList(props: QuestionsListProps) { /> { + resetProgress('all').finally(() => null); + }} />
diff --git a/src/components/Questions/QuestionsProgress.tsx b/src/components/Questions/QuestionsProgress.tsx index 84448d496..b201cdb1c 100644 --- a/src/components/Questions/QuestionsProgress.tsx +++ b/src/components/Questions/QuestionsProgress.tsx @@ -8,6 +8,7 @@ type QuestionsProgressProps = { didNotKnowCount?: number; totalCount?: number; skippedCount?: number; + onResetClick?: () => void; }; export function QuestionsProgress(props: QuestionsProgressProps) { @@ -18,6 +19,7 @@ export function QuestionsProgress(props: QuestionsProgressProps) { didNotKnowCount = 0, totalCount = 0, skippedCount = 0, + onResetClick = () => null, } = props; const totalSolved = knowCount + didNotKnowCount + skippedCount; @@ -64,7 +66,11 @@ export function QuestionsProgress(props: QuestionsProgressProps) { -