Blanks Exercises were removing the blanks on the editor and text but not on solutions, added submit and preview to writing and reading

This commit is contained in:
Carlos-Mesquita
2024-11-05 17:53:55 +00:00
parent 15c9c4d4bd
commit ffa2045a2d
28 changed files with 519 additions and 264 deletions

View File

@@ -9,8 +9,10 @@ export const rootReducer = (
state: ExamEditorStore,
action: Action
): Partial<ExamEditorStore> => {
console.log(action.type);
if (MODULE_ACTIONS.includes(action.type as any)) {
if (action.type === "REORDER_EXERCISES" && "payload" in action && "event" in action.payload) {
if (action.type === "REORDER_EXERCISES") {
const updatedState = sectionReducer(state, action as SectionActions);
if (!updatedState.modules) return state;
@@ -26,7 +28,7 @@ export const rootReducer = (
if (SECTION_ACTIONS.includes(action.type as any)) {
if (action.type === "UPDATE_SECTION_STATE") {
const updatedState = sectionReducer(state, action as SectionActions);
if (!updatedState.modules) return state;
if (!updatedState.modules) return state;
return moduleReducer({
...state,

View File

@@ -1,6 +1,6 @@
import { Module } from "@/interfaces";
import { defaultSectionSettings } from "../defaults";
import { reorderExercises } from "../reorder/global";
import { reorderModule } from "../reorder/global";
import ExamEditorStore, { ModuleState } from "../types";
export type ModuleActions =
@@ -97,7 +97,7 @@ export const moduleReducer = (
...state,
modules: {
...state.modules,
[currentModule]: reorderExercises(currentModuleState)
[currentModule]: reorderModule(currentModuleState)
}
};
}

View File

@@ -2,6 +2,7 @@ import { Module } from "@/interfaces";
import ExamEditorStore, { Generating, ReadingSectionSettings, Section, SectionSettings, SectionState } from "../types";
import { DragEndEvent } from "@dnd-kit/core";
import { LevelPart, ListeningPart, ReadingPart } from "@/interfaces/exam";
import { reorderSection } from "../reorder/global";
export type SectionActions =
| { type: 'UPDATE_SECTION_SINGLE_FIELD'; payload: { module: Module; sectionId: number; field: string; value: any } }
@@ -80,7 +81,7 @@ export const sectionReducer = (
...modules[currentModule],
sections: sections.map(section =>
section.sectionId === sectionId
? { ...section, state: {...section.state, ...updatedState} }
? { ...section, state: { ...section.state, ...updatedState } }
: section
)
}
@@ -93,10 +94,18 @@ export const sectionReducer = (
const oldIndex = active.id as number;
const newIndex = over.id as number;
const currentSectionState = sections.find((s) => s.sectionId = sectionId)!.state as ReadingPart | ListeningPart | LevelPart;
const currentSectionState = sections.find((s) => s.sectionId === sectionId)!.state as ReadingPart | ListeningPart | LevelPart;
const exercises = [...currentSectionState.exercises];
const [removed] = exercises.splice(oldIndex, 1);
exercises.splice(newIndex, 0, removed);
const { exercises: reorderedExercises } = reorderSection(exercises, 1);
const newSectionState = {
...currentSectionState,
exercises: reorderedExercises
};
const [removed] = currentSectionState.exercises.splice(oldIndex, 1);
currentSectionState.exercises.splice(newIndex, 0, removed);
return {
...state,
modules: {
@@ -105,7 +114,7 @@ export const sectionReducer = (
...modules[currentModule],
sections: sections.map(section =>
section.sectionId === sectionId
? { ...section, state: currentSectionState }
? { ...section, state: newSectionState }
: section
)
}

View File

@@ -1,59 +1,9 @@
import { FillBlanksExercise, LevelPart, ListeningPart, MatchSentencesExercise, MultipleChoiceExercise, ReadingPart, TrueFalseExercise, WriteBlanksExercise } from "@/interfaces/exam";
import { Exercise, FillBlanksExercise, LevelPart, ListeningPart, MatchSentencesExercise, MultipleChoiceExercise, ReadingPart, TrueFalseExercise, WriteBlanksExercise } from "@/interfaces/exam";
import { ModuleState } from "../types";
import ReorderResult from "./types";
const reorderFillBlanks = (exercise: FillBlanksExercise, startId: number): ReorderResult<FillBlanksExercise> => {
const newSolutions = exercise.solutions
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
.map((solution, index) => ({
...solution,
id: (startId + index).toString()
}));
const idMapping = exercise.solutions
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
.reduce((acc, solution, index) => {
acc[solution.id] = (startId + index).toString();
return acc;
}, {} as Record<string, string>);
let newText = exercise.text;
Object.entries(idMapping).forEach(([oldId, newId]) => {
const regex = new RegExp(`\\{\\{${oldId}\\}\\}`, 'g');
newText = newText.replace(regex, `{{${newId}}}`);
});
const newWords = exercise.words.map(word => {
if (typeof word === 'string') {
return word;
} else if ('letter' in word && 'word' in word) {
return word;
} else if ('options' in word) {
return word;
}
return word;
});
const newUserSolutions = exercise.userSolutions?.map(solution => ({
...solution,
id: idMapping[solution.id] || solution.id
}));
return {
exercise: {
...exercise,
solutions: newSolutions,
text: newText,
words: newWords,
userSolutions: newUserSolutions
},
lastId: startId + newSolutions.length - 1
};
};
const reorderWriteBlanks = (exercise: WriteBlanksExercise, startId: number): ReorderResult<WriteBlanksExercise> => {
console.log();
const newSolutions = exercise.solutions
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
.map((solution, index) => ({
@@ -61,21 +11,78 @@ const reorderWriteBlanks = (exercise: WriteBlanksExercise, startId: number): Reo
id: (startId + index).toString()
}));
const idMapping = exercise.solutions
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
.reduce((acc, solution, index) => {
acc[solution.id] = (startId + index).toString();
return acc;
}, {} as Record<string, string>);
let newText = exercise.text;
Object.entries(idMapping).forEach(([oldId, newId]) => {
const regex = new RegExp(`\\{\\{${oldId}\\}\\}`, 'g');
newText = newText.replace(regex, `{{${newId}}}`);
});
const newWords = exercise.words.map(word => {
if (typeof word === 'string') {
return word;
} else if ('letter' in word && 'word' in word) {
return word;
} else if ('options' in word) {
return word;
}
return word;
});
const newUserSolutions = exercise.userSolutions?.map(solution => ({
...solution,
id: idMapping[solution.id] || solution.id
}));
return {
exercise: {
...exercise,
solutions: newSolutions,
text: newSolutions.reduce((text, solution, index) => {
return text.replace(
new RegExp(`\\{\\{${solution.id}\\}\\}`),
`{{${startId + index}}}`
);
}, exercise.text)
text: newText,
words: newWords,
userSolutions: newUserSolutions
},
lastId: startId + newSolutions.length
};
};
const reorderWriteBlanks = (exercise: WriteBlanksExercise, startId: number): ReorderResult<WriteBlanksExercise> => {
const oldIds = exercise.solutions.map(s => s.id);
const newIds = oldIds.map((_, index) => (startId + index).toString());
const newSolutions = exercise.solutions.map((solution, index) => ({
id: newIds[index],
solution: [...solution.solution]
}));
let newText = exercise.text;
oldIds.forEach((oldId, index) => {
newText = newText.replace(
`{{${oldId}}}`,
`{{${newIds[index]}}}`
);
});
const result = {
exercise: {
...exercise,
solutions: newSolutions,
text: newText
},
lastId: startId + newSolutions.length
};
return result;
};
const reorderTrueFalse = (exercise: TrueFalseExercise, startId: number): ReorderResult<TrueFalseExercise> => {
const newQuestions = exercise.questions
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
@@ -129,60 +136,79 @@ const reorderMultipleChoice = (exercise: MultipleChoiceExercise, startId: number
};
const reorderSection = (exercises: Exercise[], startId: number): { exercises: Exercise[], lastId: number } => {
let currentId = startId;
const reorderedExercises = exercises.map(exercise => {
let result;
switch (exercise.type) {
case 'fillBlanks':
console.log("Reordering FillBlanks");
result = reorderFillBlanks(exercise, currentId);
currentId = result.lastId;
return result.exercise;
const reorderExercises = (moduleState: ModuleState) => {
let currentId = 1;
case 'writeBlanks':
result = reorderWriteBlanks(exercise, currentId);
currentId = result.lastId;
return result.exercise;
const reorderedSections = moduleState.sections.map(section => {
const currentSection = section.state as ReadingPart | ListeningPart |LevelPart;
const reorderedExercises = currentSection.exercises.map(exercise => {
let result;
switch (exercise.type) {
case 'fillBlanks':
result = reorderFillBlanks(exercise, currentId);
currentId = result.lastId;
return result.exercise;
case 'writeBlanks':
result = reorderWriteBlanks(exercise, currentId);
currentId = result.lastId;
return result.exercise;
case 'trueFalse':
result = reorderTrueFalse(exercise, currentId);
currentId = result.lastId;
return result.exercise;
case 'matchSentences':
result = reorderMatchSentences(exercise, currentId);
currentId = result.lastId;
return result.exercise;
case 'multipleChoice':
result = reorderMultipleChoice(exercise, currentId);
currentId = result.lastId
return result.exercise;
default:
return exercise;
}
});
return {
...section,
state: {
...currentSection,
exercises: reorderedExercises
}
};
case 'trueFalse':
result = reorderTrueFalse(exercise, currentId);
currentId = result.lastId;
return result.exercise;
case 'matchSentences':
result = reorderMatchSentences(exercise, currentId);
currentId = result.lastId;
return result.exercise;
case 'multipleChoice':
result = reorderMultipleChoice(exercise, currentId);
currentId = result.lastId;
return result.exercise;
default:
return exercise;
}
});
return {
...moduleState,
sections: reorderedSections
exercises: reorderedExercises,
lastId: currentId
};
};
const reorderModule = (moduleState: ModuleState) => {
let currentId = 1;
const reorderedSections = moduleState.sections.map(section => {
const currentSection = section.state as ReadingPart | ListeningPart | LevelPart;
console.log(currentSection.exercises);
const result = reorderSection(currentSection.exercises, currentId);
currentId = result.lastId;
console.log(result);
return {
...section,
state: {
...currentSection,
exercises: result.exercises
}
};
});
return {
...moduleState,
sections: reorderedSections
};
};
export {
reorderFillBlanks,
reorderWriteBlanks,
reorderTrueFalse,
reorderMatchSentences,
reorderExercises,
reorderSection,
reorderModule,
reorderMultipleChoice,
};