({ problem }: { problem: AdminAddMCQProps })
| 9 | } |
| 10 | |
| 11 | const 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> |
nothing calls this directly
no test coverage detected