({ mcq, setIsUpdate }: { mcq: MCQQuestion; setIsUpdate: Dispatch<SetStateAction<boolean>> })
| 16 | import { updateMCQ } from "../utils"; |
| 17 | |
| 18 | const EditMCQ = ({ mcq, setIsUpdate }: { mcq: MCQQuestion; setIsUpdate: Dispatch<SetStateAction<boolean>> }) => { |
| 19 | const [question, setQuestion] = useState<string>(mcq.question); |
| 20 | const [option, setOption] = useState<string>(""); |
| 21 | const [correctOption, setCorrectOption] = useState<string>(mcq.correctOption); |
| 22 | const [options, setOptions] = useState<string[]>(mcq.options); |
| 23 | const [isUpdating, setIsUpdating] = useState<boolean>(false); |
| 24 | |
| 25 | function handleAddOption() { |
| 26 | setOptions([...options, ...option.split(",")]); |
| 27 | setOption(""); |
| 28 | } |
| 29 | |
| 30 | function handleRemoveOption(option: string) { |
| 31 | setOptions(options.filter((o) => o !== option)); |
| 32 | } |
| 33 | |
| 34 | function handleUpdate() { |
| 35 | const asyncUpdate = async () => { |
| 36 | setIsUpdating(true); |
| 37 | await updateMCQ(mcq.id, { |
| 38 | id: mcq.id, |
| 39 | question, |
| 40 | options, |
| 41 | correctOption, |
| 42 | problemId: mcq.problemId, |
| 43 | }); |
| 44 | setIsUpdating(false); |
| 45 | }; |
| 46 | asyncUpdate(); |
| 47 | } |
| 48 | |
| 49 | return ( |
| 50 | <Dialog> |
| 51 | <DialogTrigger asChild> |
| 52 | <Button variant="outline">Edit</Button> |
| 53 | </DialogTrigger> |
| 54 | <DialogContent className="min-w-full"> |
| 55 | <DialogHeader> |
| 56 | <DialogTitle> |
| 57 | <div className="flex justify-center my-4 space-x-4"> |
| 58 | <Input |
| 59 | placeholder="Question" |
| 60 | value={question} |
| 61 | className="w-1/3" |
| 62 | onChange={(e) => setQuestion(e.target.value)} |
| 63 | /> |
| 64 | <div className="flex w-1/3 space-x-4"> |
| 65 | <Input placeholder="Option" value={option} onChange={(e) => setOption(e.target.value)} /> |
| 66 | <Button onClick={() => handleAddOption()}>Add Option</Button> |
| 67 | </div> |
| 68 | </div> |
| 69 | </DialogTitle> |
| 70 | </DialogHeader> |
| 71 | <div className="grid grid-cols-2 justify-center"> |
| 72 | {options.map((op, i) => ( |
| 73 | <Card key={i} className={"m-4 p-4 flex justify-between " + (op == correctOption ? "border-green-400" : "")}> |
| 74 | <CardTitle>{op}</CardTitle> |
| 75 | <div className="flex space-x-3"> |
nothing calls this directly
no test coverage detected