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>(
`/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<UpdatePracticeParentsResponse>(
`/practices/${practiceId}/parents/${parent.parent_kind.toUpperCase()}/${parent.parent_id}`,
{ skipErrorToast: true },
)
/** PUT /practices/:id */

View File

@ -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

View File

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

View File

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

View File

@ -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() {
</ContentPageDescription>
</div>
<div className="flex items-center gap-3">
<PracticeActionButton
variant="outline"
className="rounded-[6px] border-brand-500 text-brand-500 "
pathOptions={modulePracticePathOptions}
parentLabel={displayModuleName}
>
<Calendar className="h-4 w-4" />
Add Practice
</PracticeActionButton>
{practices.length === 0 ? (
<PracticeActionButton
variant="outline"
className="rounded-[6px] border-brand-500 text-brand-500 "
pathOptions={modulePracticePathOptions}
parentLabel={displayModuleName}
>
<Calendar className="h-4 w-4" />
Add Practice
</PracticeActionButton>
) : null}
<Button
className="rounded-[6px] bg-brand-500 font-semibold hover:bg-brand-600"
onClick={() =>
@ -632,16 +646,19 @@ export function ModuleDetailPage() {
onEdit={() => openEditLesson(lesson)}
onDelete={() => setDeletingLesson(lesson)}
description={lesson.description}
onAddPractice={() =>
setLessonPracticeChoice({
isExamPrep: false,
level,
courseId,
moduleId,
lessonId: String(lesson.id),
lessonTitle: lesson.title,
backTo: "lesson",
})
onAddPractice={
lesson.has_practice
? undefined
: () =>
setLessonPracticeChoice({
isExamPrep: false,
level,
courseId,
moduleId,
lessonId: String(lesson.id),
lessonTitle: lesson.title,
backTo: "lesson",
})
}
onViewPractices={() =>
navigate(
@ -926,7 +943,7 @@ export function ModuleDetailPage() {
</div>
)}
{practiceToUnlink && moduleParent ? (
{practiceToUnlink && practiceUnlinkParent ? (
<Dialog
open={practiceToUnlink != null}
onOpenChange={(open) => {
@ -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."

View File

@ -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)
}

View File

@ -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)
}