import { useState } from "react" import { Loader2, Plus, Trash2 } from "lucide-react" import { toast } from "sonner" import { Button } from "../../../../components/ui/button" import { Input } from "../../../../components/ui/input" import type { PracticeParent, PracticeParentKind } from "../../../../types/course.types" import { dedupeParents, formatPracticeParentLabel, isPracticeParentKind, newParentRow, parentsFromPractice, } from "../../../../lib/practiceParents" import { isPracticeParentUnlinkNotLinkedError, mapPracticeParentUnlinkError, unlinkParentConfirmMessage, unlinkPracticeFromParent, } from "../../../../lib/practiceParentUnlink" const LMS_KIND_OPTIONS: { value: PracticeParentKind; label: string }[] = [ { value: "COURSE", label: "Course" }, { value: "MODULE", label: "Module" }, { value: "LESSON", label: "Lesson" }, ] const EXAM_PREP_KIND_OPTIONS: { value: PracticeParentKind; label: string }[] = [ { value: "CATALOG_COURSE", label: "Catalog course" }, { value: "UNIT", label: "Unit" }, { value: "LESSON", label: "Lesson" }, ] interface PracticeParentsFieldProps { parents: PracticeParent[] onChange: (parents: PracticeParent[]) => void disabled?: boolean /** When set, this row cannot be removed (contextual entry point). */ lockedParentKey?: string | null /** When true, all rows can be cleared (unlinked practice). */ optional?: boolean /** Use exam-prep parent kinds instead of Learn English. */ isExamPrep?: boolean /** When set, removing a saved location calls DELETE .../parents immediately. */ practiceId?: number /** Called after a successful per-parent unlink with the remaining parents. */ onUnlinked?: (parents: PracticeParent[]) => void } function parentKey(parent: PracticeParent): string { return `${parent.parent_kind}:${parent.parent_id}` } export function PracticeParentsField({ parents, onChange, disabled = false, lockedParentKey = null, optional = false, isExamPrep = false, practiceId, onUnlinked, }: PracticeParentsFieldProps) { const kindOptions = isExamPrep ? EXAM_PREP_KIND_OPTIONS : LMS_KIND_OPTIONS const rows = parents.length > 0 ? parents : [newParentRow(isExamPrep ? "CATALOG_COURSE" : "LESSON")] const [unlinkingKey, setUnlinkingKey] = useState(null) const canPersistUnlink = practiceId != null && Number.isFinite(practiceId) && practiceId > 0 const updateRow = (index: number, patch: Partial) => { const next = rows.map((row, i) => (i === index ? { ...row, ...patch } : row)) onChange(dedupeParents(next)) } const removeRowLocally = (index: number) => { const row = rows[index] if (lockedParentKey && parentKey(row) === lockedParentKey) return if (rows.length <= 1) { if (optional) onChange([]) return } onChange(rows.filter((_, i) => i !== index)) } const removeRow = async (index: number) => { const row = rows[index] if (lockedParentKey && parentKey(row) === lockedParentKey) return if (disabled || unlinkingKey) return const hasSavedParent = row.parent_id > 0 && isPracticeParentKind(row.parent_kind) if (canPersistUnlink && hasSavedParent) { const label = formatPracticeParentLabel(row) const remaining = parentsFromPractice({ parents }).filter( (p) => parentKey(p) !== parentKey(row), ) const proceed = window.confirm( unlinkParentConfirmMessage(label, { isLastParent: remaining.length === 0, }), ) if (!proceed) return const key = parentKey(row) setUnlinkingKey(key) try { const updated = await unlinkPracticeFromParent({ practiceId: practiceId!, parent: row, isExamPrep, }) const nextParents = parentsFromPractice(updated) onChange(nextParents) onUnlinked?.(nextParents) toast.success( remaining.length === 0 ? "Practice is now unlinked" : `Removed from ${label}`, ) } catch (err) { if (isPracticeParentUnlinkNotLinkedError(err)) { const nextParents = parentsFromPractice({ parents }).filter( (p) => parentKey(p) !== parentKey(row), ) onChange(nextParents) onUnlinked?.(nextParents) toast.info("This location was already removed.") return } toast.error("Could not remove location", { description: mapPracticeParentUnlinkError(err), }) } finally { setUnlinkingKey(null) } return } removeRowLocally(index) } const addRow = () => { onChange([...rows, newParentRow()]) } return (

Attached locations

{canPersistUnlink ? "Remove a location with × to detach immediately. Add new locations here, then save the practice." : optional ? isExamPrep ? "Attach now or leave empty and link this practice to catalog courses, units, or lessons later." : "Attach now or leave empty and link this practice later." : isExamPrep ? "Link this practice to one or more catalog courses, units, or lessons." : "Link this practice to one or more courses, modules, or lessons."}

    {rows.map((row, index) => { const key = parentKey(row) const isLocked = Boolean(lockedParentKey && key === lockedParentKey && row.parent_id > 0) const isUnlinking = unlinkingKey === key return (
  • 0 ? row.parent_id : ""} onChange={(e) => updateRow(index, { parent_id: Number(e.target.value) || 0 }) } placeholder="Parent id" className="h-9 w-28 rounded-[8px] border-grayScale-200 font-mono text-sm" /> {row.parent_kind} parent id from the content hierarchy
  • ) })}
) }