({
question,
setAtempted,
isReset,
isSubmitted,
setisSubmitted,
}: {
question: Question;
setAtempted: Dispatch<SetStateAction<Object>>;
isReset: boolean;
isSubmitted: boolean;
setisSubmitted: Dispatch<SetStateAction<boolean>>;
})
| 4 | import { MCQQuestion as Question } from "@prisma/client"; |
| 5 | |
| 6 | const MCQQuestion = ({ |
| 7 | question, |
| 8 | setAtempted, |
| 9 | isReset, |
| 10 | isSubmitted, |
| 11 | setisSubmitted, |
| 12 | }: { |
| 13 | question: Question; |
| 14 | setAtempted: Dispatch<SetStateAction<Object>>; |
| 15 | isReset: boolean; |
| 16 | isSubmitted: boolean; |
| 17 | setisSubmitted: Dispatch<SetStateAction<boolean>>; |
| 18 | }) => { |
| 19 | const [selectedOption, setSelectedOption] = useState<string | null>(null); |
| 20 | const [isCorrect, setIsCorrect] = useState<boolean | null>(null); |
| 21 | |
| 22 | const handleOptionSelect = (option: string) => { |
| 23 | if (isSubmitted) { |
| 24 | return; |
| 25 | } |
| 26 | if (selectedOption === option) { |
| 27 | setAtempted((prev) => ({ ...prev, [question.id]: false })); |
| 28 | setSelectedOption(null); |
| 29 | return; |
| 30 | } |
| 31 | setSelectedOption(option); |
| 32 | setAtempted((prev) => ({ ...prev, [question.id]: option == question.correctOption })); |
| 33 | setIsCorrect(null); |
| 34 | }; |
| 35 | |
| 36 | useEffect(() => { |
| 37 | if (isSubmitted) { |
| 38 | handleOnSubmit(); |
| 39 | } |
| 40 | }, [isSubmitted]); |
| 41 | |
| 42 | const handleOnSubmit = () => { |
| 43 | setisSubmitted(true); |
| 44 | if (selectedOption === question.correctOption) { |
| 45 | } else { |
| 46 | setIsCorrect(false); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | useEffect(() => { |
| 51 | setSelectedOption(null); |
| 52 | setIsCorrect(null); |
| 53 | setisSubmitted(false); |
| 54 | }, [isReset]); |
| 55 | |
| 56 | return ( |
| 57 | <div> |
| 58 | <Card className="my-4"> |
| 59 | <CardHeader> |
| 60 | <CardTitle>{question.question}</CardTitle> |
| 61 | </CardHeader> |
| 62 | <CardContent className="flex flex-col"> |
| 63 | {question.options.map((option, index) => ( |
nothing calls this directly
no test coverage detected