import { SpinnerIcon } from "@/components/ui/spinner-icon"; import { useCallback, useEffect, useState } from "react" import { toast } from "sonner" import { notifyApiError } from "../../../lib/apiErrors" import { getPracticesByParentCourse, getPracticesByParentModule, setLearnEnglishPracticePublishStatus, } from "../../../api/courses.api" import type { PracticeParentKind } from "../../../types/course.types" import { Button } from "../../../components/ui/button" import { cn } from "../../../lib/utils" import { draftPracticesForParent, isPracticePublished, unwrapPracticesList, } from "../../../lib/parentContextPractice" type Props = { parentKind: Extract parentId: number className?: string onPublished?: () => void } export function PublishPracticeButton({ parentKind, parentId, className, onPublished, }: Props) { const [loading, setLoading] = useState(true) const [acting, setActing] = useState(false) const [hasDraft, setHasDraft] = useState(false) const [allPublished, setAllPublished] = useState(false) const [hasPractice, setHasPractice] = useState(false) const loadPractices = useCallback(async () => { if (!Number.isFinite(parentId) || parentId < 1) { setHasPractice(false) setHasDraft(false) setAllPublished(false) setLoading(false) return } setLoading(true) try { const res = parentKind === "COURSE" ? await getPracticesByParentCourse(parentId, { limit: 50, offset: 0 }) : await getPracticesByParentModule(parentId, { limit: 50, offset: 0 }) const list = unwrapPracticesList(res) const drafts = draftPracticesForParent(list) setHasPractice(list.length > 0) setHasDraft(drafts.length > 0) setAllPublished( list.length > 0 && list.every((p) => isPracticePublished(p)), ) } catch { setHasPractice(false) setHasDraft(false) setAllPublished(false) } finally { setLoading(false) } }, [parentKind, parentId]) useEffect(() => { void loadPractices() }, [loadPractices]) const isDraftMode = allPublished const handlePublish = async () => { if (!Number.isFinite(parentId) || parentId < 1) return setActing(true) try { const res = parentKind === "COURSE" ? await getPracticesByParentCourse(parentId, { limit: 50, offset: 0 }) : await getPracticesByParentModule(parentId, { limit: 50, offset: 0 }) const drafts = draftPracticesForParent(unwrapPracticesList(res)) if (drafts.length === 0) { toast.info("No draft practice to publish") await loadPractices() return } for (const practice of drafts) { await setLearnEnglishPracticePublishStatus(practice.id, { publish_status: "PUBLISHED", }) } toast.success( drafts.length === 1 ? "Practice published" : `${drafts.length} practices published`, ) await loadPractices() onPublished?.() } catch (e: unknown) { notifyApiError(e, "Failed to publish practice") } finally { setActing(false) } } const handleSaveAsDraft = async () => { if (!Number.isFinite(parentId) || parentId < 1) return setActing(true) try { const res = parentKind === "COURSE" ? await getPracticesByParentCourse(parentId, { limit: 50, offset: 0 }) : await getPracticesByParentModule(parentId, { limit: 50, offset: 0 }) const toDraft = unwrapPracticesList(res).filter(isPracticePublished) if (toDraft.length === 0) { toast.info("No published practice to save as draft") await loadPractices() return } for (const practice of toDraft) { await setLearnEnglishPracticePublishStatus(practice.id, { publish_status: "DRAFT", }) } toast.success( toDraft.length === 1 ? "Practice saved as draft" : `${toDraft.length} practices saved as draft`, ) await loadPractices() onPublished?.() } catch (e: unknown) { notifyApiError(e, "Failed to save practice as draft") } finally { setActing(false) } } const disabled = loading || acting || !hasPractice || (!hasDraft && !allPublished) let label = "Publish Practice" if (loading) label = "Loading…" else if (acting) label = isDraftMode ? "Saving…" : "Publishing…" else if (allPublished) label = "Save as Draft" const handleClick = () => { if (isDraftMode) { void handleSaveAsDraft() return } void handlePublish() } return ( ) }