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 <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-08-18 15:09:25 +03:00
parent 941a453592
commit 2fd5a96ea5
7 changed files with 105 additions and 54 deletions

View File

@ -766,6 +766,7 @@ export const unlinkExamPrepPracticeParent = (
) => ) =>
http.delete<UpdatePracticeParentsResponse>( http.delete<UpdatePracticeParentsResponse>(
`/exam-prep/practices/${practiceId}/parents/${parent.parent_kind.toUpperCase()}/${parent.parent_id}`, `/exam-prep/practices/${practiceId}/parents/${parent.parent_kind.toUpperCase()}/${parent.parent_id}`,
{ skipErrorToast: true },
) )
/** DELETE /exam-prep/practices/:practiceId */ /** DELETE /exam-prep/practices/:practiceId */
@ -967,6 +968,7 @@ export const unlinkPracticeParent = (
) => ) =>
http.delete<UpdatePracticeParentsResponse>( http.delete<UpdatePracticeParentsResponse>(
`/practices/${practiceId}/parents/${parent.parent_kind.toUpperCase()}/${parent.parent_id}`, `/practices/${practiceId}/parents/${parent.parent_kind.toUpperCase()}/${parent.parent_id}`,
{ skipErrorToast: true },
) )
/** PUT /practices/:id */ /** PUT /practices/:id */

View File

