From a847d0b08db371567844245aeab94abb8f0be35d Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Sun, 3 Sep 2023 12:02:34 +0100 Subject: [PATCH] Show user progress --- src/components/Questions/QuestionsList.tsx | 43 ++++++++++------- .../Questions/QuestionsProgress.tsx | 47 +++++++++++++++---- 2 files changed, 65 insertions(+), 25 deletions(-) diff --git a/src/components/Questions/QuestionsList.tsx b/src/components/Questions/QuestionsList.tsx index 3d615c0c2..3f219bbdc 100644 --- a/src/components/Questions/QuestionsList.tsx +++ b/src/components/Questions/QuestionsList.tsx @@ -12,6 +12,7 @@ import { useToast } from '../../hooks/use-toast'; type UserQuestionProgress = { know: string[]; didNotKnow: string[]; + skipped: string[]; }; type QuestionsListProps = { @@ -20,13 +21,11 @@ type QuestionsListProps = { }; export function QuestionsList(props: QuestionsListProps) { - const { questions: defaultQuestions, groupId } = props; + const { questions: unshuffledQuestions, groupId } = props; const toast = useToast(); const [isLoading, setIsLoading] = useState(true); - const [isUpdatingStatus, setIsUpdatingStatus] = useState(false); - const [confettiEl, setConfettiEl] = useState(null); const [questions, setQuestions] = useState(); @@ -72,33 +71,37 @@ export function QuestionsList(props: QuestionsListProps) { const knownQuestions = userProgress?.know || []; const didNotKnowQuestions = userProgress?.didNotKnow || []; + const skippedQuestions = userProgress?.skipped || []; - const pendingQuestions = defaultQuestions.filter((question) => { + const pendingQuestions = unshuffledQuestions.filter((question) => { return ( !knownQuestions.includes(question.id) && - !didNotKnowQuestions.includes(question.id) + !didNotKnowQuestions.includes(question.id) && + !skippedQuestions.includes(question.id) ); }); // Shuffle and set pending questions setPendingQuestions(pendingQuestions.sort(() => Math.random() - 0.5)); - setQuestions(defaultQuestions); + setQuestions(unshuffledQuestions); setIsLoading(false); } async function updateQuestionStatus( - status: 'know' | 'dontKnow', + status: 'know' | 'dontKnow' | 'skip', questionId: string ) { - setIsUpdatingStatus(true); - let newProgress = userProgress || { know: [], didNotKnow: [] }; + setIsLoading(true); + let newProgress = userProgress || { know: [], didNotKnow: [], skipped: [] }; if (!isLoggedIn()) { if (status === 'know') { newProgress.know.push(questionId); - } else { + } else if (status == 'dontKnow') { newProgress.didNotKnow.push(questionId); + } else if (status == 'skip') { + newProgress.skipped.push(questionId); } } else { const { response, error } = await httpPut( @@ -120,16 +123,17 @@ export function QuestionsList(props: QuestionsListProps) { setUserProgress(newProgress); setPendingQuestions(pendingQuestions.filter((q) => q.id !== questionId)); - setIsUpdatingStatus(false); + setIsLoading(false); } useEffect(() => { loadQuestions().then(() => null); - }, [defaultQuestions]); + }, [unshuffledQuestions]); const knownCount = userProgress?.know.length || 0; const didNotKnowCount = userProgress?.didNotKnow.length || 0; - const hasProgress = knownCount > 0 || didNotKnowCount > 0; + const skippedCount = userProgress?.skipped.length || 0; + const hasProgress = knownCount > 0 || didNotKnowCount > 0 || skippedCount > 0; const currQuestion = pendingQuestions[0]; @@ -143,6 +147,10 @@ export function QuestionsList(props: QuestionsListProps) { /> @@ -154,7 +162,7 @@ export function QuestionsList(props: QuestionsListProps) {