import React, { useEffect, useState } from 'react'; import { MdAdd, } from 'react-icons/md'; import Alert, { AlertItem } from '../Shared/Alert'; import { ReadingPart, TrueFalseExercise } from '@/interfaces/exam'; import QuestionsList from '../Shared/QuestionsList'; import Header from '../../Shared/Header'; import SortableQuestion from '../Shared/SortableQuestion'; import clsx from 'clsx'; import useExamEditorStore from '@/stores/examEditor'; import useSectionEdit from '../../Hooks/useSectionEdit'; import { toast } from 'react-toastify'; import validateTrueFalseQuestions from './validation'; import setEditingAlert from '../Shared/setEditingAlert'; import { DragEndEvent } from '@dnd-kit/core'; import { handleTrueFalseReorder } from '@/stores/examEditor/reorder/local'; import PromptEdit from '../Shared/PromptEdit'; const TrueFalse: React.FC<{ exercise: TrueFalseExercise, sectionId: number }> = ({ exercise, sectionId }) => { const { currentModule, dispatch } = useExamEditorStore(); const { state } = useExamEditorStore( (state) => state.modules[currentModule].sections.find((section) => section.sectionId === sectionId)! ); const section = state as ReadingPart; const [local, setLocal] = useState(exercise); const [alerts, setAlerts] = useState([]); const updateLocal = (exercise: TrueFalseExercise) => { setLocal(exercise); setEditing(true); }; const updateQuestion = (index: number, field: string, value: string) => { const newQuestions = [...local.questions]; newQuestions[index] = { ...newQuestions[index], [field]: value }; updateLocal({ ...local, questions: newQuestions }); }; const addQuestion = () => { const newId = (parseInt(local.questions[local.questions.length - 1].id) + 1).toString(); updateLocal({ ...local, questions: [ ...local.questions, { prompt: "", solution: undefined, id: newId } ] }); }; const deleteQuestion = (index: number) => { if (local.questions.length == 1) { toast.error("There needs to be at least one question!"); return; } const newQuestions = local.questions.filter((_, i) => i !== index); const minId = Math.min(...newQuestions.map(q => parseInt(q.id))); const updatedQuestions = newQuestions.map((question, i) => ({ ...question, id: String(minId + i) })); updateLocal({ ...local, questions: updatedQuestions }); }; const { editing, handleSave, handleDiscard, modeHandle, setEditing } = useSectionEdit({ sectionId, mode: "edit", onSave: () => { const isValid = validateTrueFalseQuestions( local.questions, setAlerts ); if (!isValid) { toast.error("Please fix the errors before saving!"); return; } setEditing(false); setAlerts([]); //dispatch({ type: 'UPDATE_ROOT', payload: { updates: { globalEdit: globalEdit.filter(id => id !== sectionId) } } }); const newSection = { ...section, exercises: section.exercises.map((ex) => ex.id === local.id ? local : ex) }; dispatch({ type: "UPDATE_SECTION_STATE", payload: { sectionId, update: newSection } }); }, onDiscard: () => { setLocal(exercise); }, onMode: () => { const newSection = { ...section, exercises: section.exercises.filter((ex) => ex.id !== local.id) }; dispatch({ type: "UPDATE_SECTION_STATE", payload: { sectionId, update: newSection } }); }, }); useEffect(() => { validateTrueFalseQuestions(local.questions, setAlerts); }, [local.questions]); useEffect(() => { setEditingAlert(editing, setAlerts); }, [editing]); const handleDragEnd = (event: DragEndEvent) => { setEditing(true); setLocal(handleTrueFalseReorder(event, local)); } return (
{alerts.length > 0 && } updateLocal({ ...local, prompt: text })} />
q.id)} handleDragEnd={handleDragEnd} > {local.questions.map((question, index) => ( <> updateQuestion(index, 'prompt', e.target.value)} className="w-full p-3 border rounded-lg focus:ring-2 focus:ring-blue-500 focus:outline-none" placeholder="Enter question..." />
{['true', 'false', 'not_given'].map((value) => ( ))}
))}
); }; export default TrueFalse;