From 4f32e3cf93871f73ab144c8e76ad5b0e897b6440 Mon Sep 17 00:00:00 2001 From: Carlos-Mesquita Date: Thu, 5 Dec 2024 18:36:04 +0000 Subject: [PATCH 1/2] ENCOA-257 Fixed FillBlanks MC --- .../Blanks/MultipleChoice/MCOption.tsx | 12 --- .../Exercises/Blanks/MultipleChoice/index.tsx | 73 ++++++++++--------- 2 files changed, 37 insertions(+), 48 deletions(-) diff --git a/src/components/ExamEditor/Exercises/Blanks/MultipleChoice/MCOption.tsx b/src/components/ExamEditor/Exercises/Blanks/MultipleChoice/MCOption.tsx index 6b5595c0..d3f4683f 100644 --- a/src/components/ExamEditor/Exercises/Blanks/MultipleChoice/MCOption.tsx +++ b/src/components/ExamEditor/Exercises/Blanks/MultipleChoice/MCOption.tsx @@ -1,5 +1,3 @@ - -import { MdDelete, } from "react-icons/md"; import clsx from "clsx"; interface MCOptionProps { @@ -24,7 +22,6 @@ const MCOption: React.FC = ({ onSelect, isEditMode, onEdit, - onRemove }) => { const optionKeys = ['A', 'B', 'C', 'D'] as const; @@ -32,15 +29,6 @@ const MCOption: React.FC = ({
Question {id} - {isEditMode && onRemove && ( - - )}
{optionKeys.map((key) => ( diff --git a/src/components/ExamEditor/Exercises/Blanks/MultipleChoice/index.tsx b/src/components/ExamEditor/Exercises/Blanks/MultipleChoice/index.tsx index 826ed848..c7c04250 100644 --- a/src/components/ExamEditor/Exercises/Blanks/MultipleChoice/index.tsx +++ b/src/components/ExamEditor/Exercises/Blanks/MultipleChoice/index.tsx @@ -114,7 +114,7 @@ const FillBlanksMC: React.FC<{ exercise: FillBlanksExercise; sectionId: number } newState.exercises = newState.exercises.map((ex) => ex.id === exercise.id ? updatedExercise : ex ); - setLocal((prev) => ({...prev, isPractice: !local.isPractice})) + setLocal((prev) => ({ ...prev, isPractice: !local.isPractice })) dispatch({ type: 'UPDATE_SECTION_STATE', payload: { sectionId, update: newState, module: currentModule } }); } }); @@ -174,37 +174,6 @@ const FillBlanksMC: React.FC<{ exercise: FillBlanksExercise; sectionId: number } }); }; - const handleRemoveOption = (index: number) => { - if (!editing) setEditing(true); - - if (answers.size === 1) { - toast.error("There needs to be at least 1 question!"); - return; - } - - setLocal(prev => { - const newWords = prev.words.filter((_, i) => i !== index) as FillBlanksMCOption[]; - const removedOption = prev.words[index] as FillBlanksMCOption; - const removedValues = Object.values(removedOption.options); - const newAnswers = new Map(answers); - for (const [blankId, answer] of newAnswers.entries()) { - if (removedValues.includes(answer)) { - newAnswers.delete(blankId); - } - } - setAnswers(newAnswers); - - return { - ...prev, - words: newWords, - solutions: Array.from(newAnswers.entries()).map(([id, solution]) => ({ - id, - solution - })) - }; - }); - }; - useEffect(() => { validateBlanks(blanksState.blanks, answers, alerts, setAlerts); // eslint-disable-next-line react-hooks/exhaustive-deps @@ -255,6 +224,41 @@ const FillBlanksMC: React.FC<{ exercise: FillBlanksExercise; sectionId: number } blanksDispatcher({ type: "REMOVE_BLANK", payload: blankId }); }; + useEffect(() => { + const existingWordIds = new Set((local.words as FillBlanksMCOption[]).map(word => word.id)); + const blanksMissingWords = blanksState.blanks.filter(blank => !existingWordIds.has(blank.id.toString())); + if (blanksMissingWords.length > 0) { + setLocal(prev => { + const newWords = [...prev.words] as FillBlanksMCOption[]; + + blanksMissingWords.forEach(blank => { + const newMCOption: FillBlanksMCOption = { + id: blank.id.toString(), + options: { + A: 'Option A', + B: 'Option B', + C: 'Option C', + D: 'Option D' + } + }; + newWords.push(newMCOption); + }); + + return { + ...prev, + words: newWords, + solutions: Array.from(answers.entries()).map(([id, solution]) => ({ + id, + solution + })) + }; + }); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [blanksState.blanks]); + + console.log(local); + return (
updateLocal({...local, prompt})} + updatePrompt={(prompt: string) => updateLocal({ ...local, prompt })} > {!blanksState.textMode && selectedBlankId && ( @@ -309,9 +313,6 @@ const FillBlanksMC: React.FC<{ exercise: FillBlanksExercise; sectionId: number } key as "A" | "B" | "C" | "D", value )} - onRemove={() => handleRemoveOption( - (local.words as FillBlanksMCOption[]).findIndex(w => w.id === mcOption.id) - )} /> ); })} From 35d28fbff60ac3091ac975ee28c443f67fbb32e2 Mon Sep 17 00:00:00 2001 From: Carlos-Mesquita Date: Thu, 5 Dec 2024 18:40:59 +0000 Subject: [PATCH 2/2] Missed a .log --- .../ExamEditor/Exercises/Blanks/MultipleChoice/index.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/components/ExamEditor/Exercises/Blanks/MultipleChoice/index.tsx b/src/components/ExamEditor/Exercises/Blanks/MultipleChoice/index.tsx index c7c04250..a1bb2e40 100644 --- a/src/components/ExamEditor/Exercises/Blanks/MultipleChoice/index.tsx +++ b/src/components/ExamEditor/Exercises/Blanks/MultipleChoice/index.tsx @@ -257,8 +257,6 @@ const FillBlanksMC: React.FC<{ exercise: FillBlanksExercise; sectionId: number } // eslint-disable-next-line react-hooks/exhaustive-deps }, [blanksState.blanks]); - console.log(local); - return (