From 0739e044a1c6b03f9aed93d855a475be3c7c7818 Mon Sep 17 00:00:00 2001 From: Carlos-Mesquita Date: Sun, 5 Jan 2025 17:46:11 +0000 Subject: [PATCH] ENCOA-307 --- .../SectionRenderer/SectionContext/level.tsx | 3 +- .../SectionContext/reading.tsx | 9 +- .../SectionExercises/index.tsx | 244 +++++++++--------- .../SettingsEditor/Shared/SectionPicker.tsx | 6 +- .../ExamEditor/SettingsEditor/level.tsx | 2 - 5 files changed, 135 insertions(+), 129 deletions(-) diff --git a/src/components/ExamEditor/SectionRenderer/SectionContext/level.tsx b/src/components/ExamEditor/SectionRenderer/SectionContext/level.tsx index 2a163b3b..abc98e05 100644 --- a/src/components/ExamEditor/SectionRenderer/SectionContext/level.tsx +++ b/src/components/ExamEditor/SectionRenderer/SectionContext/level.tsx @@ -1,4 +1,3 @@ - import useExamEditorStore from "@/stores/examEditor"; import ListeningContext from "./listening"; import ReadingContext from "./reading"; @@ -30,7 +29,7 @@ const LevelContext: React.FC = ({ sectionId }) => { )} {(readingSection || listeningSection || hasReadingContext) && (
- {readingSection || hasReadingContext && } + {(readingSection !== undefined || hasReadingContext) && } {listeningSection && }
)} diff --git a/src/components/ExamEditor/SectionRenderer/SectionContext/reading.tsx b/src/components/ExamEditor/SectionRenderer/SectionContext/reading.tsx index 424bac59..b36d2b03 100644 --- a/src/components/ExamEditor/SectionRenderer/SectionContext/reading.tsx +++ b/src/components/ExamEditor/SectionRenderer/SectionContext/reading.tsx @@ -11,11 +11,10 @@ import { Module } from "@/interfaces"; interface Props { module: Module; sectionId: number; - level?: boolean; } -const ReadingContext: React.FC = ({ sectionId, module, level = false }) => { +const ReadingContext: React.FC = ({ sectionId, module }) => { const { dispatch } = useExamEditorStore(); const sectionState = useExamEditorStore( (state) => state.modules[module].sections.find((section) => section.sectionId === sectionId)! @@ -55,7 +54,7 @@ const ReadingContext: React.FC = ({ sectionId, module, level = false }) = } }); - useEffect(()=> { + useEffect(() => { if (readingPart.text === undefined) { setTitle(''); setContent(''); @@ -74,7 +73,9 @@ const ReadingContext: React.FC = ({ sectionId, module, level = false }) = useEffect(() => { - const passageRes = levelGenResults.find((res) => res.generating === "passage"); + const passageRes = [...levelGenResults].reverse() + .find((res) => res.generating === "passage"); + if (levelGenResults && passageRes) { setEditing(true); setTitle(passageRes.result[0].title); diff --git a/src/components/ExamEditor/SectionRenderer/SectionExercises/index.tsx b/src/components/ExamEditor/SectionRenderer/SectionExercises/index.tsx index 6b4e4e2f..c6dc6f03 100644 --- a/src/components/ExamEditor/SectionRenderer/SectionExercises/index.tsx +++ b/src/components/ExamEditor/SectionRenderer/SectionExercises/index.tsx @@ -6,7 +6,7 @@ import Dropdown from "@/components/Dropdown"; import useExamEditorStore from "@/stores/examEditor"; import Writing from "../../Exercises/Writing"; import Speaking from "../../Exercises/Speaking"; -import { ReactElement, ReactNode, useEffect, useState } from "react"; +import { ReactNode, useEffect } from "react"; import { DndContext, PointerSensor, @@ -43,7 +43,7 @@ const SectionExercises: React.FC<{ sectionId: number; }> = ({ sectionId }) => { const genResult = section?.genResult; const generating = section?.generating; - const levelGenResults = section?.levelGenResults + const levelGenResults = section?.levelGenResults; const levelGenerating = section?.levelGenerating; const sectionState = section?.state; @@ -66,134 +66,146 @@ const SectionExercises: React.FC<{ sectionId: number; }> = ({ sectionId }) => { }, [genResult, dispatch, sectionId, currentModule]); - useEffect(() => { - if (levelGenResults && levelGenResults.some(res => res.generating.startsWith("exercises"))) { - const newExercises = levelGenResults - .filter(res => res.generating.startsWith("exercises")) - .map(res => res.result[0].exercises) - .flat(); - - const updates = [ - { - type: "UPDATE_SECTION_STATE", - payload: { - sectionId, - module: "level", - update: { - exercises: [...(sectionState as ExamPart).exercises, ...newExercises] - } - } - }, - { - type: "UPDATE_SECTION_SINGLE_FIELD", - payload: { - sectionId, - module: currentModule, - field: "levelGenerating", - value: levelGenerating?.filter(g => !g?.startsWith("exercises")) - } - }, - { - type: "UPDATE_SECTION_SINGLE_FIELD", - payload: { - sectionId, - module: currentModule, - field: "levelGenResults", - value: levelGenResults.filter(res => !res.generating.startsWith("exercises")) + const handleExerciseGen = ( + results: any[], + assignExercisesFn: (results: any[]) => any[], + { + sectionId, + currentModule, + sectionState, + levelGenerating, + levelGenResults + }: { + sectionId: number; + currentModule: string; + sectionState: ExamPart; + levelGenerating?: Generating[]; + levelGenResults: any[]; + } + ) => { + const nonWritingOrSpeaking = results[0]?.generating.startsWith("exercises"); + + const updates = [ + { + type: "UPDATE_SECTION_STATE", + payload: { + sectionId, + module: "level", + update: { + exercises: [ + ...sectionState.exercises, + ...assignExercisesFn(results) + ] } } - ] as Action[]; + }, + { + type: "UPDATE_SECTION_SINGLE_FIELD", + payload: { + sectionId, + module: currentModule, + field: "levelGenerating", + value: levelGenerating?.filter(g => + nonWritingOrSpeaking + ? !g?.startsWith("exercises") + : !results.flatMap(res => res.generating as Generating).includes(g) + ) + } + }, + { + type: "UPDATE_SECTION_SINGLE_FIELD", + payload: { + sectionId, + module: currentModule, + field: "levelGenResults", + value: levelGenResults.filter(res => + nonWritingOrSpeaking + ? !res.generating.startsWith("exercises") + : !results.flatMap(res => res.generating as Generating).includes(res.generating) + ) + } + } + ] as Action[]; + + updates.forEach(update => dispatch(update)); + }; - updates.forEach(update => dispatch(update)); + useEffect(() => { + if (levelGenResults && levelGenResults?.some(res => res.generating.startsWith("exercises"))) { + const results = levelGenResults.filter(res => + res.generating.startsWith("exercises") + ); + + const assignExercises = (results: any[]) => + results + .map(res => res.result[0].exercises) + .flat(); + + handleExerciseGen( + results, + assignExercises, + { + sectionId, + currentModule, + sectionState: sectionState as ExamPart, + levelGenerating, + levelGenResults + } + ); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [levelGenResults, sectionState, levelGenerating, sectionId, currentModule]); useEffect(() => { - if (levelGenResults && levelGenResults.some(res => res.generating === "writing_letter" || res.generating === "writing_2")) { - const results = levelGenResults.filter(res => res.generating === "writing_letter" || res.generating === "writing_2"); + if (levelGenResults && levelGenResults?.some(res => + res.generating === "writing_letter" || res.generating === "writing_2" + )) { + const results = levelGenResults.filter(res => + res.generating === "writing_letter" || res.generating === "writing_2" + ); - const updates = [ + const assignExercises = (results: any[]) => + results.map(res => ({ + ...writingTask(res.generating === "writing_letter" ? 1 : 2), + prompt: res.result[0].prompt, + variant: res.generating === "writing_letter" ? "letter" : "essay" + }) as WritingExercise); + + handleExerciseGen( + results, + assignExercises, { - type: "UPDATE_SECTION_STATE", - payload: { - sectionId, - module: "level", - update: { - exercises: [...(sectionState as ExamPart).exercises, - ...results.map((res) => { - return { - ...writingTask(res.generating === "writing_letter" ? 1 : 2), - prompt: res.result[0].prompt, - variant: res.generating === "writing_letter" ? "letter" : "essay" - } as WritingExercise; - }) - ] - } - } - }, - { - type: "UPDATE_SECTION_SINGLE_FIELD", - payload: { - sectionId, - module: currentModule, - field: "levelGenerating", - value: levelGenerating?.filter(g => !results.flatMap(res => res.generating as Generating).includes(g)) - } - }, - { - type: "UPDATE_SECTION_SINGLE_FIELD", - payload: { - sectionId, - module: currentModule, - field: "levelGenResults", - value: levelGenResults.filter(res => !results.flatMap(res => res.generating as Generating).includes(res.generating)) - } + sectionId, + currentModule, + sectionState: sectionState as ExamPart, + levelGenerating, + levelGenResults } - ] as Action[]; - - updates.forEach(update => dispatch(update)); + ); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [levelGenResults, sectionState, levelGenerating, sectionId, currentModule]); useEffect(() => { - if (levelGenResults && levelGenResults.some(res => res.generating.startsWith("speaking"))) { - const results = levelGenResults.filter(res => res.generating.startsWith("speaking")); - const updates = [ - { - type: "UPDATE_SECTION_STATE", - payload: { - sectionId, - module: "level", - update: { - exercises: [...(sectionState as ExamPart).exercises, - ...results.map(createSpeakingExercise) - ] - } - } - }, - { - type: "UPDATE_SECTION_SINGLE_FIELD", - payload: { - sectionId, - module: currentModule, - field: "levelGenerating", - value: levelGenerating?.filter(g => !results.flatMap(res => res.generating as Generating).includes(g)) - } - }, - { - type: "UPDATE_SECTION_SINGLE_FIELD", - payload: { - sectionId, - module: currentModule, - field: "levelGenResults", - value: levelGenResults.filter(res => !results.flatMap(res => res.generating as Generating).includes(res.generating)) - } - } - ] as Action[]; + if (levelGenResults && levelGenResults?.some(res => res.generating.startsWith("speaking"))) { + const results = levelGenResults.filter(res => + res.generating.startsWith("speaking") + ); - updates.forEach(update => dispatch(update)); + const assignExercises = (results: any[]) => + results.map(createSpeakingExercise); + + handleExerciseGen( + results, + assignExercises, + { + sectionId, + currentModule, + sectionState: sectionState as ExamPart, + levelGenerating, + levelGenResults + } + ); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [levelGenResults, sectionState, levelGenerating, sectionId, currentModule]); @@ -243,7 +255,7 @@ const SectionExercises: React.FC<{ sectionId: number; }> = ({ sectionId }) => { const onFocus = (questionId: string, id: string | undefined) => { if (id) { - dispatch({ type: "UPDATE_SECTION_SINGLE_FIELD", payload: { module: currentModule, sectionId, field: "focusedExercise", value: { questionId, id} } }) + dispatch({ type: "UPDATE_SECTION_SINGLE_FIELD", payload: { module: currentModule, sectionId, field: "focusedExercise", value: { questionId, id } } }) } } @@ -284,9 +296,9 @@ const SectionExercises: React.FC<{ sectionId: number; }> = ({ sectionId }) => { {currentModule === "level" && ( <> { - questions.ids?.length === 0 && !levelGenerating?.some((g) => g?.startsWith("exercises") || g?.startsWith("writing") || g?.startsWith("speaking")) && generating !== "exercises" + questions.ids?.length === 0 && !levelGenerating?.some((g) => g?.startsWith("exercises") || g?.startsWith("writing") || g?.startsWith("speaking")) && generating !== "exercises" && background(Generated exercises will appear here!)} - {levelGenerating?.some((g) => g?.startsWith("exercises") || g?.startsWith("writing") || g?.startsWith("speaking")) && } + {levelGenerating?.some((g) => g?.startsWith("exercises") || g?.startsWith("writing") || g?.startsWith("speaking")) && } ) } diff --git a/src/components/ExamEditor/SettingsEditor/Shared/SectionPicker.tsx b/src/components/ExamEditor/SettingsEditor/Shared/SectionPicker.tsx index 1e32b0b3..d572a661 100644 --- a/src/components/ExamEditor/SettingsEditor/Shared/SectionPicker.tsx +++ b/src/components/ExamEditor/SettingsEditor/Shared/SectionPicker.tsx @@ -38,11 +38,7 @@ const SectionPicker: React.FC = ({ const newValue = currentValue === value ? undefined : value; setSelectedValue(newValue); let update = {}; - if (module == "reading") { - update = { - text: undefined - } - } else { + if (module === "listening") { if (state.audio?.source) { URL.revokeObjectURL(state.audio.source) } diff --git a/src/components/ExamEditor/SettingsEditor/level.tsx b/src/components/ExamEditor/SettingsEditor/level.tsx index 79e9774e..33eb0c87 100644 --- a/src/components/ExamEditor/SettingsEditor/level.tsx +++ b/src/components/ExamEditor/SettingsEditor/level.tsx @@ -15,10 +15,8 @@ import { usePersistentExamStore } from "@/stores/exam"; import openDetachedTab from "@/utils/popout"; import ListeningComponents from "./listening/components"; import ReadingComponents from "./reading/components"; -import WritingComponents from "./writing/components"; import SpeakingComponents from "./speaking/components"; import SectionPicker from "./Shared/SectionPicker"; -import SettingsDropdown from "./Shared/SettingsDropdown"; const LevelSettings: React.FC = () => {