diff --git a/src/lib/practiceParentUnlink.ts b/src/lib/practiceParentUnlink.ts index 1fb664e..dc7fdce 100644 --- a/src/lib/practiceParentUnlink.ts +++ b/src/lib/practiceParentUnlink.ts @@ -67,6 +67,27 @@ export function resolveModuleContextUnlinkParent( ) } +/** Parent row to DELETE when removing a practice from a course's practice tab. */ +export function resolveCourseContextUnlinkParent( + practice: ParentContextPractice, + courseId: number, + moduleIdsInCourse: number[], +): PracticeParent | null { + const parents = parentsFromPractice(practice) + const courseLink = parents.find( + (p) => p.parent_kind === "COURSE" && p.parent_id === courseId, + ) + if (courseLink) return courseLink + + const moduleIds = new Set(moduleIdsInCourse.filter((id) => id > 0)) + const moduleLink = parents.find( + (p) => p.parent_kind === "MODULE" && moduleIds.has(p.parent_id), + ) + if (moduleLink) return moduleLink + + return parents.find((p) => p.parent_kind === "LESSON") ?? null +} + export async function unlinkPracticeFromParent(opts: { practiceId: number parent: PracticeParent diff --git a/src/lib/practiceParents.ts b/src/lib/practiceParents.ts index 42de60a..d4261f9 100644 --- a/src/lib/practiceParents.ts +++ b/src/lib/practiceParents.ts @@ -156,3 +156,6 @@ export function primaryPracticeParent(parents: PracticeParent[]): PracticeParent export function newParentRow(kind: PracticeParentKind = "LESSON"): PracticeParent { return { parent_kind: kind, parent_id: 0 } } + +export const learnEnglishPracticeLimitHint = + "This location already has a practice. Remove or unlink it before adding another." diff --git a/src/pages/content-management/CourseDetailPage.tsx b/src/pages/content-management/CourseDetailPage.tsx index 3d94840..76517e3 100644 --- a/src/pages/content-management/CourseDetailPage.tsx +++ b/src/pages/content-management/CourseDetailPage.tsx @@ -29,6 +29,7 @@ import spinnerSrc from "../../assets/Circular-indeterminate progress indicator.s import alertSrc from "../../assets/Alert.svg"; import { deleteTopLevelCourseModule, + deleteParentLinkedPractice, getPracticesByParentCourse, getProgramCourses, getTopLevelCourseModules, @@ -41,6 +42,7 @@ import { refreshFileUrl, resolveFileUrl } from "../../api/files.api"; import type { ParentContextPractice, ContentAccessTier, + PracticeParent, PracticePublishStatus, ProgramCourseListItem, TopLevelCourseModuleItem, @@ -63,6 +65,12 @@ import { ModuleIconUploadField } from "./components/ModuleIconUploadField"; import { ModulePracticeCard } from "./components/ModulePracticeCard"; import { PublishPracticeButton } from "./components/PublishPracticeButton"; import { DisplayValue } from "../../lib/displayValue" +import { learnEnglishPracticeLimitHint, parentsFromPractice } from "../../lib/practiceParents" +import { + isPracticeParentUnlinkNotLinkedError, + resolveCourseContextUnlinkParent, + unlinkPracticeFromParent, +} from "../../lib/practiceParentUnlink" import { SearchHighlight } from "../../components/SearchHighlight" const MODULE_CARD_GRADIENT = "from-[#8E44AD] to-[#C39BD3]" as const; @@ -197,6 +205,12 @@ export function CourseDetailPage() { const [publishStatusPracticeId, setPublishStatusPracticeId] = useState< number | null >(null); + const [practiceToUnlink, setPracticeToUnlink] = + useState(null); + const [practiceToDelete, setPracticeToDelete] = + useState(null); + const [unlinkingPractice, setUnlinkingPractice] = useState(false); + const [deletingPractice, setDeletingPractice] = useState(false); const [publishStatusModuleId, setPublishStatusModuleId] = useState< number | null >(null); @@ -396,6 +410,65 @@ export function CourseDetailPage() { } }; + const courseModuleIds = useMemo( + () => modules.map((module) => module.id).filter((id) => id > 0), + [modules], + ); + + const practiceUnlinkParent = useMemo((): PracticeParent | null => { + if (!practiceToUnlink || !Number.isFinite(courseIdNum) || courseIdNum < 1) { + return null; + } + return resolveCourseContextUnlinkParent( + practiceToUnlink, + courseIdNum, + courseModuleIds, + ); + }, [courseIdNum, courseModuleIds, practiceToUnlink]); + + const confirmUnlinkPractice = async () => { + if (!practiceToUnlink || !practiceUnlinkParent) { + toast.error("This practice is not linked to this course."); + return; + } + setUnlinkingPractice(true); + try { + await unlinkPracticeFromParent({ + practiceId: practiceToUnlink.id, + parent: practiceUnlinkParent, + isExamPrep: false, + }); + toast.success(`Practice removed from ${displayTitle}`); + setPracticeToUnlink(null); + await loadCoursePractices(); + } catch (e) { + if (isPracticeParentUnlinkNotLinkedError(e)) { + toast.info("This location was already removed."); + setPracticeToUnlink(null); + await loadCoursePractices(); + return; + } + notifyApiError(e, "Could not remove from course"); + } finally { + setUnlinkingPractice(false); + } + }; + + const confirmDeletePractice = async () => { + if (!practiceToDelete) return; + setDeletingPractice(true); + try { + await deleteParentLinkedPractice(practiceToDelete.id); + toast.success("Practice deleted"); + setPracticeToDelete(null); + await loadCoursePractices(); + } catch (e: unknown) { + notifyApiError(e, "Failed to delete practice"); + } finally { + setDeletingPractice(false); + } + }; + const handleModulePublishStatus = async ( moduleId: number, nextStatus: PracticePublishStatus, @@ -550,22 +623,24 @@ export function CourseDetailPage() {
- {practices.length === 0 ? ( - - - Add Practice - - ) : null} + 0} + title={ + practices.length > 0 ? learnEnglishPracticeLimitHint : undefined + } + > + + Add Practice +
@@ -925,6 +1002,90 @@ export function CourseDetailPage() { )} + {practiceToUnlink && practiceUnlinkParent ? ( + { + if (!open && !unlinkingPractice) setPracticeToUnlink(null); + }} + > + + + Remove from this course? + + + {practiceToUnlink.title} + {" "} + will be detached from{" "} + + {displayTitle} + + . + {parentsFromPractice(practiceToUnlink).filter( + (p) => + !( + p.parent_kind === practiceUnlinkParent.parent_kind && + p.parent_id === practiceUnlinkParent.parent_id + ), + ).length === 0 + ? " The practice will be unlinked until re-attached." + : " Other locations are unaffected."} + + + + + + + + + ) : null} + + { + if (!open && !deletingPractice) setPracticeToDelete(null); + }} + > + + + Delete this practice permanently? + + + {practiceToDelete?.title} + {" "} + and all of its questions will be deleted. This cannot be undone. + + + + + + + + + {deletingModule && (
diff --git a/src/pages/content-management/LessonPracticesPage.tsx b/src/pages/content-management/LessonPracticesPage.tsx index d329d45..b6788fe 100644 --- a/src/pages/content-management/LessonPracticesPage.tsx +++ b/src/pages/content-management/LessonPracticesPage.tsx @@ -11,7 +11,10 @@ import { setExamPrepPracticePublishStatus, setLearnEnglishPracticePublishStatus, } from "../../api/courses.api"; -import { parentsFromPractice } from "../../lib/practiceParents"; +import { + learnEnglishPracticeLimitHint, + parentsFromPractice, +} from "../../lib/practiceParents"; import { isPracticeParentUnlinkNotLinkedError, unlinkPracticeFromParent, @@ -312,8 +315,7 @@ export function LessonPracticesPage() { {!isExamPrep && practices.length >= 1 ? (

- This lesson already has a practice. Remove or unlink it before adding - another. + {learnEnglishPracticeLimitHint}

) : null} {!loading && !loadError ? ( @@ -339,16 +341,20 @@ export function LessonPracticesPage() { /> Refresh - {!isExamPrep && practices.length === 0 ? ( - - - Add Practice - - ) : null} + = 1} + title={ + !isExamPrep && practices.length >= 1 + ? learnEnglishPracticeLimitHint + : undefined + } + > + + Add Practice +
diff --git a/src/pages/content-management/ModuleDetailPage.tsx b/src/pages/content-management/ModuleDetailPage.tsx index db6dd83..76b1637 100644 --- a/src/pages/content-management/ModuleDetailPage.tsx +++ b/src/pages/content-management/ModuleDetailPage.tsx @@ -4,6 +4,7 @@ import { ArrowLeft, Video, Calendar, Trash2, X } from "lucide-react"; import { Link, useLocation, useNavigate, useParams } from "react-router-dom"; import { toast } from "sonner"; import { + deleteParentLinkedPractice, deleteTopLevelModuleLesson, getModuleLessons, getPracticesByParentModule, @@ -25,7 +26,7 @@ import { fetchAllOffsetPages, offsetPageFromListEnvelope, } from "../../lib/fetchAllOffsetPages"; -import { parentsFromPractice } from "../../lib/practiceParents"; +import { learnEnglishPracticeLimitHint, parentsFromPractice } from "../../lib/practiceParents"; import { isPracticeParentUnlinkNotLinkedError, resolveModuleContextUnlinkParent, @@ -138,7 +139,10 @@ export function ModuleDetailPage() { >(null); const [practiceToUnlink, setPracticeToUnlink] = useState(null); + const [practiceToDelete, setPracticeToDelete] = + useState(null); const [unlinkingPractice, setUnlinkingPractice] = useState(false); + const [deletingPractice, setDeletingPractice] = useState(false); const [loadedModuleName, setLoadedModuleName] = useState(null); const [loadedModuleDescription, setLoadedModuleDescription] = useState< string | null @@ -395,6 +399,21 @@ export function ModuleDetailPage() { } }; + const confirmDeletePractice = async () => { + if (!practiceToDelete) return; + setDeletingPractice(true); + try { + await deleteParentLinkedPractice(practiceToDelete.id); + toast.success("Practice deleted"); + setPracticeToDelete(null); + await loadModulePractices(); + } catch (e: unknown) { + notifyApiError(e, "Failed to delete practice"); + } finally { + setDeletingPractice(false); + } + }; + const openEditLesson = (lesson: TopLevelModuleLessonItem) => { setEditingLesson(lesson); setEditLessonTitle(lesson.title ?? ""); @@ -541,17 +560,19 @@ export function ModuleDetailPage() {
- {practices.length === 0 ? ( - - - Add Practice - - ) : null} + 0} + title={ + practices.length > 0 ? learnEnglishPracticeLimitHint : undefined + } + > + + Add Practice +
@@ -992,6 +1012,41 @@ export function ModuleDetailPage() { ) : null} + { + if (!open && !deletingPractice) setPracticeToDelete(null); + }} + > + + + Delete this practice permanently? + + + {practiceToDelete?.title} + {" "} + and all of its questions will be deleted. This cannot be undone. + + + + + + + + + {lessonPracticeChoice && lessonPracticeChoicePaths ? ( void; /** When set (e.g. on module lesson cards), shows an "Add practice" control scoped to this lesson. */ onAddPractice?: () => void; + /** When true, the lesson card shows a disabled "Add practice" control. */ + addPracticeDisabled?: boolean; /** When set with hoverModuleActions, shows a book icon next to edit/delete on thumbnail hover. */ onViewPractices?: () => void; onPublish?: () => void; @@ -113,6 +116,7 @@ export function VideoCard({ onDelete, onPublish, onAddPractice, + addPracticeDisabled = false, onViewPractices, onTogglePublishStatus, publishStatusUpdating = false, @@ -575,8 +579,11 @@ export function VideoCard({ type="button" variant="outline" className="h-9 w-full rounded-[6px] border-brand-200 text-[12px] font-bold text-brand-600 hover:bg-brand-50" + disabled={addPracticeDisabled} + title={addPracticeDisabled ? learnEnglishPracticeLimitHint : undefined} onClick={(e) => { e.stopPropagation(); + if (addPracticeDisabled) return; onAddPractice(); }} >