diff --git a/src/modules/admin/pages/courses/EditCourse.jsx b/src/modules/admin/pages/courses/EditCourse.jsx index 0453bfe..40ff674 100644 --- a/src/modules/admin/pages/courses/EditCourse.jsx +++ b/src/modules/admin/pages/courses/EditCourse.jsx @@ -394,25 +394,60 @@ export default function EditCourse() { }; // ─── Form submit (step 0 → step 1) ─────────────────────────────────────── + const saveBasicInfo = async (values) => { + const payload = { + ...values, + objectives: values.objectives?.map((o, i) => ({ + objective_id: o.objective_id ?? null, + text: o.text, + order_index: i, + })) ?? [], + level: values.level || null, + course_code: values.course_code || null, + updatedBy: user?.user_id ?? null, + }; + return updateCourse(courseId, payload); + }; + const onSubmit = async (values) => { if (isDirty) { - const payload = { - ...values, - objectives: values.objectives?.map((o, i) => ({ - objective_id: o.objective_id ?? null, - text: o.text, - order_index: i, - })) ?? [], - level: values.level || null, - course_code: values.course_code || null, - updatedBy: user?.user_id ?? null, - }; - const result = await updateCourse(courseId, payload); + const result = await saveBasicInfo(values); if (!result) return; } setCurrentStep(1); }; + // ─── Done — persists any unsaved section before leaving the wizard ─────── + // + // Each section (Categories, Instructors, Badge, Achievements, Product) has + // its own "Save X" button and dirty flag, so it's easy to edit a section, + // skip its save button, and hit "Done" — which previously just navigated + // away and silently discarded that section's changes. Flush every dirty + // section here instead of trusting the user caught every save button. + const [doneLoading, setDoneLoading] = useState(false); + const handleDone = async () => { + setDoneLoading(true); + try { + if (isDirty) { + let valid = true; + await handleSubmit( + async (values) => { valid = !!(await saveBasicInfo(values)); }, + () => { valid = false; setCurrentStep(0); }, + )(); + if (!valid) return; + } + if (categoriesDirty) await handleSaveCategories(); + if (instructorsDirty) await handleSaveInstructors(); + if (badgeDirty) await handleSaveBadge(); + if (achievementsDirty) await handleSaveAchievements(); + if (productDirty && productForm.price) await handleSaveProduct(); + + navigate(`/admin/courses/${courseId}/view`); + } finally { + setDoneLoading(false); + } + }; + return (