From 4abc20d1663db4603a3c92cb01ca76f2734608a0 Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Thu, 13 Aug 2026 12:03:04 +0300 Subject: [PATCH] Keep practice parent links on edit and load full exam-prep practice lists. Co-authored-by: Cursor --- src/lib/practiceParents.ts | 5 ++- .../content-management/EditPracticeFlow.tsx | 39 ++++++++++++++++++- .../LessonPracticesPage.tsx | 4 +- .../CatalogCoursePracticesPanel.tsx | 9 ++--- .../components/UnitPracticesPanel.tsx | 9 ++--- 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/lib/practiceParents.ts b/src/lib/practiceParents.ts index 3e0b08b..42de60a 100644 --- a/src/lib/practiceParents.ts +++ b/src/lib/practiceParents.ts @@ -45,8 +45,9 @@ export function parentsFromPractice(practice: { parent_kind?: string parent_id?: number }): PracticeParent[] { - if (practice.parents == null) return [] - if (practice.parents.length > 0) return dedupeParents(practice.parents) + if (Array.isArray(practice.parents) && practice.parents.length > 0) { + return dedupeParents(practice.parents) + } const kind = practice.parent_kind?.trim().toUpperCase() ?? "" const parent_id = practice.parent_id if (isPracticeParentKind(kind) && parent_id != null && parent_id > 0) { diff --git a/src/pages/content-management/EditPracticeFlow.tsx b/src/pages/content-management/EditPracticeFlow.tsx index 1762526..3614f86 100644 --- a/src/pages/content-management/EditPracticeFlow.tsx +++ b/src/pages/content-management/EditPracticeFlow.tsx @@ -366,7 +366,29 @@ export function EditPracticeFlow() { if (cancelled) return; const loadedPersona = mapped.personaId != null ? String(mapped.personaId) : null; - setFormData(mapped.formData); + const lid = lessonId ? Number(lessonId) : NaN; + const mid = moduleId ? Number(moduleId) : NaN; + const uid = unitId ? Number(unitId) : NaN; + const cid = courseId ? Number(courseId) : NaN; + const routeParent: PracticeParent | null = Number.isFinite(lid) && lid > 0 + ? { parent_kind: "LESSON", parent_id: lid } + : isModuleContext && !isExamPrep && Number.isFinite(mid) && mid > 0 + ? { parent_kind: "MODULE", parent_id: mid } + : isUnitContext && isExamPrep && Number.isFinite(uid) && uid > 0 + ? { parent_kind: "UNIT", parent_id: uid } + : isCourseContext && Number.isFinite(cid) && cid > 0 + ? { + parent_kind: isExamPrep ? "CATALOG_COURSE" : "COURSE", + parent_id: cid, + } + : null; + const formParents = + mapped.parents.length > 0 + ? mapped.parents + : routeParent + ? [routeParent] + : mapped.formData.parents; + setFormData({ ...mapped.formData, parents: formParents }); setPreservedQuestionSet(mapped.preservedQuestionSet); initialParentsRef.current = mapped.parents; setLoadedPersonaId(mapped.personaId); @@ -389,7 +411,20 @@ export function EditPracticeFlow() { return () => { cancelled = true; }; - }, [validPracticeId, practiceId, isExamPrep, typeDefinitions, definitionsLoading]); + }, [ + validPracticeId, + practiceId, + isExamPrep, + typeDefinitions, + definitionsLoading, + lessonId, + moduleId, + unitId, + courseId, + isModuleContext, + isUnitContext, + isCourseContext, + ]); useEffect(() => { if (loadedPersonaId == null || personasLoading) return; diff --git a/src/pages/content-management/LessonPracticesPage.tsx b/src/pages/content-management/LessonPracticesPage.tsx index 70ae624..e5a7f9b 100644 --- a/src/pages/content-management/LessonPracticesPage.tsx +++ b/src/pages/content-management/LessonPracticesPage.tsx @@ -341,11 +341,11 @@ export function LessonPracticesPage() { : list.length, ); } - } catch { + } catch (error) { setPractices([]); setTotalCount(0); setLoadError("Could not load practices for this lesson."); - notifyApiError(err, "Failed to load practices"); + notifyApiError(error, "Failed to load practices"); } finally { setLoading(false); } diff --git a/src/pages/content-management/components/CatalogCoursePracticesPanel.tsx b/src/pages/content-management/components/CatalogCoursePracticesPanel.tsx index 5ce872f..108a450 100644 --- a/src/pages/content-management/components/CatalogCoursePracticesPanel.tsx +++ b/src/pages/content-management/components/CatalogCoursePracticesPanel.tsx @@ -85,18 +85,15 @@ export function CatalogCoursePracticesPanel({ setLoading(true) setLoadError(null) try { - const res = await getExamPrepCatalogCoursePractices(catalogCourseId, { - limit: 20, - offset: 0, - }) + const res = await getExamPrepCatalogCoursePractices(catalogCourseId) const { practices: list, totalCount: total } = extractPracticesPage(res) setPractices(list) setTotalCount(total) - } catch { + } catch (error) { setPractices([]) setTotalCount(0) setLoadError("Could not load practices for this catalog course.") - notifyApiError(err, "Failed to load practices") + notifyApiError(error, "Failed to load practices") } finally { setLoading(false) } diff --git a/src/pages/content-management/components/UnitPracticesPanel.tsx b/src/pages/content-management/components/UnitPracticesPanel.tsx index b814524..b1cfff5 100644 --- a/src/pages/content-management/components/UnitPracticesPanel.tsx +++ b/src/pages/content-management/components/UnitPracticesPanel.tsx @@ -87,18 +87,15 @@ export function UnitPracticesPanel({ setLoading(true) setLoadError(null) try { - const res = await getExamPrepUnitPractices(unitId, { - limit: 20, - offset: 0, - }) + const res = await getExamPrepUnitPractices(unitId) const { practices: list, totalCount: total } = extractPracticesPage(res) setPractices(list) setTotalCount(total) - } catch { + } catch (error) { setPractices([]) setTotalCount(0) setLoadError("Could not load practices for this unit.") - notifyApiError(err, "Failed to load practices") + notifyApiError(error, "Failed to load practices") } finally { setLoading(false) }