({ problem }: { problem: Problem })
| 6 | import { createQuizScore, getAllMCQsForProblem } from "../utils"; |
| 7 | |
| 8 | function MCQCard({ problem }: { problem: Problem }) { |
| 9 | const session = useSession(); |
| 10 | |
| 11 | const [questions, setQuestions] = useState<mcq[]>([]); |
| 12 | const [score, setScore] = useState<number>(0); |
| 13 | const [isReset, setIsReset] = useState<boolean>(false); |
| 14 | const [isSubmitted, setIsSubmitted] = useState<boolean>(false); |
| 15 | const [feedback, setFeedback] = useState<string>(""); |
| 16 | const [atempted, setAtempted] = useState({}); |
| 17 | // @ts-ignore |
| 18 | const userId = session.data?.user?.id; |
| 19 | |
| 20 | useEffect(() => { |
| 21 | const fetchQuestions = async () => { |
| 22 | try { |
| 23 | const questions = await getAllMCQsForProblem(problem.id); |
| 24 | setQuestions(questions); |
| 25 | } catch (error) { |
| 26 | console.error("Error fetching MCQ questions:", error); |
| 27 | } |
| 28 | }; |
| 29 | fetchQuestions(); |
| 30 | }, [problem.id]); |
| 31 | |
| 32 | const handleResetScore = () => { |
| 33 | setAtempted({}); |
| 34 | setIsReset(!isReset); |
| 35 | setScore(0); |
| 36 | }; |
| 37 | |
| 38 | const handleSubmit = (score: number) => { |
| 39 | setIsSubmitted(!isSubmitted); |
| 40 | function getScore(atempted: any) { |
| 41 | let score = 0; |
| 42 | for (const key in atempted) { |
| 43 | if (atempted[key]) { |
| 44 | score++; |
| 45 | } |
| 46 | } |
| 47 | score = parseInt(((score / questions.length) * 100).toPrecision(3)); |
| 48 | return score; |
| 49 | } |
| 50 | setScore(getScore(atempted)); |
| 51 | |
| 52 | console.log("in submit", score); |
| 53 | async function submitQuiz() { |
| 54 | try { |
| 55 | let score = getScore(atempted); |
| 56 | console.log("in submit", atempted); |
| 57 | await createQuizScore({ |
| 58 | userId, |
| 59 | score, |
| 60 | problemId: problem.id, |
| 61 | }); |
| 62 | toast({ |
| 63 | description: "Submitted Successfully", |
| 64 | }); |
| 65 | } catch (error) { |
nothing calls this directly
no test coverage detected