From 2fd5a96ea558a91a554d0b36f01e0abd7cc8dd21 Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Tue, 18 Aug 2026 15:09:25 +0300 Subject: [PATCH] Fix module practice unlink and enforce one practice per parent in admin UI. Resolve lesson vs module parent when unlinking from the module tab, suppress duplicate unlink error toasts, and hide add-practice actions when a parent already has a practice. Co-authored-by: Cursor --- src/api/courses.api.ts | 2 + src/lib/practiceParentUnlink.ts | 22 ++++++ .../content-management/CourseDetailPage.tsx | 30 ++++---- .../LessonPracticesPage.tsx | 24 ++++-- .../content-management/ModuleDetailPage.tsx | 77 +++++++++++-------- .../CatalogCoursePracticesPanel.tsx | 2 +- .../components/UnitPracticesPanel.tsx | 2 +- 7 files changed, 105 insertions(+), 54 deletions(-) diff --git a/src/api/courses.api.ts b/src/api/courses.api.ts index 85a1f9c..6540696 100644 --- a/src/api/courses.api.ts +++ b/src/api/courses.api.ts @@ -766,6 +766,7 @@ export const unlinkExamPrepPracticeParent = ( ) => http.delete( `/exam-prep/practices/${practiceId}/parents/${parent.parent_kind.toUpperCase()}/${parent.parent_id}`, + { skipErrorToast: true }, ) /** DELETE /exam-prep/practices/:practiceId */ @@ -967,6 +968,7 @@ export const unlinkPracticeParent = ( ) => http.delete( `/practices/${practiceId}/parents/${parent.parent_kind.toUpperCase()}/${parent.parent_id}`, + { skipErrorToast: true }, ) /** PUT /practices/:id */ diff --git a/src/lib/practiceParentUnlink.ts b/src/lib/practiceParentUnlink.ts index 241331b..1fb664e 100644 --- a/src/lib/practiceParentUnlink.ts +++ b/src/lib/practiceParentUnlink.ts @@ -45,6 +45,28 @@ export function isPracticeParentUnlinkNotLinkedError(err: unknown): boolean { return msg.includes("not linked") } +/** Parent row to DELETE when removing a practice from a module's practice tab. */ +export function resolveModuleContextUnlinkParent( + practice: ParentContextPractice, + moduleId: number, + lessonIdsInModule: number[], +): PracticeParent | null { + const parents = parentsFromPractice(practice) + const moduleLink = parents.find( + (p) => p.parent_kind === "MODULE" && p.parent_id === moduleId, + ) + if (moduleLink) return moduleLink + + const lessonIds = new Set(lessonIdsInModule.filter((id) => id > 0)) + if (lessonIds.size === 0) return null + + return ( + parents.find( + (p) => p.parent_kind === "LESSON" && lessonIds.has(p.parent_id), + ) ?? null + ) +} + export async function unlinkPracticeFromParent(opts: { practiceId: number parent: PracticeParent diff --git a/src/pages/content-management/CourseDetailPage.tsx b/src/pages/content-management/CourseDetailPage.tsx index b0a903d..3d94840 100644 --- a/src/pages/content-management/CourseDetailPage.tsx +++ b/src/pages/content-management/CourseDetailPage.tsx @@ -550,20 +550,22 @@ export function CourseDetailPage() {
- - - Add Practice - + {practices.length === 0 ? ( + + + Add Practice + + ) : null} - - - Add Practice - + {!isExamPrep && practices.length === 0 ? ( + + + Add Practice + + ) : null}
diff --git a/src/pages/content-management/ModuleDetailPage.tsx b/src/pages/content-management/ModuleDetailPage.tsx index 25a0ed6..db6dd83 100644 --- a/src/pages/content-management/ModuleDetailPage.tsx +++ b/src/pages/content-management/ModuleDetailPage.tsx @@ -28,7 +28,7 @@ import { import { parentsFromPractice } from "../../lib/practiceParents"; import { isPracticeParentUnlinkNotLinkedError, - mapPracticeParentUnlinkError, + resolveModuleContextUnlinkParent, unlinkPracticeFromParent, } from "../../lib/practiceParentUnlink"; import { Button } from "../../components/ui/button"; @@ -352,19 +352,31 @@ export function ModuleDetailPage() { } }; - const moduleParent = useMemo((): PracticeParent | null => { + const moduleLessonIds = useMemo( + () => lessons.map((lesson) => lesson.id).filter((id) => id > 0), + [lessons], + ); + + const practiceUnlinkParent = useMemo((): PracticeParent | null => { const mid = Number(moduleId); - if (!Number.isFinite(mid) || mid < 1) return null; - return { parent_kind: "MODULE", parent_id: mid }; - }, [moduleId]); + if (!practiceToUnlink || !Number.isFinite(mid) || mid < 1) return null; + return resolveModuleContextUnlinkParent( + practiceToUnlink, + mid, + moduleLessonIds, + ); + }, [moduleId, moduleLessonIds, practiceToUnlink]); const confirmUnlinkPractice = async () => { - if (!practiceToUnlink || !moduleParent) return; + if (!practiceToUnlink || !practiceUnlinkParent) { + toast.error("This practice is not linked to this module."); + return; + } setUnlinkingPractice(true); try { await unlinkPracticeFromParent({ practiceId: practiceToUnlink.id, - parent: moduleParent, + parent: practiceUnlinkParent, isExamPrep: false, }); toast.success(`Practice removed from ${displayModuleName}`); @@ -377,7 +389,7 @@ export function ModuleDetailPage() { await loadModulePractices(); return; } - notifyApiError(err, "Could not remove from module"); + notifyApiError(e, "Could not remove from module"); } finally { setUnlinkingPractice(false); } @@ -529,15 +541,17 @@ export function ModuleDetailPage() {
- - - Add Practice - + {practices.length === 0 ? ( + + + Add Practice + + ) : null}
)} - {practiceToUnlink && moduleParent ? ( + {practiceToUnlink && practiceUnlinkParent ? ( { @@ -948,8 +965,8 @@ export function ModuleDetailPage() { {parentsFromPractice(practiceToUnlink).filter( (p) => !( - p.parent_kind === moduleParent.parent_kind && - p.parent_id === moduleParent.parent_id + p.parent_kind === practiceUnlinkParent.parent_kind && + p.parent_id === practiceUnlinkParent.parent_id ), ).length === 0 ? " The practice will be unlinked until re-attached." diff --git a/src/pages/content-management/components/CatalogCoursePracticesPanel.tsx b/src/pages/content-management/components/CatalogCoursePracticesPanel.tsx index 500afe6..e777ce0 100644 --- a/src/pages/content-management/components/CatalogCoursePracticesPanel.tsx +++ b/src/pages/content-management/components/CatalogCoursePracticesPanel.tsx @@ -173,7 +173,7 @@ export function CatalogCoursePracticesPanel({ await load() return } - notifyApiError(err, "Could not remove location") + notifyApiError(e, "Could not remove location") } finally { setUnlinking(false) } diff --git a/src/pages/content-management/components/UnitPracticesPanel.tsx b/src/pages/content-management/components/UnitPracticesPanel.tsx index c3c040d..3ca47eb 100644 --- a/src/pages/content-management/components/UnitPracticesPanel.tsx +++ b/src/pages/content-management/components/UnitPracticesPanel.tsx @@ -172,7 +172,7 @@ export function UnitPracticesPanel({ await load() return } - notifyApiError(err, "Could not remove location") + notifyApiError(e, "Could not remove location") } finally { setUnlinking(false) }