Add practice unlink/delete on course and module tabs and disable Add Practice when full.

Show disabled Add Practice controls with tooltips when a parent already has a practice, and wire course/module practice cards to unlink or permanently delete from the overflow menu.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-08-18 15:48:19 +03:00
parent 2fd5a96ea5
commit 483ec8c15e
6 changed files with 307 additions and 54 deletions

View File

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

View File

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

View File

@ -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<ParentContextPractice | null>(null);
const [practiceToDelete, setPracticeToDelete] =
useState<ParentContextPractice | null>(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() {
</ContentPageDescription>
</div>
<div className="flex items-center gap-4">
{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}
<PracticeActionButton
variant="outline"
className="rounded-[6px] border-brand-500 text-brand-500 "
pathOptions={{
isExamPrep: false,
level: programIdParam,
courseId: courseIdParam,
backTo: "modules",
}}
parentLabel={displayTitle}
disabled={practices.length > 0}
title={
practices.length > 0 ? learnEnglishPracticeLimitHint : undefined
}
>
<Calendar className="h-4 w-4" />
Add Practice
</PracticeActionButton>
<Button
className="rounded-[6px] bg-brand-500 font-semibold hover:bg-brand-600"
onClick={() => setIsAddModuleOpen(true)}
@ -884,6 +959,8 @@ export function CourseDetailPage() {
onSaveAsDraft={() =>
void handlePracticePublishStatus(practice.id, "DRAFT")
}
onUnlink={() => setPracticeToUnlink(practice)}
onDelete={() => setPracticeToDelete(practice)}
/>
))}
</div>
@ -925,6 +1002,90 @@ export function CourseDetailPage() {
</div>
)}
{practiceToUnlink && practiceUnlinkParent ? (
<Dialog
open={practiceToUnlink != null}
onOpenChange={(open) => {
if (!open && !unlinkingPractice) setPracticeToUnlink(null);
}}
>
<DialogContent className="max-w-md rounded-2xl">
<DialogHeader>
<DialogTitle>Remove from this course?</DialogTitle>
<DialogDescription>
<span className="font-semibold text-grayScale-800">
{practiceToUnlink.title}
</span>{" "}
will be detached from{" "}
<span className="font-semibold text-grayScale-800">
{displayTitle}
</span>
.
{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."}
</DialogDescription>
</DialogHeader>
<DialogFooter className="gap-2 border-t border-grayScale-100 px-6 py-4 sm:justify-end">
<Button
variant="outline"
onClick={() => setPracticeToUnlink(null)}
disabled={unlinkingPractice}
>
Cancel
</Button>
<Button
onClick={() => void confirmUnlinkPractice()}
disabled={unlinkingPractice}
>
{unlinkingPractice ? "Removing…" : "Remove from course"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
) : null}
<Dialog
open={practiceToDelete != null}
onOpenChange={(open) => {
if (!open && !deletingPractice) setPracticeToDelete(null);
}}
>
<DialogContent className="max-w-md rounded-2xl">
<DialogHeader>
<DialogTitle>Delete this practice permanently?</DialogTitle>
<DialogDescription>
<span className="font-semibold text-grayScale-800">
{practiceToDelete?.title}
</span>{" "}
and all of its questions will be deleted. This cannot be undone.
</DialogDescription>
</DialogHeader>
<DialogFooter className="gap-2 border-t border-grayScale-100 px-6 py-4 sm:justify-end">
<Button
variant="outline"
onClick={() => setPracticeToDelete(null)}
disabled={deletingPractice}
>
Cancel
</Button>
<Button
className="bg-red-500 hover:bg-red-600"
onClick={() => void confirmDeletePractice()}
disabled={deletingPractice}
>
{deletingPractice ? "Deleting…" : "Delete practice"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
{deletingModule && (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 backdrop-blur-sm">
<div className="mx-4 w-full max-w-sm animate-in fade-in zoom-in-95 rounded-2xl bg-white shadow-2xl">

View File

@ -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() {
</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.
{learnEnglishPracticeLimitHint}
</p>
) : null}
{!loading && !loadError ? (
@ -339,16 +341,20 @@ export function LessonPracticesPage() {
/>
Refresh
</Button>
{!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}
<PracticeActionButton
className="rounded-[6px] bg-brand-500 font-semibold hover:bg-brand-600"
pathOptions={practicePathOptions}
parentLabel={displayTitle}
disabled={!isExamPrep && practices.length >= 1}
title={
!isExamPrep && practices.length >= 1
? learnEnglishPracticeLimitHint
: undefined
}
>
<Calendar className="h-4 w-4" />
Add Practice
</PracticeActionButton>
</div>
</div>

View File

@ -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<ParentContextPractice | null>(null);
const [practiceToDelete, setPracticeToDelete] =
useState<ParentContextPractice | null>(null);
const [unlinkingPractice, setUnlinkingPractice] = useState(false);
const [deletingPractice, setDeletingPractice] = useState(false);
const [loadedModuleName, setLoadedModuleName] = useState<string | null>(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() {
</ContentPageDescription>
</div>
<div className="flex items-center gap-3">
{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}
<PracticeActionButton
variant="outline"
className="rounded-[6px] border-brand-500 text-brand-500 "
pathOptions={modulePracticePathOptions}
parentLabel={displayModuleName}
disabled={practices.length > 0}
title={
practices.length > 0 ? learnEnglishPracticeLimitHint : undefined
}
>
<Calendar className="h-4 w-4" />
Add Practice
</PracticeActionButton>
<Button
className="rounded-[6px] bg-brand-500 font-semibold hover:bg-brand-600"
onClick={() =>
@ -646,20 +667,18 @@ export function ModuleDetailPage() {
onEdit={() => openEditLesson(lesson)}
onDelete={() => setDeletingLesson(lesson)}
description={lesson.description}
onAddPractice={
lesson.has_practice
? undefined
: () =>
setLessonPracticeChoice({
isExamPrep: false,
level,
courseId,
moduleId,
lessonId: String(lesson.id),
lessonTitle: lesson.title,
backTo: "lesson",
})
onAddPractice={() =>
setLessonPracticeChoice({
isExamPrep: false,
level,
courseId,
moduleId,
lessonId: String(lesson.id),
lessonTitle: lesson.title,
backTo: "lesson",
})
}
addPracticeDisabled={!!lesson.has_practice}
onViewPractices={() =>
navigate(
`/new-content/learn-english/${level}/courses/${courseId}/modules/${moduleId}/lessons/${lesson.id}/practices?lessonTitle=${encodeURIComponent(lesson.title ?? "")}`,
@ -749,6 +768,7 @@ export function ModuleDetailPage() {
void handlePracticePublishStatus(practice.id, "DRAFT")
}
onUnlink={() => setPracticeToUnlink(practice)}
onDelete={() => setPracticeToDelete(practice)}
/>
))}
</div>
@ -992,6 +1012,41 @@ export function ModuleDetailPage() {
</Dialog>
) : null}
<Dialog
open={practiceToDelete != null}
onOpenChange={(open) => {
if (!open && !deletingPractice) setPracticeToDelete(null);
}}
>
<DialogContent className="max-w-md rounded-2xl">
<DialogHeader>
<DialogTitle>Delete this practice permanently?</DialogTitle>
<DialogDescription>
<span className="font-semibold text-grayScale-800">
{practiceToDelete?.title}
</span>{" "}
and all of its questions will be deleted. This cannot be undone.
</DialogDescription>
</DialogHeader>
<DialogFooter className="gap-2 border-t border-grayScale-100 px-6 py-4 sm:justify-end">
<Button
variant="outline"
onClick={() => setPracticeToDelete(null)}
disabled={deletingPractice}
>
Cancel
</Button>
<Button
className="bg-red-500 hover:bg-red-600"
onClick={() => void confirmDeletePractice()}
disabled={deletingPractice}
>
{deletingPractice ? "Deleting…" : "Delete practice"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
{lessonPracticeChoice && lessonPracticeChoicePaths ? (
<PracticeActionChoiceDialog
open={lessonPracticeChoice != null}

View File

@ -16,6 +16,7 @@ import {
DialogTitle,
} from "../../../components/ui/dialog";
import { isAdminOrSuperAdminRole } from "../../../lib/sessionRole";
import { learnEnglishPracticeLimitHint } from "../../../lib/practiceParents";
import { cn } from "../../../lib/utils";
import { SearchHighlight } from "../../../components/SearchHighlight";
import {
@ -85,6 +86,8 @@ interface VideoCardProps {
onDelete?: () => 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();
}}
>