import { notifyApiError } from "../../lib/apiErrors" import { useState } from "react"; import { useNavigate, useParams } from "react-router-dom"; import { toast } from "sonner"; import { PageBackLink } from "../../components/navigation/PageBackLink"; import { navigateBack } from "../../lib/navigateBack"; import { Button } from "../../components/ui/button"; import { Stepper } from "../../components/ui/stepper"; import { createModuleLesson } from "../../api/courses.api"; import type { PracticePublishStatus } from "../../types/course.types"; import { VideoDetailStep } from "./components/video-steps/VideoDetailStep"; import { ReviewPublishStep } from "./components/video-steps/ReviewPublishStep"; import successIcon from "../../assets/success.svg"; const STEPS = [ { id: 1, label: "Video Detail" }, { id: 2, label: "Review & Publish" }, ]; export type AddLessonFormData = { title: string; sortOrder: string; description: string; videoUrl: string; thumbnailUrl: string; }; const emptyForm = (): AddLessonFormData => ({ title: "", sortOrder: "0", description: "", videoUrl: "", thumbnailUrl: "", }); function descriptionToApiPlain(html: string): string { if (!html?.trim()) return ""; const t = html.trim(); if (!t.includes("<")) return t; if (typeof document === "undefined") { return t.replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim(); } const doc = new DOMParser().parseFromString(html, "text/html"); return doc.body.textContent?.replace(/\s+/g, " ").trim() ?? ""; } export function AddVideoFlow() { const navigate = useNavigate(); const { level, courseId, moduleId } = useParams<{ level: string; courseId: string; moduleId: string; }>(); const [currentStep, setCurrentStep] = useState(1); const [isPublished, setIsPublished] = useState(false); const [lastCreatedPublishStatus, setLastCreatedPublishStatus] = useState("PUBLISHED"); const [formData, setFormData] = useState(emptyForm); const [publishing, setPublishing] = useState(false); const [formResetKey, setFormResetKey] = useState(0); const nextStep = () => setCurrentStep((prev) => Math.min(prev + 1, 2)); const prevStep = () => setCurrentStep((prev) => Math.max(prev - 1, 1)); const backPath = `/new-content/learn-english/${level}/courses/${courseId}/modules/${moduleId}`; const goBack = () => navigateBack(navigate, backPath); const handleCreateLesson = async (publishStatus: PracticePublishStatus) => { const mid = Number(moduleId); if (!Number.isFinite(mid) || mid < 1) { toast.error("Invalid module"); return; } const title = formData.title.trim(); const videoUrl = formData.videoUrl.trim(); const thumbnail = formData.thumbnailUrl.trim(); if (!title) { toast.error("Title is required"); return; } if (!videoUrl) { toast.error("Video URL is required"); return; } if (!thumbnail) { toast.error("Thumbnail is required"); return; } const description = descriptionToApiPlain(formData.description); if (!description) { toast.error("Description is required"); return; } const sortOrderRaw = formData.sortOrder.trim(); if (sortOrderRaw === "") { toast.error("Sort order is required"); return; } const sort_order = Number(sortOrderRaw); if (!Number.isInteger(sort_order) || sort_order < 0) { toast.error("Sort order must be a whole number of 0 or greater"); return; } setPublishing(true); try { await createModuleLesson(mid, { title, video_url: videoUrl, thumbnail, description, sort_order, publish_status: publishStatus, }); setLastCreatedPublishStatus(publishStatus); toast.success( publishStatus === "DRAFT" ? "Lesson saved as draft" : "Lesson published", ); setIsPublished(true); } catch (e: unknown) { console.error(e); notifyApiError(e, "Failed to create lesson"); } finally { setPublishing(false); } }; if (isPublished) { return (
Success

{lastCreatedPublishStatus === "DRAFT" ? "Lesson saved as draft" : "Lesson published successfully"}

{lastCreatedPublishStatus === "DRAFT" ? "You can finish editing and publish it later from the module." : "Your lesson is now available in this module."}

); } return (

Add new lesson

s.label)} currentStep={currentStep} />
{currentStep === 1 && ( )} {currentStep === 2 && ( void handleCreateLesson(status)} publishing={publishing} /> )}
); }