MCPcopy Create free account
hub / github.com/code100x/daily-code / MCQCard

Function MCQCard

apps/web/components/mcq/MCQCard.tsx:8–128  ·  view source on GitHub ↗
({ problem }: { problem: Problem })

Source from the content-addressed store, hash-verified

6import { createQuizScore, getAllMCQsForProblem } from "../utils";
7
8function 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) {

Callers

nothing calls this directly

Calls 3

fetchQuestionsFunction · 0.85
giveFeedbackFunction · 0.85
handleSubmitFunction · 0.70

Tested by

no test coverage detected