mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2025-09-02 05:42:41 +02:00
wip
This commit is contained in:
@@ -3,6 +3,7 @@ import type { QuizQuestion } from '../../queries/ai-quiz';
|
|||||||
import { AIMCQQuestion } from './AIMCQQuestion';
|
import { AIMCQQuestion } from './AIMCQQuestion';
|
||||||
import { AIOpenEndedQuestion } from './AIOpenEndedQuestion';
|
import { AIOpenEndedQuestion } from './AIOpenEndedQuestion';
|
||||||
import { QuizTopNavigation } from './QuizTopNavigation';
|
import { QuizTopNavigation } from './QuizTopNavigation';
|
||||||
|
import { getPercentage } from '../../lib/number';
|
||||||
|
|
||||||
export type QuestionState = {
|
export type QuestionState = {
|
||||||
isSubmitted: boolean;
|
isSubmitted: boolean;
|
||||||
@@ -32,16 +33,16 @@ export function AIQuizContent(props: AIQuizContentProps) {
|
|||||||
const [activeQuestionIndex, setActiveQuestionIndex] = useState(0);
|
const [activeQuestionIndex, setActiveQuestionIndex] = useState(0);
|
||||||
const activeQuestion = questions[activeQuestionIndex];
|
const activeQuestion = questions[activeQuestionIndex];
|
||||||
|
|
||||||
const [selectedOptions, setSelectedOptions] = useState<
|
const [questionStates, setQuestionStates] = useState<
|
||||||
Record<number, QuestionState>
|
Record<number, QuestionState>
|
||||||
>({});
|
>({});
|
||||||
const [isAllQuestionsSubmitted, setIsAllQuestionsSubmitted] = useState(false);
|
const [isAllQuestionsSubmitted, setIsAllQuestionsSubmitted] = useState(false);
|
||||||
|
|
||||||
const activeQuestionState =
|
const activeQuestionState =
|
||||||
selectedOptions[activeQuestionIndex] ?? DEFAULT_QUESTION_STATE;
|
questionStates[activeQuestionIndex] ?? DEFAULT_QUESTION_STATE;
|
||||||
|
|
||||||
const handleSubmit = (status: QuestionState['status']) => {
|
const handleSubmit = (status: QuestionState['status']) => {
|
||||||
setSelectedOptions((prev) => {
|
setQuestionStates((prev) => {
|
||||||
const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE;
|
const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE;
|
||||||
|
|
||||||
const newSelectedOptions = {
|
const newSelectedOptions = {
|
||||||
@@ -60,7 +61,7 @@ export function AIQuizContent(props: AIQuizContentProps) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSetUserAnswer = (userAnswer: string) => {
|
const handleSetUserAnswer = (userAnswer: string) => {
|
||||||
setSelectedOptions((prev) => {
|
setQuestionStates((prev) => {
|
||||||
const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE;
|
const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE;
|
||||||
|
|
||||||
const newSelectedOptions = {
|
const newSelectedOptions = {
|
||||||
@@ -76,7 +77,7 @@ export function AIQuizContent(props: AIQuizContentProps) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSetCorrectAnswer = (correctAnswer: string) => {
|
const handleSetCorrectAnswer = (correctAnswer: string) => {
|
||||||
setSelectedOptions((prev) => {
|
setQuestionStates((prev) => {
|
||||||
const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE;
|
const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE;
|
||||||
|
|
||||||
const newSelectedOptions = {
|
const newSelectedOptions = {
|
||||||
@@ -92,7 +93,7 @@ export function AIQuizContent(props: AIQuizContentProps) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSelectOptions = (options: number[]) => {
|
const handleSelectOptions = (options: number[]) => {
|
||||||
setSelectedOptions((prev) => {
|
setQuestionStates((prev) => {
|
||||||
const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE;
|
const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE;
|
||||||
|
|
||||||
const newSelectedOptions = {
|
const newSelectedOptions = {
|
||||||
@@ -114,7 +115,7 @@ export function AIQuizContent(props: AIQuizContentProps) {
|
|||||||
const totalQuestions = questions?.length ?? 0;
|
const totalQuestions = questions?.length ?? 0;
|
||||||
const progressPercentage = isLoading
|
const progressPercentage = isLoading
|
||||||
? 0
|
? 0
|
||||||
: Math.min(((activeQuestionIndex + 1) / totalQuestions) * 100, 100);
|
: getPercentage(activeQuestionIndex + 1, totalQuestions);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="mx-auto w-full max-w-lg py-10">
|
<div className="mx-auto w-full max-w-lg py-10">
|
||||||
|
@@ -32,5 +32,5 @@ export function getPercentage(portion: number, total: number): number {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const percentage = (portion / total) * 100;
|
const percentage = (portion / total) * 100;
|
||||||
return Math.round(percentage);
|
return Math.min(Math.round(percentage), 100);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user