From 620670cf0c2757bc095a1f93ff5e0caf92478dc9 Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Wed, 19 Aug 2026 13:12:51 +0300 Subject: [PATCH] back button fix --- src/components/navigation/PageBackLink.tsx | 12 +-- src/lib/navigateBack.ts | 10 +-- src/lib/practiceBulkActions.ts | 78 ++++++++++++---- src/lib/practiceParentUnlink.ts | 90 ++++++++++++++----- src/lib/practiceParents.ts | 2 +- .../content-management/CourseDetailPage.tsx | 76 ++++++++++++---- .../content-management/ModuleDetailPage.tsx | 31 ++++--- 7 files changed, 213 insertions(+), 86 deletions(-) diff --git a/src/components/navigation/PageBackLink.tsx b/src/components/navigation/PageBackLink.tsx index 321c3cb..f3760d2 100644 --- a/src/components/navigation/PageBackLink.tsx +++ b/src/components/navigation/PageBackLink.tsx @@ -1,6 +1,5 @@ import { ArrowLeft } from "lucide-react"; -import { useNavigate } from "react-router-dom"; -import { navigateBack } from "../../lib/navigateBack"; +import { Link } from "react-router-dom"; import { cn } from "../../lib/utils"; type PageBackLinkProps = { @@ -16,12 +15,9 @@ export function PageBackLink({ className, iconClassName, }: PageBackLinkProps) { - const navigate = useNavigate(); - return ( - + ); } diff --git a/src/lib/navigateBack.ts b/src/lib/navigateBack.ts index b21c0dd..620fca8 100644 --- a/src/lib/navigateBack.ts +++ b/src/lib/navigateBack.ts @@ -1,14 +1,6 @@ import type { NavigateFunction } from "react-router-dom"; -export function canNavigateBack(): boolean { - const idx = (window.history.state as { idx?: number } | null)?.idx; - return typeof idx === "number" && idx > 0; -} - +/** Navigate to an explicit back destination (never browser history). */ export function navigateBack(navigate: NavigateFunction, fallbackTo: string): void { - if (canNavigateBack()) { - navigate(-1); - return; - } navigate(fallbackTo); } diff --git a/src/lib/practiceBulkActions.ts b/src/lib/practiceBulkActions.ts index 14d0b15..2585b64 100644 --- a/src/lib/practiceBulkActions.ts +++ b/src/lib/practiceBulkActions.ts @@ -6,13 +6,15 @@ import type { ParentContextPractice, PracticeParent } from "../types/course.type import { parentsFromPractice } from "./practiceParents" import { isPracticeParentUnlinkNotLinkedError, + listCourseContextUnlinkParents, + listModuleContextUnlinkParents, resolveCourseContextUnlinkParent, resolveModuleContextUnlinkParent, unlinkPracticeFromParent, } from "./practiceParentUnlink" export type PracticeUnlinkContext = - | { scope: "course"; courseId: number; moduleIds: number[] } + | { scope: "course"; courseId: number; moduleIds: number[]; lessonIds?: number[] } | { scope: "module"; moduleId: number; lessonIds: number[] } | { scope: "lesson"; lessonId: number } | { scope: "unit"; unitId: number } @@ -34,6 +36,7 @@ export function resolvePracticeUnlinkParent( practice, context.courseId, context.moduleIds, + context.lessonIds ?? [], ) case "module": return resolveModuleContextUnlinkParent( @@ -65,6 +68,36 @@ export function resolvePracticeUnlinkParent( } } +export function listPracticeUnlinkParents( + practice: ParentContextPractice, + context: PracticeUnlinkContext, +): PracticeParent[] { + switch (context.scope) { + case "course": + return listCourseContextUnlinkParents( + practice, + context.courseId, + context.moduleIds, + context.lessonIds ?? [], + ) + case "module": + return listModuleContextUnlinkParents( + practice, + context.moduleId, + context.lessonIds, + ) + case "lesson": { + const parent = resolvePracticeUnlinkParent(practice, context) + return parent ? [parent] : [] + } + case "unit": + case "catalog_course": { + const parent = resolvePracticeUnlinkParent(practice, context) + return parent ? [parent] : [] + } + } +} + export async function bulkUnlinkPractices(opts: { practices: ParentContextPractice[] context: PracticeUnlinkContext @@ -77,25 +110,40 @@ export async function bulkUnlinkPractices(opts: { } for (const practice of opts.practices) { - const parent = resolvePracticeUnlinkParent(practice, opts.context) - if (!parent) { + const parents = listPracticeUnlinkParents(practice, opts.context) + if (parents.length === 0) { result.skipped += 1 continue } - try { - await unlinkPracticeFromParent({ - practiceId: practice.id, - parent, - isExamPrep: opts.isExamPrep, - }) - result.succeeded += 1 - } catch (error) { - if (isPracticeParentUnlinkNotLinkedError(error)) { - result.skipped += 1 - } else { - result.failed += 1 + + let practiceSucceeded = false + let practiceFailed = false + let practiceSkipped = false + + for (const parent of parents) { + try { + await unlinkPracticeFromParent({ + practiceId: practice.id, + parent, + isExamPrep: opts.isExamPrep, + }) + practiceSucceeded = true + } catch (error) { + if (isPracticeParentUnlinkNotLinkedError(error)) { + practiceSkipped = true + } else { + practiceFailed = true + } } } + + if (practiceFailed) { + result.failed += 1 + } else if (practiceSucceeded) { + result.succeeded += 1 + } else if (practiceSkipped) { + result.skipped += 1 + } } return result diff --git a/src/lib/practiceParentUnlink.ts b/src/lib/practiceParentUnlink.ts index dc7fdce..ed876f5 100644 --- a/src/lib/practiceParentUnlink.ts +++ b/src/lib/practiceParentUnlink.ts @@ -5,7 +5,7 @@ import { } from "../api/courses.api" import type { ParentContextPractice, PracticeParent } from "../types/course.types" import { normalizeParentContextPractice } from "./parentContextPractice" -import { formatPracticeParentLabel, parentsFromPractice } from "./practiceParents" +import { formatPracticeParentLabel, parentsFromPractice, dedupeParents } from "./practiceParents" export function unlinkParentConfirmMessage( locationLabel: string, @@ -51,41 +51,85 @@ export function resolveModuleContextUnlinkParent( 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 + listModuleContextUnlinkParents(practice, moduleId, lessonIdsInModule)[0] ?? + null ) } +/** All parent rows to remove when detaching a practice from a module practice tab. */ +export function listModuleContextUnlinkParents( + practice: ParentContextPractice, + moduleId: number, + lessonIdsInModule: number[], +): PracticeParent[] { + const parents = parentsFromPractice(practice) + const lessonIds = new Set(lessonIdsInModule.filter((id) => id > 0)) + const targets: PracticeParent[] = [] + + for (const parent of parents) { + if (parent.parent_kind === "COURSE") continue + if (parent.parent_kind === "MODULE" && parent.parent_id === moduleId) { + targets.push(parent) + } else if ( + parent.parent_kind === "LESSON" && + lessonIds.has(parent.parent_id) + ) { + targets.push(parent) + } + } + + return dedupeParents(targets) +} + /** Parent row to DELETE when removing a practice from a course's practice tab. */ export function resolveCourseContextUnlinkParent( practice: ParentContextPractice, courseId: number, moduleIdsInCourse: number[], + lessonIdsInCourse: number[] = [], ): PracticeParent | null { + return ( + listCourseContextUnlinkParents( + practice, + courseId, + moduleIdsInCourse, + lessonIdsInCourse, + )[0] ?? null + ) +} + +/** All parent rows to remove when detaching a practice from a course practice tab. */ +export function listCourseContextUnlinkParents( + practice: ParentContextPractice, + courseId: number, + moduleIdsInCourse: number[], + lessonIdsInCourse: number[] = [], +): PracticeParent[] { 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 + const lessonIds = new Set(lessonIdsInCourse.filter((id) => id > 0)) + const targets: PracticeParent[] = [] - return parents.find((p) => p.parent_kind === "LESSON") ?? null + for (const parent of parents) { + if (parent.parent_kind === "COURSE" && parent.parent_id === courseId) { + targets.push(parent) + } else if ( + parent.parent_kind === "MODULE" && + moduleIds.has(parent.parent_id) + ) { + targets.push(parent) + } else if ( + parent.parent_kind === "LESSON" && + (lessonIds.size === 0 || lessonIds.has(parent.parent_id)) + ) { + if (lessonIds.size > 0) { + targets.push(parent) + } + } + } + + return dedupeParents(targets) } export async function unlinkPracticeFromParent(opts: { diff --git a/src/lib/practiceParents.ts b/src/lib/practiceParents.ts index d4261f9..bda4667 100644 --- a/src/lib/practiceParents.ts +++ b/src/lib/practiceParents.ts @@ -18,7 +18,7 @@ export function normalizePracticeParent(row: unknown): PracticeParent | null { const kind = String(o.parent_kind ?? o.ParentKind ?? o.parentKind ?? "") .trim() .toUpperCase() - const parent_id = Number(o.parent_id ?? o.ParentId ?? o.parentId ?? o.id ?? o.ID) + const parent_id = Number(o.parent_id ?? o.ParentId ?? o.parentId) if (!isPracticeParentKind(kind)) return null if (!Number.isFinite(parent_id) || parent_id <= 0) return null return { parent_kind: kind, parent_id } diff --git a/src/pages/content-management/CourseDetailPage.tsx b/src/pages/content-management/CourseDetailPage.tsx index 935d08d..ea00369 100644 --- a/src/pages/content-management/CourseDetailPage.tsx +++ b/src/pages/content-management/CourseDetailPage.tsx @@ -30,6 +30,7 @@ import alertSrc from "../../assets/Alert.svg"; import { deleteTopLevelCourseModule, deleteParentLinkedPractice, + getModuleLessons, getPracticesByParentCourse, getProgramCourses, getTopLevelCourseModules, @@ -46,6 +47,7 @@ import type { PracticePublishStatus, ProgramCourseListItem, TopLevelCourseModuleItem, + TopLevelModuleLessonItem, } from "../../types/course.types"; import { ContentPublishStatusChip } from "./components/ContentPublishStatusChip"; import { ContentAccessTierChip } from "./components/ContentAccessTierChip"; @@ -68,7 +70,7 @@ import { DisplayValue } from "../../lib/displayValue" import { learnEnglishPracticeLimitHint, parentsFromPractice } from "../../lib/practiceParents" import { isPracticeParentUnlinkNotLinkedError, - resolveCourseContextUnlinkParent, + listCourseContextUnlinkParents, unlinkPracticeFromParent, } from "../../lib/practiceParentUnlink" import { SearchHighlight } from "../../components/SearchHighlight" @@ -211,6 +213,7 @@ export function CourseDetailPage() { useState(null); const [unlinkingPractice, setUnlinkingPractice] = useState(false); const [deletingPractice, setDeletingPractice] = useState(false); + const [courseLessonIds, setCourseLessonIds] = useState([]); const [publishStatusModuleId, setPublishStatusModuleId] = useState< number | null >(null); @@ -415,29 +418,68 @@ export function CourseDetailPage() { [modules], ); - const practiceUnlinkParent = useMemo((): PracticeParent | null => { - if (!practiceToUnlink || !Number.isFinite(courseIdNum) || courseIdNum < 1) { - return null; + const loadCourseLessonIds = useCallback(async () => { + if (courseModuleIds.length === 0) { + setCourseLessonIds([]); + return; } - return resolveCourseContextUnlinkParent( + try { + const lessonLists = await Promise.all( + courseModuleIds.map((moduleId) => + fetchAllOffsetPages(async (offset, limit) => + offsetPageFromListEnvelope( + await getModuleLessons(moduleId, { limit, offset }), + "lessons", + ), + ), + ), + ); + setCourseLessonIds( + lessonLists + .flatMap((list) => list.map((lesson) => lesson.id)) + .filter((id) => id > 0), + ); + } catch { + setCourseLessonIds([]); + } + }, [courseModuleIds]); + + useEffect(() => { + if (activeTab !== "practice") return; + void loadCourseLessonIds(); + }, [activeTab, loadCourseLessonIds]); + + const practiceUnlinkParents = useMemo((): PracticeParent[] => { + if (!practiceToUnlink || !Number.isFinite(courseIdNum) || courseIdNum < 1) { + return []; + } + return listCourseContextUnlinkParents( practiceToUnlink, courseIdNum, courseModuleIds, + courseLessonIds, ); - }, [courseIdNum, courseModuleIds, practiceToUnlink]); + }, [ + courseIdNum, + courseLessonIds, + courseModuleIds, + practiceToUnlink, + ]); const confirmUnlinkPractice = async () => { - if (!practiceToUnlink || !practiceUnlinkParent) { + if (!practiceToUnlink || practiceUnlinkParents.length === 0) { toast.error("This practice is not linked to this course."); return; } setUnlinkingPractice(true); try { - await unlinkPracticeFromParent({ - practiceId: practiceToUnlink.id, - parent: practiceUnlinkParent, - isExamPrep: false, - }); + for (const parent of practiceUnlinkParents) { + await unlinkPracticeFromParent({ + practiceId: practiceToUnlink.id, + parent, + isExamPrep: false, + }); + } toast.success(`Practice removed from ${displayTitle}`); setPracticeToUnlink(null); await loadCoursePractices(); @@ -946,6 +988,7 @@ export function CourseDetailPage() { scope: "course", courseId: courseIdNum, moduleIds: courseModuleIds, + lessonIds: courseLessonIds, }} publishStatusUpdatingId={publishStatusPracticeId} onReload={loadCoursePractices} @@ -1003,7 +1046,7 @@ export function CourseDetailPage() { )} - {practiceToUnlink && practiceUnlinkParent ? ( + {practiceToUnlink && practiceUnlinkParents.length > 0 ? ( { @@ -1024,9 +1067,10 @@ export function CourseDetailPage() { . {parentsFromPractice(practiceToUnlink).filter( (p) => - !( - p.parent_kind === practiceUnlinkParent.parent_kind && - p.parent_id === practiceUnlinkParent.parent_id + !practiceUnlinkParents.some( + (target) => + target.parent_kind === p.parent_kind && + target.parent_id === p.parent_id, ), ).length === 0 ? " The practice will be unlinked until re-attached." diff --git a/src/pages/content-management/ModuleDetailPage.tsx b/src/pages/content-management/ModuleDetailPage.tsx index 0e8d689..c1dc658 100644 --- a/src/pages/content-management/ModuleDetailPage.tsx +++ b/src/pages/content-management/ModuleDetailPage.tsx @@ -29,7 +29,7 @@ import { import { learnEnglishPracticeLimitHint, parentsFromPractice } from "../../lib/practiceParents"; import { isPracticeParentUnlinkNotLinkedError, - resolveModuleContextUnlinkParent, + listModuleContextUnlinkParents, unlinkPracticeFromParent, } from "../../lib/practiceParentUnlink"; import { Button } from "../../components/ui/button"; @@ -361,10 +361,10 @@ export function ModuleDetailPage() { [lessons], ); - const practiceUnlinkParent = useMemo((): PracticeParent | null => { + const practiceUnlinkParents = useMemo((): PracticeParent[] => { const mid = Number(moduleId); - if (!practiceToUnlink || !Number.isFinite(mid) || mid < 1) return null; - return resolveModuleContextUnlinkParent( + if (!practiceToUnlink || !Number.isFinite(mid) || mid < 1) return []; + return listModuleContextUnlinkParents( practiceToUnlink, mid, moduleLessonIds, @@ -372,17 +372,19 @@ export function ModuleDetailPage() { }, [moduleId, moduleLessonIds, practiceToUnlink]); const confirmUnlinkPractice = async () => { - if (!practiceToUnlink || !practiceUnlinkParent) { + if (!practiceToUnlink || practiceUnlinkParents.length === 0) { toast.error("This practice is not linked to this module."); return; } setUnlinkingPractice(true); try { - await unlinkPracticeFromParent({ - practiceId: practiceToUnlink.id, - parent: practiceUnlinkParent, - isExamPrep: false, - }); + for (const parent of practiceUnlinkParents) { + await unlinkPracticeFromParent({ + practiceId: practiceToUnlink.id, + parent, + isExamPrep: false, + }); + } toast.success(`Practice removed from ${displayModuleName}`); setPracticeToUnlink(null); await loadModulePractices(); @@ -964,7 +966,7 @@ export function ModuleDetailPage() { )} - {practiceToUnlink && practiceUnlinkParent ? ( + {practiceToUnlink && practiceUnlinkParents.length > 0 ? ( { @@ -985,9 +987,10 @@ export function ModuleDetailPage() { . {parentsFromPractice(practiceToUnlink).filter( (p) => - !( - p.parent_kind === practiceUnlinkParent.parent_kind && - p.parent_id === practiceUnlinkParent.parent_id + !practiceUnlinkParents.some( + (target) => + target.parent_kind === p.parent_kind && + target.parent_id === p.parent_id, ), ).length === 0 ? " The practice will be unlinked until re-attached."