ENCOA-228 Now when user navigates between modules the generation items persist. Reading, listening and writing added to level module

This commit is contained in:
Carlos-Mesquita
2024-11-12 14:17:54 +00:00
parent 696c968ebc
commit fdf411d133
66 changed files with 2546 additions and 1635 deletions

View File

@@ -14,27 +14,51 @@ interface GeneratorConfig {
export function generate(
sectionId: number,
module: Module,
type: "context" | "exercises",
type: Generating,
config: GeneratorConfig,
mapData: (data: any) => Record<string, any>[]
mapData: (data: any) => Record<string, any>[],
levelSectionId?: number,
level: boolean = false
) {
const dispatch = useExamEditorStore.getState().dispatch;
const setGenerating = (sectionId: number, generating: Generating, level: boolean, remove?: boolean) => {
const state = useExamEditorStore.getState();
const dispatch = state.dispatch;
let generatingUpdate;
if (level) {
if (remove) {
generatingUpdate = state.modules["level"].sections.find((s) => s.sectionId === levelSectionId)!.levelGenerating.filter(g => g === generating)
}
else {
generatingUpdate = [...state.modules["level"].sections.find((s) => s.sectionId === levelSectionId)!.levelGenerating, generating];
}
} else {
generatingUpdate = generating;
}
const setGenerating = (sectionId: number, generating: Generating) => {
dispatch({
type: "UPDATE_SECTION_SINGLE_FIELD",
payload: { sectionId, module, field: "generating", value: generating }
payload: { sectionId : sectionId, module: level ? "level" : module, field: level ? "levelGenerating" : "generating", value: generatingUpdate }
});
};
const setGeneratedExercises = (sectionId: number, exercises: Record<string, any>[] | undefined) => {
const setGeneratedResult = (sectionId: number, generating: Generating, result: Record<string, any>[] | undefined, level: boolean) => {
const state = useExamEditorStore.getState();
const dispatch = state.dispatch;
let genResults;
if (level) {
genResults = [...state.modules["level"].sections.find((s) => s.sectionId === levelSectionId)!.levelGenResults, { generating, result, module }];
} else {
genResults = { generating, result, module };
}
dispatch({
type: "UPDATE_SECTION_SINGLE_FIELD",
payload: { sectionId, module, field: "genResult", value: exercises }
payload: { sectionId, module: level ? "level" : module, field: level ? "levelGenResults" : "genResult", value: genResults }
});
};
setGenerating(sectionId, type);
setGenerating(level ? levelSectionId! : sectionId, type, level);
const queryString = config.queryParams
? new URLSearchParams(config.queryParams).toString()
@@ -49,13 +73,11 @@ export function generate(
request
.then((result) => {
playSound("check");
setGeneratedExercises(sectionId, mapData(result.data));
setGeneratedResult(level ? levelSectionId! : sectionId, type, mapData(result.data), level);
})
.catch((error) => {
setGenerating(sectionId, undefined, level, true);
playSound("error");
toast.error("Something went wrong! Try to generate again.");
})
.finally(() => {
setGenerating(sectionId, undefined);
});
}