import { Exercise, InteractiveSpeakingExercise, LevelExam, LevelPart, SpeakingExercise } from "@/interfaces/exam"; import SettingsEditor from "."; import Option from "@/interfaces/option"; import Dropdown from "@/components/Dropdown"; import clsx from "clsx"; import ExercisePicker from "../ExercisePicker"; import useExamEditorStore from "@/stores/examEditor"; import useSettingsState from "../Hooks/useSettingsState"; import { LevelSectionSettings, SectionSettings } from "@/stores/examEditor/types"; import { toast } from "react-toastify"; import axios from "axios"; import { playSound } from "@/utils/sound"; import { useRouter } from "next/router"; import { usePersistentExamStore } from "@/stores/examStore"; 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 = () => { const router = useRouter(); const { setExam, setExerciseIndex, setPartIndex, setQuestionIndex, setBgColor, } = usePersistentExamStore(); const { currentModule, title } = useExamEditorStore(); const { focusedSection, difficulty, sections, minTimer, isPrivate, } = useExamEditorStore(state => state.modules[currentModule]); const { localSettings, updateLocalAndScheduleGlobal } = useSettingsState( currentModule, focusedSection ); const section = sections.find((section) => section.sectionId == focusedSection); const focusedExercise = section?.focusedExercise; if (section === undefined) return <>; const currentSection = section.state as LevelPart; const readingSection = section.readingSection; const listeningSection = section.listeningSection; const canPreview = currentSection.exercises.length > 0; const submitLevel = () => { if (title === "") { toast.error("Enter a title for the exam!"); return; } const exam: LevelExam = { parts: sections.map((s) => { const part = s.state as LevelPart; return { ...part, intro: localSettings.currentIntro, category: localSettings.category }; }), isDiagnostic: false, minTimer, module: "level", id: title, difficulty, private: isPrivate, }; axios.post(`/api/exam/level`, exam) .then((result) => { playSound("sent"); toast.success(`Submitted Exam ID: ${result.data.id}`); }) .catch((error) => { console.log(error); toast.error(error.response.data.error || "Something went wrong while submitting, please try again later."); }) } const preview = () => { setExam({ parts: sections.map((s) => { const exercise = s.state as LevelPart; return { ...exercise, intro: s.settings.currentIntro, category: s.settings.category }; }), minTimer, module: "level", id: title, isDiagnostic: false, variant: undefined, difficulty, private: isPrivate, } as LevelExam); setExerciseIndex(0); setQuestionIndex(0); setPartIndex(0); openDetachedTab("popout?type=Exam&module=level", router) } const speakingExercise = focusedExercise === undefined ? undefined : currentSection.exercises[focusedExercise] as SpeakingExercise | InteractiveSpeakingExercise; return (
updateLocalAndScheduleGlobal({ isLevelDropdownOpen: isOpen }, false)} >
updateLocalAndScheduleGlobal({ isReadingDropdownOpen: isOpen }, false)} >
updateLocalAndScheduleGlobal({ isListeningDropdownOpen: isOpen }, false)} >
updateLocalAndScheduleGlobal({ isWritingDropdownOpen: isOpen }, false)} >
updateLocalAndScheduleGlobal({ isSpeakingDropdownOpen: isOpen }, false)} > updateLocalAndScheduleGlobal({ isSpeakingDropdownOpen: isOpen }, false)} >
{speakingExercise !== undefined && updateLocalAndScheduleGlobal({ isSpeakingDropdownOpen: isOpen }, false)} > }
); }; export default LevelSettings;