Added Speaking to level, fixed a bug where it was causing level to crash if the listening was already created and the section was switched, added true false exercises to listening
This commit is contained in:
@@ -8,7 +8,7 @@ import GenLoader from "../Shared/GenLoader";
|
||||
import { useEffect, useState } from "react";
|
||||
import useSectionEdit from "../../Hooks/useSectionEdit";
|
||||
import useExamEditorStore from "@/stores/examEditor";
|
||||
import { InteractiveSpeakingExercise } from "@/interfaces/exam";
|
||||
import { InteractiveSpeakingExercise, LevelPart } from "@/interfaces/exam";
|
||||
import { BsFileText } from "react-icons/bs";
|
||||
import { FaChevronLeft, FaChevronRight } from "react-icons/fa6";
|
||||
import { RiVideoLine } from "react-icons/ri";
|
||||
@@ -23,10 +23,9 @@ interface Props {
|
||||
const InteractiveSpeaking: React.FC<Props> = ({ sectionId, exercise, module = "speaking" }) => {
|
||||
const { dispatch } = useExamEditorStore();
|
||||
const [local, setLocal] = useState(exercise);
|
||||
|
||||
const [currentVideoIndex, setCurrentVideoIndex] = useState(0);
|
||||
|
||||
const { generating, genResult, state } = useExamEditorStore(
|
||||
const { generating, genResult, state, levelGenResults, levelGenerating } = useExamEditorStore(
|
||||
(state) => state.modules[module].sections.find((section) => section.sectionId === sectionId)!
|
||||
);
|
||||
|
||||
@@ -34,48 +33,94 @@ const InteractiveSpeaking: React.FC<Props> = ({ sectionId, exercise, module = "s
|
||||
sectionId,
|
||||
onSave: () => {
|
||||
setEditing(false);
|
||||
dispatch({ type: "UPDATE_SECTION_STATE", payload: { sectionId: sectionId, update: local, module: module } });
|
||||
|
||||
if (module === "level") {
|
||||
const updatedState = {
|
||||
...state,
|
||||
exercises: (state as LevelPart).exercises.map((ex) =>
|
||||
ex.id === local.id ? local : ex
|
||||
)
|
||||
};
|
||||
dispatch({
|
||||
type: "UPDATE_SECTION_STATE",
|
||||
payload: { sectionId, update: updatedState, module }
|
||||
});
|
||||
} else {
|
||||
dispatch({
|
||||
type: "UPDATE_SECTION_STATE",
|
||||
payload: { sectionId, update: local, module }
|
||||
});
|
||||
}
|
||||
|
||||
if (genResult) {
|
||||
dispatch({
|
||||
type: "UPDATE_SECTION_SINGLE_FIELD",
|
||||
payload: {
|
||||
sectionId,
|
||||
module: module,
|
||||
module,
|
||||
field: "genResult",
|
||||
value: undefined
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const speakingScript = levelGenResults?.find((res) => res.generating === `${local.id}-speakingScript`);
|
||||
if (module === "level" && speakingScript) {
|
||||
dispatch({
|
||||
type: "UPDATE_SECTION_SINGLE_FIELD", payload: {
|
||||
sectionId,
|
||||
field: "levelGenResults",
|
||||
value: levelGenResults.filter((res) => res.generating !== `${local.id}-speakingScript`),
|
||||
module
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
onDiscard: () => {
|
||||
setLocal(exercise);
|
||||
},
|
||||
onPractice: () => {
|
||||
const updatedExercise = {
|
||||
...state,
|
||||
isPractice: !local.isPractice
|
||||
};
|
||||
setLocal((prev) => ({...prev, isPractice: !local.isPractice}))
|
||||
dispatch({ type: 'UPDATE_SECTION_STATE', payload: { sectionId, update: updatedExercise, module: module } });
|
||||
const updatedLocal = { ...local, isPractice: !local.isPractice };
|
||||
setLocal(updatedLocal);
|
||||
|
||||
if (module === "level") {
|
||||
const updatedState = {
|
||||
...state,
|
||||
exercises: (state as LevelPart).exercises.map((ex) =>
|
||||
ex.id === local.id ? updatedLocal : ex
|
||||
)
|
||||
};
|
||||
dispatch({
|
||||
type: "UPDATE_SECTION_STATE",
|
||||
payload: { sectionId, update: updatedState, module }
|
||||
});
|
||||
} else {
|
||||
dispatch({
|
||||
type: "UPDATE_SECTION_STATE",
|
||||
payload: { sectionId, update: updatedLocal, module }
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (genResult && generating === "speakingScript") {
|
||||
setEditing(true);
|
||||
setLocal({
|
||||
const updatedLocal = {
|
||||
...local,
|
||||
title: genResult.result[0].title,
|
||||
prompts: genResult.result[0].prompts.map((item: any) => ({
|
||||
text: item || "",
|
||||
video_url: ""
|
||||
}))
|
||||
});
|
||||
};
|
||||
setEditing(true);
|
||||
setLocal(updatedLocal);
|
||||
|
||||
dispatch({
|
||||
type: "UPDATE_SECTION_SINGLE_FIELD",
|
||||
payload: {
|
||||
sectionId,
|
||||
module: module,
|
||||
module,
|
||||
field: "generating",
|
||||
value: undefined
|
||||
}
|
||||
@@ -84,6 +129,90 @@ const InteractiveSpeaking: React.FC<Props> = ({ sectionId, exercise, module = "s
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [genResult, generating]);
|
||||
|
||||
useEffect(() => {
|
||||
if (genResult && generating === "video") {
|
||||
const updatedLocal = { ...local, prompts: genResult.result[0].prompts };
|
||||
setLocal(updatedLocal);
|
||||
|
||||
dispatch({
|
||||
type: "UPDATE_SECTION_STATE",
|
||||
payload: { sectionId, update: updatedLocal, module }
|
||||
});
|
||||
|
||||
|
||||
dispatch({
|
||||
type: "UPDATE_SECTION_SINGLE_FIELD",
|
||||
payload: {
|
||||
sectionId,
|
||||
module,
|
||||
field: "generating",
|
||||
value: undefined
|
||||
}
|
||||
});
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [genResult, generating]);
|
||||
|
||||
useEffect(() => {
|
||||
const speakingScript = levelGenResults?.find((res) => res.generating === `${local.id}-speakingScript`);
|
||||
const isGenerating = levelGenerating?.includes(`${local.id}-speakingScript`);
|
||||
|
||||
if (speakingScript && isGenerating) {
|
||||
const updatedLocal = {
|
||||
...local,
|
||||
title: speakingScript.result[0].title,
|
||||
prompts: speakingScript.result[0].prompts.map((item: any) => ({
|
||||
text: item || "",
|
||||
video_url: ""
|
||||
}))
|
||||
};
|
||||
setEditing(true);
|
||||
setLocal(updatedLocal);
|
||||
|
||||
dispatch({
|
||||
type: "UPDATE_SECTION_SINGLE_FIELD", payload: {
|
||||
sectionId,
|
||||
field: "levelGenerating",
|
||||
value: levelGenerating.filter((g) => g !== `${local.id}-speakingScript`),
|
||||
module
|
||||
}
|
||||
});
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [levelGenResults, levelGenerating]);
|
||||
|
||||
useEffect(() => {
|
||||
const speakingVideo = levelGenResults?.find((res) => res.generating === `${local.id}-video`);
|
||||
const isGenerating = levelGenerating?.includes(`${local.id}-video`);
|
||||
|
||||
if (speakingVideo && isGenerating) {
|
||||
const updatedLocal = { ...local, prompts: speakingVideo.result[0].prompts };
|
||||
setLocal(updatedLocal);
|
||||
|
||||
const updatedState = {
|
||||
...state,
|
||||
exercises: (state as LevelPart).exercises.map((ex) =>
|
||||
ex.id === local.id ? updatedLocal : ex
|
||||
)
|
||||
};
|
||||
|
||||
dispatch({
|
||||
type: "UPDATE_SECTION_STATE",
|
||||
payload: { sectionId, update: updatedState, module }
|
||||
});
|
||||
|
||||
dispatch({
|
||||
type: "UPDATE_SECTION_SINGLE_FIELD", payload: {
|
||||
sectionId,
|
||||
field: "levelGenerating",
|
||||
value: levelGenerating.filter((g) => g !== `${local.id}-video`),
|
||||
module
|
||||
}
|
||||
});
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [levelGenResults, levelGenerating]);
|
||||
|
||||
const addPrompt = () => {
|
||||
setLocal(prev => ({
|
||||
...prev,
|
||||
@@ -150,7 +279,7 @@ const InteractiveSpeaking: React.FC<Props> = ({ sectionId, exercise, module = "s
|
||||
module="speaking"
|
||||
/>
|
||||
</div>
|
||||
{generating && generating === "speakingScript" ? (
|
||||
{(generating && generating === "speakingScript") || (levelGenerating.find((g) => g === `${local.id}-speakingScript`)) ? (
|
||||
<GenLoader module={module} />
|
||||
) : (
|
||||
<>
|
||||
@@ -206,7 +335,7 @@ const InteractiveSpeaking: React.FC<Props> = ({ sectionId, exercise, module = "s
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
{generating && generating === "video" &&
|
||||
{(generating && generating === "video") || levelGenerating.find((g) => g === `${local.id}-video`) &&
|
||||
<GenLoader module={module} custom="Generating the videos ... This may take a while ..." />
|
||||
}
|
||||
<Card>
|
||||
|
||||
Reference in New Issue
Block a user