diff --git a/src/components/ExamEditor/ImportExam/ImportOrFromScratch.tsx b/src/components/ExamEditor/ImportExam/ImportOrFromScratch.tsx index 66546757..2acee3b0 100644 --- a/src/components/ExamEditor/ImportExam/ImportOrFromScratch.tsx +++ b/src/components/ExamEditor/ImportExam/ImportOrFromScratch.tsx @@ -8,7 +8,7 @@ import useExamEditorStore from '@/stores/examEditor'; const ImportOrFromScratch: React.FC<{ module: Module; - setNumberOfLevelParts: React.Dispatch> + setNumberOfLevelParts: (parts: number) => void; }> = ({ module, setNumberOfLevelParts }) => { const { currentModule, dispatch } = useExamEditorStore(); const { importing } = useExamEditorStore((store) => store.modules[currentModule]) diff --git a/src/components/ExamEditor/ImportExam/WordUploader.tsx b/src/components/ExamEditor/ImportExam/WordUploader.tsx index cd7da5a7..d1465a35 100644 --- a/src/components/ExamEditor/ImportExam/WordUploader.tsx +++ b/src/components/ExamEditor/ImportExam/WordUploader.tsx @@ -9,9 +9,8 @@ import useExamEditorStore from '@/stores/examEditor'; import { LevelPart, ListeningPart, ReadingPart } from '@/interfaces/exam'; import { defaultSectionSettings } from '@/stores/examEditor/defaults'; -const WordUploader: React.FC<{ module: Module, setNumberOfLevelParts: React.Dispatch> }> = ({ module, setNumberOfLevelParts }) => { +const WordUploader: React.FC<{ module: Module, setNumberOfLevelParts: (parts: number) => void; }> = ({ module, setNumberOfLevelParts }) => { const { currentModule, dispatch } = useExamEditorStore(); - const {sectionLabels} = useExamEditorStore(state => state.modules[currentModule]); const examInputRef = useRef(null); const solutionsInputRef = useRef(null); @@ -77,16 +76,7 @@ const WordUploader: React.FC<{ module: Module, setNumberOfLevelParts: React.Disp ); if (module === "level") { - // default is 1 - const newLabelCount = data.parts.length - 2; - setNumberOfLevelParts(newLabelCount); - - const newLabels = Array.from({ length: newLabelCount }, (_, index) => ({ - id: index + 2, - label: `Part ${index + 2}` - })); - - dispatch({type: "UPDATE_MODULE", payload: { updates: { sectionLabels: [...sectionLabels, ...newLabels] }}}) + setNumberOfLevelParts(data.parts.length); } dispatch({ diff --git a/src/components/ExamEditor/SectionRenderer/SectionContext/level.tsx b/src/components/ExamEditor/SectionRenderer/SectionContext/level.tsx index eab90560..2a163b3b 100644 --- a/src/components/ExamEditor/SectionRenderer/SectionContext/level.tsx +++ b/src/components/ExamEditor/SectionRenderer/SectionContext/level.tsx @@ -10,20 +10,28 @@ interface Props { const LevelContext: React.FC = ({ sectionId }) => { const { currentModule } = useExamEditorStore(); - const { generating, readingSection, listeningSection } = useExamEditorStore( + const { generating, readingSection, listeningSection, state } = useExamEditorStore( (state) => state.modules[currentModule].sections.find((section) => section.sectionId === sectionId)! ); + const hasReadingContext = + 'text' in state && + state.text !== undefined && + typeof state.text === 'object' && + 'content' in state.text && + state.text.content !== undefined && + state.text.content !== ""; + return ( <> {generating && ( (generating === "passage" && ) || (generating === "listeningScript" && ) )} - {(readingSection || listeningSection) && ( + {(readingSection || listeningSection || hasReadingContext) && (
- {readingSection && } - {listeningSection && } + {readingSection || hasReadingContext && } + {listeningSection && }
)} diff --git a/src/components/ExamEditor/index.tsx b/src/components/ExamEditor/index.tsx index 1befa857..abf7e54f 100644 --- a/src/components/ExamEditor/index.tsx +++ b/src/components/ExamEditor/index.tsx @@ -35,6 +35,7 @@ const ExamEditor: React.FC<{ levelParts?: number }> = ({ levelParts = 0 }) => { const [numberOfLevelParts, setNumberOfLevelParts] = useState(levelParts !== 0 ? levelParts : 1); + // For exam edits useEffect(() => { if (levelParts !== 0) { setNumberOfLevelParts(levelParts); @@ -59,13 +60,11 @@ const ExamEditor: React.FC<{ levelParts?: number }> = ({ levelParts = 0 }) => { const currentLabels = sectionLabels; let updatedSections: SectionState[]; let updatedLabels: any; - - if (numberOfLevelParts > currentSections.length) { + if (currentModule === "level" && currentSections.length !== currentLabels.length || numberOfLevelParts !== currentSections.length) { const newSections = [...currentSections]; const newLabels = [...currentLabels]; - - for (let i = currentSections.length; i < numberOfLevelParts; i++) { - newSections.push(defaultSectionSettings(currentModule, i + 1)); + for (let i = currentLabels.length; i < numberOfLevelParts; i++) { + if (currentSections.length !== numberOfLevelParts) newSections.push(defaultSectionSettings(currentModule, i + 1)); newLabels.push({ id: i + 1, label: `Part ${i + 1}` @@ -97,7 +96,6 @@ const ExamEditor: React.FC<{ levelParts?: number }> = ({ levelParts = 0 }) => { // eslint-disable-next-line react-hooks/exhaustive-deps }, [numberOfLevelParts]); - const sectionIds = sections.map((section) => section.sectionId) const updateModule = useCallback((updates: Partial) => { @@ -123,9 +121,13 @@ const ExamEditor: React.FC<{ levelParts?: number }> = ({ levelParts = 0 }) => { const Settings = ModuleSettings[currentModule]; const showImport = importModule && ["reading", "listening", "level"].includes(currentModule); + const updateLevelParts = (parts: number) => { + setNumberOfLevelParts(parts); + } + return ( <> - {showImport ? : ( + {showImport ? : ( <>
diff --git a/src/stores/examEditor/defaults.ts b/src/stores/examEditor/defaults.ts index 5a2c902b..d1ea2bd6 100644 --- a/src/stores/examEditor/defaults.ts +++ b/src/stores/examEditor/defaults.ts @@ -94,7 +94,13 @@ export const sectionLabels = (module: Module, levelParts?: number) => { label: `Section ${index + 1}` })); case 'level': - return [{ id: 1, label: "Part 1" }]; + return levelParts !== undefined ? + Array.from({ length: levelParts }, (_, index) => ({ + id: index + 1, + label: `Part ${index + 1}` + })) + : + [{ id: 1, label: "Part 1" }]; } }