Exam generation rework, batch user tables, fastapi endpoint switch
This commit is contained in:
118
src/stores/examEditor/reducers/sectionReducer.ts
Normal file
118
src/stores/examEditor/reducers/sectionReducer.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
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";
|
||||
|
||||
export type SectionActions =
|
||||
| { type: 'UPDATE_SECTION_SINGLE_FIELD'; payload: { module: Module; sectionId: number; field: string; value: any } }
|
||||
| { type: 'UPDATE_SECTION_SETTINGS'; payload: { sectionId: number; update: Partial<SectionSettings | ReadingSectionSettings>; } }
|
||||
| { type: 'UPDATE_SECTION_STATE'; payload: { sectionId: number; update: Partial<Section>; } }
|
||||
| { type: 'REORDER_EXERCISES'; payload: { event: DragEndEvent, sectionId: number; } };
|
||||
|
||||
export const SECTION_ACTIONS = [
|
||||
'UPDATE_SECTION_SETTINGS',
|
||||
'UPDATE_SECTION_STATE',
|
||||
'UPDATE_SECTION_SINGLE_FIELD',
|
||||
];
|
||||
|
||||
export const sectionReducer = (
|
||||
state: ExamEditorStore,
|
||||
action: SectionActions
|
||||
): Partial<ExamEditorStore> => {
|
||||
const currentModule = state.currentModule;
|
||||
const modules = state.modules;
|
||||
const sections = state.modules[currentModule].sections;
|
||||
let sectionId: number;
|
||||
|
||||
if (action.payload && 'sectionId' in action.payload) {
|
||||
sectionId = action.payload.sectionId;
|
||||
}
|
||||
|
||||
switch (action.type) {
|
||||
case 'UPDATE_SECTION_SINGLE_FIELD':
|
||||
const { module, field, value } = action.payload;
|
||||
return {
|
||||
modules: {
|
||||
...modules,
|
||||
[module]: {
|
||||
...modules[module],
|
||||
sections: sections.map((section: SectionState) =>
|
||||
section.sectionId === sectionId
|
||||
? {
|
||||
...section,
|
||||
[field]: value
|
||||
}
|
||||
: section
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
case 'UPDATE_SECTION_SETTINGS':
|
||||
let updatedSettings = action.payload.update;
|
||||
return {
|
||||
modules: {
|
||||
...modules,
|
||||
[currentModule]: {
|
||||
...modules[currentModule],
|
||||
sections: sections.map((section: SectionState) =>
|
||||
section.sectionId === sectionId
|
||||
? {
|
||||
...section,
|
||||
settings: {
|
||||
...section.settings,
|
||||
...updatedSettings
|
||||
}
|
||||
}
|
||||
: section
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
case 'UPDATE_SECTION_STATE':
|
||||
const updatedState = action.payload.update;
|
||||
return {
|
||||
modules: {
|
||||
...modules,
|
||||
[currentModule]: {
|
||||
...modules[currentModule],
|
||||
sections: sections.map(section =>
|
||||
section.sectionId === sectionId
|
||||
? { ...section, state: {...section.state, ...updatedState} }
|
||||
: section
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
case 'REORDER_EXERCISES': {
|
||||
const { active, over } = action.payload.event;
|
||||
if (!over) return state;
|
||||
|
||||
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 [removed] = currentSectionState.exercises.splice(oldIndex, 1);
|
||||
currentSectionState.exercises.splice(newIndex, 0, removed);
|
||||
return {
|
||||
...state,
|
||||
modules: {
|
||||
...state.modules,
|
||||
[currentModule]: {
|
||||
...modules[currentModule],
|
||||
sections: sections.map(section =>
|
||||
section.sectionId === sectionId
|
||||
? { ...section, state: currentSectionState }
|
||||
: section
|
||||
)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user