diff --git a/src/pages/content-management/SpeakingPage.tsx b/src/pages/content-management/SpeakingPage.tsx index ce50978..cdd5499 100644 --- a/src/pages/content-management/SpeakingPage.tsx +++ b/src/pages/content-management/SpeakingPage.tsx @@ -136,7 +136,9 @@ export function SpeakingPage() { const [subCourseLoading, setSubCourseLoading] = useState(false) const [subCourseSearch, setSubCourseSearch] = useState("") const [subCourseMenuOpen, setSubCourseMenuOpen] = useState(false) - const [setStatus, setSetStatus] = useState<"DRAFT" | "PUBLISHED">("DRAFT") + const [setStatus, setSetStatus] = useState<"DRAFT" | "PUBLISHED">("PUBLISHED") + const [createdSetId, setCreatedSetId] = useState(null) + const [creatingSet, setCreatingSet] = useState(false) const [currentStep, setCurrentStep] = useState(1) const [detailOpen, setDetailOpen] = useState(false) const [detailLoading, setDetailLoading] = useState(false) @@ -399,10 +401,48 @@ export function SpeakingPage() { setSetDescription("") setIntroVideoUrl("") setSubCourseId("") - setSetStatus("DRAFT") + setSetStatus("PUBLISHED") + setCreatedSetId(null) setQuestionDrafts([createEmptyDraft()]) } + const handleProceedToQuestions = async () => { + if (!canProceedToQuestions) return + if (createdSetId) { + setCurrentStep(2) + return + } + + const parsedSubCourseId = Number(subCourseId) + if (!Number.isFinite(parsedSubCourseId) || parsedSubCourseId <= 0) { + toast.error("Please select a valid sub-course") + return + } + + setCreatingSet(true) + try { + const setRes = await createQuestionSet({ + title: setTitle.trim(), + ...(setDescription.trim() ? { description: setDescription.trim() } : {}), + set_type: "PRACTICE", + owner_type: "SUB_COURSE", + owner_id: parsedSubCourseId, + status: setStatus, + ...(introVideoUrl.trim() ? { intro_video_url: introVideoUrl.trim() } : {}), + }) + const setId = setRes.data?.data?.id + if (!setId) throw new Error("Question set creation failed: missing set ID") + setCreatedSetId(setId) + setCurrentStep(2) + toast.success("Practice created. Continue adding questions.") + } catch (error) { + console.error("Failed to create speaking practice set:", error) + toast.error("Failed to create practice set") + } finally { + setCreatingSet(false) + } + } + const handleIntroVideoFileChange = async (event: ChangeEvent) => { const file = event.target.files?.[0] event.target.value = "" @@ -844,18 +884,7 @@ export function SpeakingPage() { setSaving(true) try { - // 1) Create speaking practice set. - const setRes = await createQuestionSet({ - title: setTitle.trim(), - ...(setDescription.trim() ? { description: setDescription.trim() } : {}), - set_type: "PRACTICE", - owner_type: "SUB_COURSE", - owner_id: parsedSubCourseId, - status: setStatus, - ...(introVideoUrl.trim() ? { intro_video_url: introVideoUrl.trim() } : {}), - }) - - const setId = setRes.data?.data?.id + const setId = createdSetId if (!setId) throw new Error("Question set creation failed: missing set ID") // 2) Create all AUDIO questions then attach in sequence. @@ -1039,6 +1068,7 @@ export function SpeakingPage() {