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

Function AdminAddMCQ

apps/web/components/admin/AdminAddMCQ.tsx:11–145  ·  view source on GitHub ↗
({ problem }: { problem: AdminAddMCQProps })

Source from the content-addressed store, hash-verified

9}
10
11const AdminAddMCQ = ({ problem }: { problem: AdminAddMCQProps }) => {
12 const [mcqs, setMcqs] = useState<Omit<MCQQuestion, "id">[]>([]);
13 const [ExistingMCQs, setExistingMCQs] = useState<MCQQuestion[]>(problem.mcqQuestions);
14 const [question, setQuestion] = useState<string>("");
15 const [options, setOptions] = useState<string[]>([]);
16 const [option, setoption] = useState<string>("");
17 const [correctOption, setCorrectOption] = useState<string>("");
18 const [isUpdate, setIsUpdate] = useState<boolean>(false);
19
20 useEffect(() => {
21 async function fetchMCQs() {
22 const mcqs = await getAllMCQQuestion(problem.id);
23 setExistingMCQs(mcqs);
24 }
25 fetchMCQs();
26 }, [mcqs, isUpdate]);
27
28 function handleAddOption() {
29 setOptions([...options, ...option.split(",")]);
30 setoption("");
31 }
32 function handleRemoveOption(option: string) {
33 setOptions(options.filter((o) => o !== option));
34 }
35 function handleAddMCQ() {
36 const data = {
37 question: question,
38 options: options,
39 correctOption: correctOption,
40 problemId: problem.id,
41 };
42 setMcqs([...mcqs, data]);
43 setQuestion("");
44 setOptions([]);
45 setoption("");
46 setCorrectOption("");
47 }
48 function handleRemoveMCQ(question: string) {
49 setMcqs(mcqs.filter((mcq) => mcq.question !== question));
50 }
51
52 function handleSubmit() {
53 mcqs.map(async (mcq) => {
54 await createMCQ(mcq);
55 });
56 setMcqs([]);
57 }
58
59 function handleRemoveExistingMCQs(id: string) {
60 deleteMCQ(id);
61 setExistingMCQs(ExistingMCQs.filter((mcq) => mcq.id !== id));
62 }
63
64 return (
65 <Sheet>
66 <SheetTrigger>
67 <Button variant={"outline"}> Add MCQ </Button>
68 </SheetTrigger>

Callers

nothing calls this directly

Calls 5

handleRemoveMCQFunction · 0.85
handleRemoveExistingMCQsFunction · 0.85
fetchMCQsFunction · 0.70
handleRemoveOptionFunction · 0.70
handleSubmitFunction · 0.70

Tested by

no test coverage detected