|
|
@ -24,14 +24,14 @@ type QuestionsListProps = { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
export function QuestionsList(props: QuestionsListProps) { |
|
|
|
export function QuestionsList(props: QuestionsListProps) { |
|
|
|
const { questions: unshuffledQuestions, groupId } = props; |
|
|
|
const { questions: defaultQuestions, groupId } = props; |
|
|
|
|
|
|
|
|
|
|
|
const toast = useToast(); |
|
|
|
const toast = useToast(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const [questions, setQuestions] = useState(defaultQuestions); |
|
|
|
const [isLoading, setIsLoading] = useState(true); |
|
|
|
const [isLoading, setIsLoading] = useState(true); |
|
|
|
const [showConfetti, setShowConfetti] = useState(false); |
|
|
|
const [showConfetti, setShowConfetti] = useState(false); |
|
|
|
const [questions, setQuestions] = useState<QuestionType[]>(); |
|
|
|
const [currQuestionIndex, setCurrQuestionIndex] = useState(0); |
|
|
|
const [pendingQuestions, setPendingQuestions] = useState<QuestionType[]>([]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const [userProgress, setUserProgress] = useState<UserQuestionProgress>(); |
|
|
|
const [userProgress, setUserProgress] = useState<UserQuestionProgress>(); |
|
|
|
const containerRef = useRef<HTMLDivElement>(null); |
|
|
|
const containerRef = useRef<HTMLDivElement>(null); |
|
|
@ -57,7 +57,7 @@ export function QuestionsList(props: QuestionsListProps) { |
|
|
|
return response; |
|
|
|
return response; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function loadQuestions() { |
|
|
|
async function prepareProgress() { |
|
|
|
const userProgress = await fetchUserProgress(); |
|
|
|
const userProgress = await fetchUserProgress(); |
|
|
|
setUserProgress(userProgress); |
|
|
|
setUserProgress(userProgress); |
|
|
|
|
|
|
|
|
|
|
@ -65,7 +65,7 @@ export function QuestionsList(props: QuestionsListProps) { |
|
|
|
const didNotKnowQuestions = userProgress?.dontKnow || []; |
|
|
|
const didNotKnowQuestions = userProgress?.dontKnow || []; |
|
|
|
const skipQuestions = userProgress?.skip || []; |
|
|
|
const skipQuestions = userProgress?.skip || []; |
|
|
|
|
|
|
|
|
|
|
|
const pendingQuestions = unshuffledQuestions.filter((question) => { |
|
|
|
const pendingQuestionIndex = questions.findIndex((question) => { |
|
|
|
return ( |
|
|
|
return ( |
|
|
|
!knownQuestions.includes(question.id) && |
|
|
|
!knownQuestions.includes(question.id) && |
|
|
|
!didNotKnowQuestions.includes(question.id) && |
|
|
|
!didNotKnowQuestions.includes(question.id) && |
|
|
@ -73,31 +73,21 @@ export function QuestionsList(props: QuestionsListProps) { |
|
|
|
); |
|
|
|
); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// Shuffle and set pending questions
|
|
|
|
setCurrQuestionIndex(pendingQuestionIndex); |
|
|
|
// setPendingQuestions(pendingQuestions.sort(() => Math.random() - 0.5));
|
|
|
|
|
|
|
|
setPendingQuestions(pendingQuestions); |
|
|
|
|
|
|
|
setQuestions(unshuffledQuestions); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setIsLoading(false); |
|
|
|
setIsLoading(false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function resetProgress(type: QuestionProgressType | 'reset' = 'reset') { |
|
|
|
async function resetProgress() { |
|
|
|
let knownQuestions = userProgress?.know || []; |
|
|
|
let knownQuestions = userProgress?.know || []; |
|
|
|
let didNotKnowQuestions = userProgress?.dontKnow || []; |
|
|
|
let didNotKnowQuestions = userProgress?.dontKnow || []; |
|
|
|
let skipQuestions = userProgress?.skip || []; |
|
|
|
let skipQuestions = userProgress?.skip || []; |
|
|
|
|
|
|
|
|
|
|
|
if (!isLoggedIn()) { |
|
|
|
if (!isLoggedIn()) { |
|
|
|
if (type === 'know') { |
|
|
|
setQuestions(defaultQuestions); |
|
|
|
knownQuestions = []; |
|
|
|
|
|
|
|
} else if (type === 'dontKnow') { |
|
|
|
|
|
|
|
didNotKnowQuestions = []; |
|
|
|
|
|
|
|
} else if (type === 'skip') { |
|
|
|
|
|
|
|
skipQuestions = []; |
|
|
|
|
|
|
|
} else if (type === 'reset') { |
|
|
|
|
|
|
|
knownQuestions = []; |
|
|
|
knownQuestions = []; |
|
|
|
didNotKnowQuestions = []; |
|
|
|
didNotKnowQuestions = []; |
|
|
|
skipQuestions = []; |
|
|
|
skipQuestions = []; |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
setIsLoading(true); |
|
|
|
setIsLoading(true); |
|
|
|
|
|
|
|
|
|
|
@ -106,7 +96,7 @@ export function QuestionsList(props: QuestionsListProps) { |
|
|
|
import.meta.env.PUBLIC_API_URL |
|
|
|
import.meta.env.PUBLIC_API_URL |
|
|
|
}/v1-reset-question-progress/${groupId}`,
|
|
|
|
}/v1-reset-question-progress/${groupId}`,
|
|
|
|
{ |
|
|
|
{ |
|
|
|
status: type, |
|
|
|
status: 'reset', |
|
|
|
}, |
|
|
|
}, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
@ -120,21 +110,13 @@ export function QuestionsList(props: QuestionsListProps) { |
|
|
|
skipQuestions = response?.skip || []; |
|
|
|
skipQuestions = response?.skip || []; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const pendingQuestions = unshuffledQuestions.filter((question) => { |
|
|
|
setCurrQuestionIndex(0); |
|
|
|
return ( |
|
|
|
|
|
|
|
!knownQuestions.includes(question.id) && |
|
|
|
|
|
|
|
!didNotKnowQuestions.includes(question.id) && |
|
|
|
|
|
|
|
!skipQuestions.includes(question.id) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setUserProgress({ |
|
|
|
setUserProgress({ |
|
|
|
know: knownQuestions, |
|
|
|
know: knownQuestions, |
|
|
|
dontKnow: didNotKnowQuestions, |
|
|
|
dontKnow: didNotKnowQuestions, |
|
|
|
skip: skipQuestions, |
|
|
|
skip: skipQuestions, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
setPendingQuestions(pendingQuestions.sort(() => Math.random() - 0.5)); |
|
|
|
|
|
|
|
setIsLoading(false); |
|
|
|
setIsLoading(false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -173,30 +155,29 @@ export function QuestionsList(props: QuestionsListProps) { |
|
|
|
newProgress = response; |
|
|
|
newProgress = response; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const updatedQuestionList = pendingQuestions.filter( |
|
|
|
const nextQuestionIndex = currQuestionIndex + 1; |
|
|
|
(q) => q.id !== questionId, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setUserProgress(newProgress); |
|
|
|
setUserProgress(newProgress); |
|
|
|
setPendingQuestions(updatedQuestionList); |
|
|
|
|
|
|
|
setIsLoading(false); |
|
|
|
setIsLoading(false); |
|
|
|
|
|
|
|
|
|
|
|
if (updatedQuestionList.length === 0) { |
|
|
|
if (!nextQuestionIndex || !questions[nextQuestionIndex]) { |
|
|
|
setShowConfetti(true); |
|
|
|
setShowConfetti(true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setCurrQuestionIndex(nextQuestionIndex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
useEffect(() => { |
|
|
|
loadQuestions().then(() => null); |
|
|
|
prepareProgress().then(() => null); |
|
|
|
}, [unshuffledQuestions]); |
|
|
|
}, [questions]); |
|
|
|
|
|
|
|
|
|
|
|
const knowCount = userProgress?.know.length || 0; |
|
|
|
const knowCount = userProgress?.know.length || 0; |
|
|
|
const dontKnowCount = userProgress?.dontKnow.length || 0; |
|
|
|
const dontKnowCount = userProgress?.dontKnow.length || 0; |
|
|
|
const skipCount = userProgress?.skip.length || 0; |
|
|
|
const skipCount = userProgress?.skip.length || 0; |
|
|
|
const hasProgress = knowCount > 0 || dontKnowCount > 0 || skipCount > 0; |
|
|
|
const hasProgress = knowCount > 0 || dontKnowCount > 0 || skipCount > 0; |
|
|
|
|
|
|
|
|
|
|
|
const currQuestion = pendingQuestions[0]; |
|
|
|
const currQuestion = questions[currQuestionIndex]; |
|
|
|
const hasFinished = !isLoading && hasProgress && !currQuestion; |
|
|
|
const hasFinished = !isLoading && hasProgress && currQuestionIndex === -1; |
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
return ( |
|
|
|
<div className="mb-0 gap-3 text-center sm:mb-40"> |
|
|
|
<div className="mb-0 gap-3 text-center sm:mb-40"> |
|
|
@ -204,11 +185,37 @@ export function QuestionsList(props: QuestionsListProps) { |
|
|
|
knowCount={knowCount} |
|
|
|
knowCount={knowCount} |
|
|
|
didNotKnowCount={dontKnowCount} |
|
|
|
didNotKnowCount={dontKnowCount} |
|
|
|
skippedCount={skipCount} |
|
|
|
skippedCount={skipCount} |
|
|
|
totalCount={unshuffledQuestions?.length || questions?.length} |
|
|
|
totalCount={questions?.length} |
|
|
|
isLoading={isLoading} |
|
|
|
isLoading={isLoading} |
|
|
|
showLoginAlert={!isLoggedIn() && hasProgress} |
|
|
|
showLoginAlert={!isLoggedIn() && hasProgress} |
|
|
|
onResetClick={() => { |
|
|
|
onResetClick={() => { |
|
|
|
resetProgress('reset').finally(() => null); |
|
|
|
resetProgress().finally(() => null); |
|
|
|
|
|
|
|
}} |
|
|
|
|
|
|
|
onNextClick={() => { |
|
|
|
|
|
|
|
if ( |
|
|
|
|
|
|
|
currQuestionIndex !== -1 && |
|
|
|
|
|
|
|
currQuestionIndex < questions.length - 1 |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
updateQuestionStatus('skip', currQuestion.id).finally(() => null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}} |
|
|
|
|
|
|
|
onPrevClick={() => { |
|
|
|
|
|
|
|
if (currQuestionIndex > 0) { |
|
|
|
|
|
|
|
const prevQuestion = questions[currQuestionIndex - 1]; |
|
|
|
|
|
|
|
// remove last question from the progress of the user
|
|
|
|
|
|
|
|
const tempUserProgress = { |
|
|
|
|
|
|
|
know: |
|
|
|
|
|
|
|
userProgress?.know.filter((id) => id !== prevQuestion.id) || [], |
|
|
|
|
|
|
|
dontKnow: |
|
|
|
|
|
|
|
userProgress?.dontKnow.filter((id) => id !== prevQuestion.id) || |
|
|
|
|
|
|
|
[], |
|
|
|
|
|
|
|
skip: |
|
|
|
|
|
|
|
userProgress?.skip.filter((id) => id !== prevQuestion.id) || [], |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setUserProgress(tempUserProgress); |
|
|
|
|
|
|
|
setCurrQuestionIndex(currQuestionIndex - 1); |
|
|
|
|
|
|
|
} |
|
|
|
}} |
|
|
|
}} |
|
|
|
/> |
|
|
|
/> |
|
|
|
|
|
|
|
|
|
|
@ -228,12 +235,12 @@ export function QuestionsList(props: QuestionsListProps) { |
|
|
|
> |
|
|
|
> |
|
|
|
{hasFinished && ( |
|
|
|
{hasFinished && ( |
|
|
|
<QuestionFinished |
|
|
|
<QuestionFinished |
|
|
|
totalCount={unshuffledQuestions?.length || questions?.length || 0} |
|
|
|
totalCount={questions?.length || 0} |
|
|
|
knowCount={knowCount} |
|
|
|
knowCount={knowCount} |
|
|
|
didNotKnowCount={dontKnowCount} |
|
|
|
didNotKnowCount={dontKnowCount} |
|
|
|
skippedCount={skipCount} |
|
|
|
skippedCount={skipCount} |
|
|
|
onReset={(type: QuestionProgressType | 'reset') => { |
|
|
|
onReset={() => { |
|
|
|
resetProgress(type).finally(() => null); |
|
|
|
resetProgress().finally(() => null); |
|
|
|
}} |
|
|
|
}} |
|
|
|
/> |
|
|
|
/> |
|
|
|
)} |
|
|
|
)} |
|
|
|