@ -45,6 +45,28 @@ export function isPracticeParentUnlinkNotLinkedError(err: unknown): boolean {
return msg.includes("not linked") 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: { export async function unlinkPracticeFromParent(opts: {
practiceId: number practiceId: number
parent: PracticeParent parent: PracticeParent

View File

@ -550,20 +550,22 @@ export function CourseDetailPage() {
</ContentPageDescription> </ContentPageDescription>
</div> </div>
<div className="flex items-center gap-4"> <div className="flex items-center gap-4">
<PracticeActionButton {practices.length === 0 ? (
variant="outline" <PracticeActionButton
className="rounded-[6px] border-brand-500 text-brand-500 " variant="outline"
pathOptions={{ className="rounded-[6px] border-brand-500 text-brand-500 "
isExamPrep: false, pathOptions={{
level: programIdParam, isExamPrep: false,
courseId: courseIdParam, level: programIdParam,
backTo: "modules", courseId: courseIdParam,
}} backTo: "modules",
parentLabel={displayTitle} }}
> parentLabel={displayTitle}
<Calendar className="h-4 w-4" /> >
Add Practice <Calendar className="h-4 w-4" />
</PracticeActionButton> Add Practice
</PracticeActionButton>
) : null}
<Button <Button
className="rounded-[6px] bg-brand-500 font-semibold hover:bg-brand-600" className="rounded-[6px] bg-brand-500 font-semibold hover:bg-brand-600"
onClick={() => setIsAddModuleOpen(true)} onClick={() => setIsAddModuleOpen(true)}

View File

@ -310,6 +310,12 @@ export function LessonPracticesPage() {
<ContentPageDescription className="mt-1 text-[14px] text-grayScale-500"> <ContentPageDescription className="mt-1 text-[14px] text-grayScale-500">
Review speaking practices linked to this lesson. Review speaking practices linked to this lesson.
</ContentPageDescription> </ContentPageDescription>
{!isExamPrep && practices.length >= 1 ? (
<p className="mt-2 text-sm text-grayScale-500">
This lesson already has a practice. Remove or unlink it before adding
another.
</p>
) : null}
{!loading && !loadError ? ( {!loading && !loadError ? (
<p className="mt-2 text-sm text-grayScale-500"> <p className="mt-2 text-sm text-grayScale-500">
{totalCount} practice{totalCount === 1 ? "" : "s"} linked to this {totalCount} practice{totalCount === 1 ? "" : "s"} linked to this
@ -333,14 +339,16 @@ export function LessonPracticesPage() {
/> />
Refresh Refresh
</Button> </Button>
<PracticeActionButton {!isExamPrep && practices.length === 0 ? (
className="rounded-[6px] bg-brand-500 font-semibold hover:bg-brand-600" <PracticeActionButton
pathOptions={practicePathOptions} className="rounded-[6px] bg-brand-500 font-semibold hover:bg-brand-600"
parentLabel={displayTitle} pathOptions={practicePathOptions}
> parentLabel={displayTitle}
<Calendar className="h-4 w-4" /> >
Add Practice <Calendar className="h-4 w-4" />
</PracticeActionButton> Add Practice
</PracticeActionButton>
) : null}
</div> </div>
</div> </div>

View File

@ -28,7 +28,7 @@ import {
import { parentsFromPractice } from "../../lib/practiceParents"; import { parentsFromPractice } from "../../lib/practiceParents";
import { import {
isPracticeParentUnlinkNotLinkedError, isPracticeParentUnlinkNotLinkedError,
mapPracticeParentUnlinkError, resolveModuleContextUnlinkParent,
unlinkPracticeFromParent, unlinkPracticeFromParent,
} from "../../lib/practiceParentUnlink"; } from "../../lib/practiceParentUnlink";
import { Button } from "../../components/ui/button"; 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); const mid = Number(moduleId);
if (!Number.isFinite(mid) || mid < 1) return null; if (!practiceToUnlink || !Number.isFinite(mid) || mid < 1) return null;
return { parent_kind: "MODULE", parent_id: mid }; return resolveModuleContextUnlinkParent(
}, [moduleId]); practiceToUnlink,
mid,
moduleLessonIds,
);
}, [moduleId, moduleLessonIds, practiceToUnlink]);
const confirmUnlinkPractice = async () => { const confirmUnlinkPractice = async () => {
if (!practiceToUnlink || !moduleParent) return; if (!practiceToUnlink || !practiceUnlinkParent) {
toast.error("This practice is not linked to this module.");
return;
}
setUnlinkingPractice(true); setUnlinkingPractice(true);
try { try {
await unlinkPracticeFromParent({ await unlinkPracticeFromParent({
practiceId: practiceToUnlink.id, practiceId: practiceToUnlink.id,
parent: moduleParent, parent: practiceUnlinkParent,
isExamPrep: false, isExamPrep: false,
}); });
toast.success(`Practice removed from ${displayModuleName}`); toast.success(`Practice removed from ${displayModuleName}`);
@ -377,7 +389,7 @@ export function ModuleDetailPage() {
await loadModulePractices(); await loadModulePractices();
return; return;
} }
notifyApiError(err, "Could not remove from module"); notifyApiError(e, "Could not remove from module");
} finally { } finally {
setUnlinkingPractice(false); setUnlinkingPractice(false);
} }
@ -529,15 +541,17 @@ export function ModuleDetailPage() {
</ContentPageDescription> </ContentPageDescription>
</div> </div>
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<PracticeActionButton {practices.length === 0 ? (
variant="outline" <PracticeActionButton
className="rounded-[6px] border-brand-500 text-brand-500 " variant="outline"
pathOptions={modulePracticePathOptions} className="rounded-[6px] border-brand-500 text-brand-500 "
parentLabel={displayModuleName} pathOptions={modulePracticePathOptions}
> parentLabel={displayModuleName}
<Calendar className="h-4 w-4" /> >
Add Practice <Calendar className="h-4 w-4" />
</PracticeActionButton> Add Practice
</PracticeActionButton>
) : null}
<Button <Button
className="rounded-[6px] bg-brand-500 font-semibold hover:bg-brand-600" className="rounded-[6px] bg-brand-500 font-semibold hover:bg-brand-600"
onClick={() => onClick={() =>
@ -632,16 +646,19 @@ export function ModuleDetailPage() {
onEdit={() => openEditLesson(lesson)} onEdit={() => openEditLesson(lesson)}
onDelete={() => setDeletingLesson(lesson)} onDelete={() => setDeletingLesson(lesson)}
description={lesson.description} description={lesson.description}
onAddPractice={() => onAddPractice={
setLessonPracticeChoice({ lesson.has_practice
isExamPrep: false, ? undefined
level, : () =>
courseId, setLessonPracticeChoice({
moduleId, isExamPrep: false,
lessonId: String(lesson.id), level,
lessonTitle: lesson.title, courseId,
backTo: "lesson", moduleId,
}) lessonId: String(lesson.id),
lessonTitle: lesson.title,
backTo: "lesson",
})
} }
onViewPractices={() => onViewPractices={() =>
navigate( navigate(
@ -926,7 +943,7 @@ export function ModuleDetailPage() {
</div> </div>
)} )}
{practiceToUnlink && moduleParent ? ( {practiceToUnlink && practiceUnlinkParent ? (
<Dialog <Dialog
open={practiceToUnlink != null} open={practiceToUnlink != null}
onOpenChange={(open) => { onOpenChange={(open) => {
@ -948,8 +965,8 @@ export function ModuleDetailPage() {
{parentsFromPractice(practiceToUnlink).filter( {parentsFromPractice(practiceToUnlink).filter(
(p) => (p) =>
!( !(
p.parent_kind === moduleParent.parent_kind && p.parent_kind === practiceUnlinkParent.parent_kind &&
p.parent_id === moduleParent.parent_id p.parent_id === practiceUnlinkParent.parent_id
), ),
).length === 0 ).length === 0
? " The practice will be unlinked until re-attached." ? " The practice will be unlinked until re-attached."

View File

@ -173,7 +173,7 @@ export function CatalogCoursePracticesPanel({
await load() await load()
return return
} }
notifyApiError(err, "Could not remove location") notifyApiError(e, "Could not remove location")
} finally { } finally {
setUnlinking(false) setUnlinking(false)
} }

View File

@ -172,7 +172,7 @@ export function UnitPracticesPanel({
await load() await load()
return return
} }
notifyApiError(err, "Could not remove location") notifyApiError(e, "Could not remove location")
} finally { } finally {
setUnlinking(false) setUnlinking(false)
} }