From 1ed54bad90c8ae350b7e7618fc8c5faa57b06b76 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Sun, 3 Sep 2023 17:07:39 +0100 Subject: [PATCH] Change confetti to show on completion of quiz --- src/components/Confetti.tsx | 11 +-- src/components/Questions/QuestionsList.tsx | 76 ++++++++++++--------- src/pages/questions/[questionGroupId].astro | 2 +- 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/components/Confetti.tsx b/src/components/Confetti.tsx index 79c517cb9..c4b87fe0d 100644 --- a/src/components/Confetti.tsx +++ b/src/components/Confetti.tsx @@ -9,12 +9,13 @@ type ConfettiPosition = { }; type ConfettiProps = { - element: HTMLElement | null; - onDone: () => void; + pieces?: number; + element?: HTMLElement | null; + onDone?: () => void; }; export function Confetti(props: ConfettiProps) { - const { element, onDone } = props; + const { element = document.body, onDone = () => null, pieces = 40 } = props; const [confettiPos, setConfettiPos] = useState< undefined | ConfettiPosition @@ -48,7 +49,7 @@ export function Confetti(props: ConfettiProps) { return ( { setConfettiPos(undefined); @@ -56,7 +57,7 @@ export function Confetti(props: ConfettiProps) { }} initialVelocityX={4} initialVelocityY={8} - tweenDuration={25} + tweenDuration={10} confettiSource={{ x: confettiPos.x, y: confettiPos.y, diff --git a/src/components/Questions/QuestionsList.tsx b/src/components/Questions/QuestionsList.tsx index 64525e36d..7c0c525f7 100644 --- a/src/components/Questions/QuestionsList.tsx +++ b/src/components/Questions/QuestionsList.tsx @@ -5,10 +5,10 @@ import { QuestionCard } from './QuestionCard'; import { QuestionLoader } from './QuestionLoader'; import { isLoggedIn } from '../../lib/jwt'; import type { QuestionType } from '../../lib/question-group'; -import { Confetti } from '../Confetti'; import { httpGet, httpPut } from '../../lib/http'; import { useToast } from '../../hooks/use-toast'; import { QuestionFinished } from './QuestionFinished'; +import { Confetti } from '../Confetti'; type UserQuestionProgress = { know: string[]; @@ -29,25 +29,12 @@ export function QuestionsList(props: QuestionsListProps) { const toast = useToast(); const [isLoading, setIsLoading] = useState(true); - const [confettiEl, setConfettiEl] = useState(null); - + const [showConfetti, setShowConfetti] = useState(false); const [questions, setQuestions] = useState(); const [pendingQuestions, setPendingQuestions] = useState([]); const [userProgress, setUserProgress] = useState(); - const alreadyKnowRef = useRef(null); - const didNotKnowRef = useRef(null); - - function showConfetti(el: HTMLElement | null) { - // If confetti is already showing, remove that first - if (confettiEl) { - setConfettiEl(null); - } - - window.setTimeout(() => { - setConfettiEl(el); - }, 0); - } + const containerRef = useRef(null); async function fetchUserProgress(): Promise< UserQuestionProgress | undefined @@ -57,7 +44,9 @@ export function QuestionsList(props: QuestionsListProps) { } const { response, error } = await httpGet( - `/v1-get-user-question-progress/${groupId}` + `${ + import.meta.env.PUBLIC_API_URL + }/v1-get-user-question-progress/${groupId}` ); if (error) { @@ -112,7 +101,9 @@ export function QuestionsList(props: QuestionsListProps) { setIsLoading(true); const { response, error } = await httpPut( - `/v1-reset-question-progress/${groupId}`, + `${ + import.meta.env.PUBLIC_API_URL + }/v1-reset-question-progress/${groupId}`, { type, } @@ -163,7 +154,9 @@ export function QuestionsList(props: QuestionsListProps) { } } else { const { response, error } = await httpPut( - `/v1-update-question-status/${groupId}`, + `${ + import.meta.env.PUBLIC_API_URL + }/v1-update-question-status/${groupId}`, { status, questionId, @@ -179,9 +172,17 @@ export function QuestionsList(props: QuestionsListProps) { newProgress = response; } + const updatedQuestionList = pendingQuestions.filter( + (q) => q.id !== questionId + ); + setUserProgress(newProgress); - setPendingQuestions(pendingQuestions.filter((q) => q.id !== questionId)); + setPendingQuestions(updatedQuestionList); setIsLoading(false); + + if (updatedQuestionList.length === 0) { + setShowConfetti(true); + } } useEffect(() => { @@ -194,16 +195,10 @@ export function QuestionsList(props: QuestionsListProps) { const hasProgress = knowCount > 0 || dontKnowCount > 0 || skipCount > 0; const currQuestion = pendingQuestions[0]; + const hasFinished = !isLoading && hasProgress && !currQuestion; return (
- { - setConfettiEl(null); - }} - /> - -
- {!isLoading && hasProgress && !currQuestion && ( + {showConfetti && containerRef.current && ( + { + setShowConfetti(false); + }} + /> + )} + +
+ {hasFinished && ( }
-
+
- +