1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-09-01 21:32:35 +02:00
This commit is contained in:
Arik Chakma
2025-07-02 20:58:25 +06:00
parent adfbb818a6
commit fff19eb566
2 changed files with 9 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ import type { QuizQuestion } from '../../queries/ai-quiz';
import { AIMCQQuestion } from './AIMCQQuestion';
import { AIOpenEndedQuestion } from './AIOpenEndedQuestion';
import { QuizTopNavigation } from './QuizTopNavigation';
import { getPercentage } from '../../lib/number';
export type QuestionState = {
isSubmitted: boolean;
@@ -32,16 +33,16 @@ export function AIQuizContent(props: AIQuizContentProps) {
const [activeQuestionIndex, setActiveQuestionIndex] = useState(0);
const activeQuestion = questions[activeQuestionIndex];
const [selectedOptions, setSelectedOptions] = useState<
const [questionStates, setQuestionStates] = useState<
Record<number, QuestionState>
>({});
const [isAllQuestionsSubmitted, setIsAllQuestionsSubmitted] = useState(false);
const activeQuestionState =
selectedOptions[activeQuestionIndex] ?? DEFAULT_QUESTION_STATE;
questionStates[activeQuestionIndex] ?? DEFAULT_QUESTION_STATE;
const handleSubmit = (status: QuestionState['status']) => {
setSelectedOptions((prev) => {
setQuestionStates((prev) => {
const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE;
const newSelectedOptions = {
@@ -60,7 +61,7 @@ export function AIQuizContent(props: AIQuizContentProps) {
};
const handleSetUserAnswer = (userAnswer: string) => {
setSelectedOptions((prev) => {
setQuestionStates((prev) => {
const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE;
const newSelectedOptions = {
@@ -76,7 +77,7 @@ export function AIQuizContent(props: AIQuizContentProps) {
};
const handleSetCorrectAnswer = (correctAnswer: string) => {
setSelectedOptions((prev) => {
setQuestionStates((prev) => {
const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE;
const newSelectedOptions = {
@@ -92,7 +93,7 @@ export function AIQuizContent(props: AIQuizContentProps) {
};
const handleSelectOptions = (options: number[]) => {
setSelectedOptions((prev) => {
setQuestionStates((prev) => {
const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE;
const newSelectedOptions = {
@@ -114,7 +115,7 @@ export function AIQuizContent(props: AIQuizContentProps) {
const totalQuestions = questions?.length ?? 0;
const progressPercentage = isLoading
? 0
: Math.min(((activeQuestionIndex + 1) / totalQuestions) * 100, 100);
: getPercentage(activeQuestionIndex + 1, totalQuestions);
return (
<div className="mx-auto w-full max-w-lg py-10">

View File

@@ -32,5 +32,5 @@ export function getPercentage(portion: number, total: number): number {
}
const percentage = (portion / total) * 100;
return Math.round(percentage);
return Math.min(Math.round(percentage), 100);
}