diff --git a/src/api/courses.api.ts b/src/api/courses.api.ts index adcf783..7583a29 100644 --- a/src/api/courses.api.ts +++ b/src/api/courses.api.ts @@ -101,6 +101,12 @@ import type { PublishExamPrepModuleLessonRequest, CreateExamPrepLessonPracticeRequest, CreateExamPrepLessonPracticeResponse, + CreateExamPrepCatalogCoursePracticeRequest, + CreateExamPrepCatalogCoursePracticeResponse, + CreateExamPrepUnitPracticeRequest, + CreateExamPrepUnitPracticeResponse, + GetExamPrepUnitPracticesResponse, + GetExamPrepCatalogCoursePracticesResponse, GetExamPrepLessonPracticesResponse, GetExamPrepModuleLessonsResponse, GetTopLevelModuleLessonsResponse, @@ -645,6 +651,46 @@ export const setExamPrepModuleLessonAccessTier = ( export const deleteExamPrepModuleLesson = (lessonId: number) => http.delete(`/exam-prep/lessons/${lessonId}`) +/** POST /exam-prep/catalog-courses/:catalogCourseId/practices */ +export const createExamPrepCatalogCoursePractice = ( + catalogCourseId: number, + data: CreateExamPrepCatalogCoursePracticeRequest, +) => + http.post( + `/exam-prep/catalog-courses/${catalogCourseId}/practices`, + data, + ) + +/** GET /exam-prep/catalog-courses/:catalogCourseId/practices */ +export const getExamPrepCatalogCoursePractices = ( + catalogCourseId: number, + params?: { limit?: number; offset?: number }, +) => + http.get( + `/exam-prep/catalog-courses/${catalogCourseId}/practices`, + { params }, + ) + +/** POST /exam-prep/units/:unitId/practices */ +export const createExamPrepUnitPractice = ( + unitId: number, + data: CreateExamPrepUnitPracticeRequest, +) => + http.post( + `/exam-prep/units/${unitId}/practices`, + data, + ) + +/** GET /exam-prep/units/:unitId/practices */ +export const getExamPrepUnitPractices = ( + unitId: number, + params?: { limit?: number; offset?: number }, +) => + http.get( + `/exam-prep/units/${unitId}/practices`, + { params }, + ) + /** POST /exam-prep/lessons/:lessonId/practices */ export const createExamPrepLessonPractice = ( lessonId: number, diff --git a/src/app/AppRoutes.tsx b/src/app/AppRoutes.tsx index 5f2fdb1..3d38170 100644 --- a/src/app/AppRoutes.tsx +++ b/src/app/AppRoutes.tsx @@ -221,6 +221,14 @@ export function AppRoutes() { path="/new-content/courses/:programType/:courseId/attach-practice" element={} /> + } + /> + } + /> } @@ -241,6 +249,14 @@ export function AppRoutes() { path="/new-content/courses/:programType/:courseId/:unitId/:moduleId" element={} /> + } + /> + } + /> } diff --git a/src/lib/practiceContentPaths.ts b/src/lib/practiceContentPaths.ts index 556017e..e21a52b 100644 --- a/src/lib/practiceContentPaths.ts +++ b/src/lib/practiceContentPaths.ts @@ -24,24 +24,32 @@ export function resolvePracticeParentFromPathOptions( } } + const courseId = options.courseId?.trim() + if (options.isExamPrep && courseId && !options.unitId?.trim() && !options.moduleId?.trim() && !lessonId) { + const id = Number(courseId) + if (Number.isFinite(id) && id > 0) { + return { parent_kind: "CATALOG_COURSE", parent_id: id } + } + } + + const unitId = options.unitId?.trim() + if (options.isExamPrep && unitId && !options.moduleId?.trim() && !lessonId) { + const id = Number(unitId) + if (Number.isFinite(id) && id > 0) { + return { parent_kind: "UNIT", parent_id: id } + } + } + const backTo = options.backTo?.trim() const moduleId = options.moduleId?.trim() - if (backTo === "module" && moduleId) { + if (backTo === "module" && moduleId && !options.isExamPrep) { const id = Number(moduleId) if (Number.isFinite(id) && id > 0) { return { parent_kind: "MODULE", parent_id: id } } } - if (options.isExamPrep && moduleId && !lessonId) { - const id = Number(moduleId) - if (Number.isFinite(id) && id > 0) { - return { parent_kind: "MODULE", parent_id: id } - } - } - - const courseId = options.courseId?.trim() - if ((backTo === "modules" || backTo === "courses") && courseId) { + if ((backTo === "modules" || backTo === "courses") && courseId && !options.isExamPrep) { const id = Number(courseId) if (Number.isFinite(id) && id > 0) { return { parent_kind: "COURSE", parent_id: id } @@ -63,6 +71,7 @@ export function resolvePracticeParentSummaryFromPathOptions( return `Lesson #${options.lessonId.trim()}${lessonTitle ? ` — ${lessonTitle}` : ""}` } if (options.moduleId?.trim()) return `Module #${options.moduleId.trim()}` + if (options.unitId?.trim()) return `Unit #${options.unitId.trim()}` if (options.courseId?.trim()) return `Course #${options.courseId.trim()}` return "selected content" } @@ -71,6 +80,7 @@ function buildPracticeQuery(options: PracticeContentPathOptions): string { const params = new URLSearchParams() if (options.backTo?.trim()) params.set("backTo", options.backTo.trim()) if (options.courseId?.trim()) params.set("courseId", options.courseId.trim()) + if (options.unitId?.trim()) params.set("unitId", options.unitId.trim()) if (options.moduleId?.trim()) params.set("moduleId", options.moduleId.trim()) if (options.lessonId?.trim()) params.set("lessonId", options.lessonId.trim()) if (options.lessonTitle?.trim()) { @@ -89,6 +99,9 @@ function practiceBasePath(options: PracticeContentPathOptions): string { if (programType && courseId && unitId && moduleId) { return `/new-content/courses/${programType}/${courseId}/${unitId}/${moduleId}` } + if (programType && courseId && unitId) { + return `/new-content/courses/${programType}/${courseId}/${unitId}` + } if (programType && courseId) { return `/new-content/courses/${programType}/${courseId}` } diff --git a/src/lib/practiceCreationOrchestrator.ts b/src/lib/practiceCreationOrchestrator.ts index 2ec861a..a104f1b 100644 --- a/src/lib/practiceCreationOrchestrator.ts +++ b/src/lib/practiceCreationOrchestrator.ts @@ -1,7 +1,9 @@ import type { AxiosResponse } from "axios" import { addQuestionToSet, + createExamPrepCatalogCoursePractice, createExamPrepLessonPractice, + createExamPrepUnitPractice, createParentLinkedPractice, createQuestion, createQuestionSet, @@ -44,6 +46,10 @@ export interface PracticeCreationInput { definitions: QuestionTypeDefinition[] /** English proficiency lesson practices use POST /exam-prep/lessons/:id/practices. */ examPrepLessonId?: number + /** English proficiency catalog course practices use POST /exam-prep/catalog-courses/:id/practices. */ + examPrepCatalogCourseId?: number + /** English proficiency unit practices use POST /exam-prep/units/:id/practices. */ + examPrepUnitId?: number } function extractCreatedResourceId( @@ -190,7 +196,27 @@ export async function executePracticeCreation( quick_tips: opts.quickTips.trim(), publish_status: opts.status, }) - : await createParentLinkedPractice({ + : opts.examPrepCatalogCourseId + ? await createExamPrepCatalogCoursePractice(opts.examPrepCatalogCourseId, { + title: opts.practiceTitle.trim(), + story_description: opts.storyDescription.trim(), + story_image: opts.storyImage.trim(), + persona_id: opts.personaId, + question_set_id: setId, + quick_tips: opts.quickTips.trim(), + publish_status: opts.status, + }) + : opts.examPrepUnitId + ? await createExamPrepUnitPractice(opts.examPrepUnitId, { + title: opts.practiceTitle.trim(), + story_description: opts.storyDescription.trim(), + story_image: opts.storyImage.trim(), + persona_id: opts.personaId, + question_set_id: setId, + quick_tips: opts.quickTips.trim(), + publish_status: opts.status, + }) + : await createParentLinkedPractice({ parents: createParents, title: opts.practiceTitle.trim(), story_description: opts.storyDescription.trim(), diff --git a/src/lib/practiceParents.ts b/src/lib/practiceParents.ts index 20970d7..1056222 100644 --- a/src/lib/practiceParents.ts +++ b/src/lib/practiceParents.ts @@ -1,6 +1,12 @@ import type { PracticeParent, PracticeParentKind } from "../types/course.types" -const PARENT_KINDS: PracticeParentKind[] = ["COURSE", "MODULE", "LESSON"] +const PARENT_KINDS: PracticeParentKind[] = [ + "COURSE", + "MODULE", + "LESSON", + "CATALOG_COURSE", + "UNIT", +] export function isPracticeParentKind(value: string): value is PracticeParentKind { return PARENT_KINDS.includes(value.toUpperCase() as PracticeParentKind) @@ -88,7 +94,20 @@ export function validatePracticeParents( } export function formatPracticeParentLabel(parent: PracticeParent): string { - return `${parent.parent_kind} #${parent.parent_id}` + switch (parent.parent_kind) { + case "CATALOG_COURSE": + return `Catalog course #${parent.parent_id}` + case "UNIT": + return `Unit #${parent.parent_id}` + case "COURSE": + return `Course #${parent.parent_id}` + case "MODULE": + return `Module #${parent.parent_id}` + case "LESSON": + return `Lesson #${parent.parent_id}` + default: + return `${parent.parent_kind} #${parent.parent_id}` + } } export function formatPracticeParentsSummary(parents: PracticeParent[]): string { diff --git a/src/pages/content-management/AddPracticeFlow.tsx b/src/pages/content-management/AddPracticeFlow.tsx index 713f709..9fb010a 100644 --- a/src/pages/content-management/AddPracticeFlow.tsx +++ b/src/pages/content-management/AddPracticeFlow.tsx @@ -70,9 +70,10 @@ export function AddPracticeFlow() { const effectiveBackTo = useMemo(() => { if (backToParam?.trim()) return backToParam.trim(); if (isExamPrep && routeModuleId) return "module"; + if (isExamPrep && routeUnitId && !routeModuleId) return "unit"; if (isExamPrep && routeCourseId) return "courses"; return null; - }, [backToParam, isExamPrep, routeModuleId, routeCourseId]); + }, [backToParam, isExamPrep, routeModuleId, routeUnitId, routeCourseId]); const courseId = isExamPrep ? routeCourseId ?? searchParams.get("courseId") @@ -93,6 +94,7 @@ export function AddPracticeFlow() { })(); const isModuleContext = effectiveBackTo === "module"; + const isUnitContext = effectiveBackTo === "unit"; const isCourseContext = effectiveBackTo === "modules" || effectiveBackTo === "courses"; const isLessonPractice = useMemo(() => { @@ -109,26 +111,37 @@ export function AddPracticeFlow() { const lid = lessonId ? Number(lessonId) : NaN; if (Number.isFinite(lid) && lid > 0) return { kind: "LESSON", id: lid }; const mid = moduleId ? Number(moduleId) : NaN; - if (isModuleContext && Number.isFinite(mid) && mid > 0) + if (isModuleContext && !isExamPrep && Number.isFinite(mid) && mid > 0) return { kind: "MODULE", id: mid }; + const uid = unitId ? Number(unitId) : NaN; + if (isUnitContext && isExamPrep && Number.isFinite(uid) && uid > 0) + return { kind: "UNIT", id: uid }; const cid = courseId ? Number(courseId) : NaN; - if (isCourseContext && Number.isFinite(cid) && cid > 0) - return { kind: "COURSE", id: cid }; + if (isCourseContext && Number.isFinite(cid) && cid > 0) { + return { kind: isExamPrep ? "CATALOG_COURSE" : "COURSE", id: cid }; + } return null; - }, [lessonId, moduleId, courseId, isModuleContext, isCourseContext]); + }, [lessonId, moduleId, unitId, courseId, isModuleContext, isUnitContext, isCourseContext, isExamPrep]); const parentSummary = useMemo(() => { if (lessonId) return `Lesson #${lessonId}${lessonTitleDisplay ? ` — ${lessonTitleDisplay}` : ""}`; if (isModuleContext && moduleId) return `Module #${moduleId}`; - if (isCourseContext && courseId) return `Course #${courseId}`; + if (isUnitContext && unitId) return `Unit #${unitId}`; + if (isCourseContext && courseId) { + return isExamPrep + ? `Catalog course #${courseId}` + : `Course #${courseId}`; + } return null; }, [ lessonId, lessonTitleDisplay, isModuleContext, + isUnitContext, isCourseContext, moduleId, + unitId, courseId, ]); @@ -144,7 +157,9 @@ export function AddPracticeFlow() { ? "Back to Question Types" : effectiveBackTo === "module" ? "Back to Module" - : effectiveBackTo === "modules" + : effectiveBackTo === "unit" + ? "Back to Unit" + : effectiveBackTo === "modules" ? "Back to Modules" : effectiveBackTo === "courses" ? "Back to Course" @@ -164,6 +179,9 @@ export function AddPracticeFlow() { ) { return `/new-content/courses/${programType}/${courseId}/${unitId}/${moduleId}`; } + if (effectiveBackTo === "unit" && programType && courseId && unitId) { + return `/new-content/courses/${programType}/${courseId}/${unitId}`; + } if (effectiveBackTo === "courses" && programType && courseId) { return `/new-content/courses/${programType}/${courseId}`; } @@ -402,6 +420,22 @@ export function AddPracticeFlow() { parentContext != null && Number.isFinite(parentContext.id); + const useExamPrepCatalogCourseApi = + isExamPrep && + isCourseContext && + !isLessonPractice && + parentContext?.kind === "CATALOG_COURSE" && + parentContext != null && + Number.isFinite(parentContext.id); + + const useExamPrepUnitApi = + isExamPrep && + isUnitContext && + !isLessonPractice && + parentContext?.kind === "UNIT" && + parentContext != null && + Number.isFinite(parentContext.id); + setSubmitting(true); try { await executePracticeCreation({ @@ -409,6 +443,10 @@ export function AddPracticeFlow() { parentKind: parentContext?.kind, parentId: parentContext?.id, examPrepLessonId: useExamPrepLessonApi ? parentContext!.id : undefined, + examPrepCatalogCourseId: useExamPrepCatalogCourseApi + ? parentContext!.id + : undefined, + examPrepUnitId: useExamPrepUnitApi ? parentContext!.id : undefined, status, questionSetTitle: isLearnEnglishLessonPractice ? lessonDefaultTitle diff --git a/src/pages/content-management/CourseManagementPage.tsx b/src/pages/content-management/CourseManagementPage.tsx index f1a8256..be634ee 100644 --- a/src/pages/content-management/CourseManagementPage.tsx +++ b/src/pages/content-management/CourseManagementPage.tsx @@ -11,9 +11,13 @@ import { ChevronRight, ArrowRight, X, + FileText, } from "lucide-react"; import { Button } from "../../components/ui/button"; import { Card } from "../../components/ui/card"; +import { PracticeActionButton } from "./components/PracticeActionButton"; +import { CatalogCoursePracticesPanel } from "./components/CatalogCoursePracticesPanel"; +import { cn } from "../../lib/utils"; import { Dialog, DialogContent, @@ -102,6 +106,17 @@ export function CourseManagementPage() { >(null); const [catalogCoursePublishStatusUpdating, setCatalogCoursePublishStatusUpdating] = useState(false); + const [activeTab, setActiveTab] = useState<"units" | "practices">("units"); + + const catalogCoursePracticePathOptions = useMemo( + () => ({ + isExamPrep: true as const, + programType, + courseId: String(catalogCourseId), + backTo: "courses", + }), + [programType, catalogCourseId], + ); const filteredUnits = useMemo( () => @@ -550,12 +565,12 @@ export function CourseManagementPage() { ) : (

- Manage units and modules inside {courseDisplayName} + Manage units and practices inside {courseDisplayName}

)} -
+
{ @@ -704,6 +719,45 @@ export function CourseManagementPage() {
+ + + Attach Practice + +
+ + + {/* Tabs */} +
+
+ +
@@ -722,8 +776,16 @@ export function CourseManagementPage() { - {/* Grid of Units */} + {/* Grid of Units / Practices */}
+ {activeTab === "practices" ? ( + + ) : ( + <> {!unitsLoading && units.length > 0 ? ( + + )}
("video"); const [lessonPracticeChoice, setLessonPracticeChoice] = useState(null); const lessonPracticeChoicePaths = useMemo( @@ -93,16 +74,6 @@ export function CourseModuleDetailPage() { : null, [lessonPracticeChoice], ); - const modulePracticePathOptions = useMemo( - (): PracticeContentPathOptions => ({ - isExamPrep: true, - programType, - courseId, - unitId, - moduleId, - }), - [programType, courseId, unitId, moduleId], - ); const [moduleTitle, setModuleTitle] = useState("Module"); const [moduleDescription, setModuleDescription] = useState("—"); const [modulePublishStatus, setModulePublishStatus] = useState< @@ -272,9 +243,8 @@ export function CourseModuleDetailPage() { }, [parsedModuleId]); useEffect(() => { - if (activeTab !== "video") return; void loadLessons(); - }, [activeTab, loadLessons]); + }, [loadLessons]); const clearCreateLessonForm = () => { setCreateTitle(""); @@ -690,15 +660,6 @@ export function CourseModuleDetailPage() {
- - - Add Practice - { @@ -893,38 +854,9 @@ export function CourseModuleDetailPage() {
- {/* Tabs */} -
-
- - -
-
- - {/* Content */} + {/* Lessons */}
- {activeTab === "video" ? ( - lessonsLoading ? ( + {lessonsLoading ? (
Loading lessons…
@@ -1016,13 +948,7 @@ export function CourseModuleDetailPage() {
) - ) : ( -
- {MOCK_PRACTICES.map((item) => ( - - ))} -
- )} + } - {/* Thumbnail Area */} -
-
- {duration} -
-
- -
-
-
-
- {status} -
-
-
- -

- {title} -

- -
- - -
-
- - ); -} - diff --git a/src/pages/content-management/EditPracticeFlow.tsx b/src/pages/content-management/EditPracticeFlow.tsx index 9d36782..8e26dc6 100644 --- a/src/pages/content-management/EditPracticeFlow.tsx +++ b/src/pages/content-management/EditPracticeFlow.tsx @@ -77,6 +77,7 @@ export function EditPracticeFlow() { if (backToParam?.trim()) return backToParam.trim(); if (routeLessonId) return "lesson"; if (isExamPrep && routeModuleId) return "module"; + if (isExamPrep && routeUnitId && !routeModuleId) return "unit"; if (isExamPrep && routeCourseId) return "courses"; if (routeModuleId) return "module"; if (routeCourseId) return "courses"; @@ -86,6 +87,7 @@ export function EditPracticeFlow() { routeLessonId, isExamPrep, routeModuleId, + routeUnitId, routeCourseId, ]); @@ -108,6 +110,7 @@ export function EditPracticeFlow() { })(); const isModuleContext = effectiveBackTo === "module"; + const isUnitContext = effectiveBackTo === "unit"; const isCourseContext = effectiveBackTo === "courses" || effectiveBackTo === "modules"; const isLessonContext = effectiveBackTo === "lesson" || Boolean(routeLessonId); @@ -121,14 +124,21 @@ export function EditPracticeFlow() { if (lessonId) return `Lesson #${lessonId}${lessonTitleDisplay ? ` — ${lessonTitleDisplay}` : ""}`; if (isModuleContext && moduleId) return `Module #${moduleId}`; - if (isCourseContext && courseId) return `Course #${courseId}`; + if (isUnitContext && unitId) return `Unit #${unitId}`; + if (isCourseContext && courseId) { + return isExamPrep + ? `Catalog course #${courseId}` + : `Course #${courseId}`; + } return null; }, [ lessonId, lessonTitleDisplay, isModuleContext, + isUnitContext, isCourseContext, moduleId, + unitId, courseId, ]); @@ -145,7 +155,9 @@ export function EditPracticeFlow() { ? "Back to lesson practices" : effectiveBackTo === "module" ? "Back to Module" - : effectiveBackTo === "modules" + : effectiveBackTo === "unit" + ? "Back to Unit" + : effectiveBackTo === "modules" ? "Back to Modules" : effectiveBackTo === "courses" ? "Back to Course" @@ -170,6 +182,9 @@ export function EditPracticeFlow() { ) { return `/new-content/courses/${programType}/${courseId}/${unitId}/${moduleId}`; } + if (effectiveBackTo === "unit" && programType && courseId && unitId) { + return `/new-content/courses/${programType}/${courseId}/${unitId}`; + } if (effectiveBackTo === "courses" && programType && courseId) { return `/new-content/courses/${programType}/${courseId}`; } diff --git a/src/pages/content-management/LinkExistingPracticeFlow.tsx b/src/pages/content-management/LinkExistingPracticeFlow.tsx index be6c0b0..2544e7c 100644 --- a/src/pages/content-management/LinkExistingPracticeFlow.tsx +++ b/src/pages/content-management/LinkExistingPracticeFlow.tsx @@ -40,9 +40,10 @@ export function LinkExistingPracticeFlow() { const effectiveBackTo = useMemo(() => { if (backToParam?.trim()) return backToParam.trim() if (isExamPrep && routeModuleId) return "module" + if (isExamPrep && routeUnitId && !routeModuleId) return "unit" if (isExamPrep && routeCourseId) return "courses" return null - }, [backToParam, isExamPrep, routeModuleId, routeCourseId]) + }, [backToParam, isExamPrep, routeModuleId, routeUnitId, routeCourseId]) const courseId = isExamPrep ? routeCourseId ?? searchParams.get("courseId") @@ -63,6 +64,7 @@ export function LinkExistingPracticeFlow() { })() const isModuleContext = effectiveBackTo === "module" + const isUnitContext = effectiveBackTo === "unit" const isCourseContext = effectiveBackTo === "modules" || effectiveBackTo === "courses" @@ -73,16 +75,20 @@ export function LinkExistingPracticeFlow() { const lid = lessonId ? Number(lessonId) : NaN if (Number.isFinite(lid) && lid > 0) return { kind: "LESSON", id: lid } const mid = moduleId ? Number(moduleId) : NaN - if (isModuleContext && Number.isFinite(mid) && mid > 0) + if (isModuleContext && !isExamPrep && Number.isFinite(mid) && mid > 0) return { kind: "MODULE", id: mid } + const uid = unitId ? Number(unitId) : NaN + if (isUnitContext && isExamPrep && Number.isFinite(uid) && uid > 0) + return { kind: "UNIT", id: uid } const cid = courseId ? Number(courseId) : NaN - if (isCourseContext && Number.isFinite(cid) && cid > 0) - return { kind: "COURSE", id: cid } + if (isCourseContext && Number.isFinite(cid) && cid > 0) { + return { kind: isExamPrep ? "CATALOG_COURSE" : "COURSE", id: cid } + } if (isExamPrep && !courseId && !moduleId && !lessonId && programType) { return null } return null - }, [lessonId, moduleId, courseId, isModuleContext, isCourseContext, isExamPrep, programType]) + }, [lessonId, moduleId, unitId, courseId, isModuleContext, isUnitContext, isCourseContext, isExamPrep, programType]) const targetParent = useMemo( () => @@ -97,6 +103,7 @@ export function LinkExistingPracticeFlow() { if (lessonId) return `Lesson #${lessonId}${lessonTitleDisplay ? ` — ${lessonTitleDisplay}` : ""}` if (isModuleContext && moduleId) return `Module #${moduleId}` + if (isUnitContext && unitId) return `Unit #${unitId}` if (isCourseContext && courseId) return `Course #${courseId}` return "selected content" }, [ @@ -104,15 +111,19 @@ export function LinkExistingPracticeFlow() { lessonId, lessonTitleDisplay, isModuleContext, + isUnitContext, isCourseContext, moduleId, + unitId, courseId, ]) const backLabel = effectiveBackTo === "module" ? "Back to Module" - : effectiveBackTo === "modules" + : effectiveBackTo === "unit" + ? "Back to Unit" + : effectiveBackTo === "modules" ? "Back to Modules" : effectiveBackTo === "courses" ? "Back to Course" @@ -131,6 +142,9 @@ export function LinkExistingPracticeFlow() { ) { return `/new-content/courses/${programType}/${courseId}/${unitId}/${moduleId}` } + if (effectiveBackTo === "unit" && programType && courseId && unitId) { + return `/new-content/courses/${programType}/${courseId}/${unitId}` + } if (effectiveBackTo === "courses" && programType && courseId) { return `/new-content/courses/${programType}/${courseId}` } diff --git a/src/pages/content-management/ProgramDetailPage.tsx b/src/pages/content-management/ProgramDetailPage.tsx index 7975e57..28f0c9a 100644 --- a/src/pages/content-management/ProgramDetailPage.tsx +++ b/src/pages/content-management/ProgramDetailPage.tsx @@ -3,7 +3,6 @@ import { Link, useParams, useNavigate } from "react-router-dom"; import { ArrowLeft, Plus, - FileText, ClipboardList, ListChecks, ChevronRight, @@ -12,7 +11,6 @@ import { X, } from "lucide-react"; import { Button } from "../../components/ui/button"; -import { PracticeActionButton } from "./components/PracticeActionButton"; import { Card } from "../../components/ui/card"; import { Dialog, @@ -648,15 +646,6 @@ export function ProgramDetailPage() {
- - - - Add Practice - diff --git a/src/pages/content-management/UnitManagementPage.tsx b/src/pages/content-management/UnitManagementPage.tsx index 3fead9f..b31267b 100644 --- a/src/pages/content-management/UnitManagementPage.tsx +++ b/src/pages/content-management/UnitManagementPage.tsx @@ -10,6 +10,7 @@ import { Trash2, ArrowRight, X, + FileText, } from "lucide-react"; import { Button } from "../../components/ui/button"; import { Card } from "../../components/ui/card"; @@ -45,6 +46,9 @@ import { filterBySearchAndPublishStatus, type PublishStatusFilter, } from "../../lib/contentListFilters"; +import { PracticeActionButton } from "./components/PracticeActionButton"; +import { UnitPracticesPanel } from "./components/UnitPracticesPanel"; +import { cn } from "../../lib/utils"; export function UnitManagementPage() { const navigate = useNavigate(); @@ -109,6 +113,18 @@ export function UnitManagementPage() { const [listSearch, setListSearch] = useState(""); const [publishStatusFilter, setPublishStatusFilter] = useState("all"); + const [activeTab, setActiveTab] = useState<"modules" | "practices">("modules"); + + const unitPracticePathOptions = useMemo( + () => ({ + isExamPrep: true as const, + programType, + courseId: String(catalogCourseId), + unitId: String(parsedUnitId), + backTo: "unit", + }), + [programType, catalogCourseId, parsedUnitId], + ); const filteredModules = useMemo( () => @@ -608,6 +624,7 @@ export function UnitManagementPage() { ) : null} +
{ @@ -796,6 +813,46 @@ export function UnitManagementPage() {
+ + + Attach Practice + + + + + {/* Tabs */} +
+
+ + +
{/* Gradient Divider */} @@ -814,6 +871,14 @@ export function UnitManagementPage() { {/* Grid of Modules */} + {activeTab === "practices" ? ( + + ) : (
{!modulesLoading && modules.length > 0 ? (
+ )} >, +): { practices: ParentContextPractice[]; totalCount: number } { + const body = res.data + const data = + body && typeof body === "object" && "data" in body + ? (body as { data?: { practices?: unknown[]; total_count?: number } }).data + : null + const practices = unwrapPracticesList(res) + const totalCount = + typeof data?.total_count === "number" ? data.total_count : practices.length + return { practices, totalCount } +} + +function formatPracticeDate(iso: string): string { + const d = new Date(iso) + return Number.isNaN(d.getTime()) + ? iso + : d.toLocaleString(undefined, { dateStyle: "medium", timeStyle: "short" }) +} + +function PracticeCard({ + practice, + index, + total, + onEdit, + onDelete, + onTogglePublishStatus, + publishStatusUpdating, +}: { + practice: ParentContextPractice + index: number + total: number + onEdit?: () => void + onDelete?: () => void + onTogglePublishStatus?: (nextStatus: PracticePublishStatus) => void + publishStatusUpdating?: boolean +}) { + const [imgFailed, setImgFailed] = useState(false) + const thumb = resolveThumbnailForPreview(practice.story_image) + const showThumb = Boolean(thumb) && !imgFailed + + return ( + + +
+
+
+ {showThumb ? ( + <> + setImgFailed(true)} + /> +
+ + ) : ( +
+
+ +
+ + No cover image + +
+ )} +
+
+ {index + 1} / {total} +
+
+ +
+
+
+

+ {practice.title || "Untitled practice"} +

+ {practice.story_description ? ( +

+ {practice.story_description} +

+ ) : null} +
+ {onTogglePublishStatus ? ( + + ) : null} +
+ + {practice.quick_tips?.trim() ? ( +
+

+ Quick tips +

+

+ {practice.quick_tips} +

+
+ ) : null} + +
+ + + Question set {practice.question_set_id} + + + + {formatPracticeDate(practice.created_at)} + + {onEdit ? ( + + ) : null} + {onDelete ? ( + + ) : null} +
+
+
+ + + ) +} + +type CatalogCoursePracticesPanelProps = { + catalogCourseId: number + programType: string + courseName: string +} + +export function CatalogCoursePracticesPanel({ + catalogCourseId, + programType, + courseName, +}: CatalogCoursePracticesPanelProps) { + const navigate = useNavigate() + const [practices, setPractices] = useState([]) + const [totalCount, setTotalCount] = useState(0) + const [loading, setLoading] = useState(true) + const [loadError, setLoadError] = useState(null) + const [practiceToDelete, setPracticeToDelete] = useState(null) + const [deleting, setDeleting] = useState(false) + const [publishStatusUpdatingId, setPublishStatusUpdatingId] = useState(null) + const [listSearch, setListSearch] = useState("") + const [publishStatusFilter, setPublishStatusFilter] = useState("all") + + const validCourse = Number.isFinite(catalogCourseId) && catalogCourseId > 0 + + const load = useCallback(async () => { + if (!validCourse) { + setLoading(false) + setLoadError("Invalid catalog course.") + setPractices([]) + return + } + setLoading(true) + setLoadError(null) + try { + const res = await getExamPrepCatalogCoursePractices(catalogCourseId, { + limit: 20, + offset: 0, + }) + const { practices: list, totalCount: total } = extractPracticesPage(res) + setPractices(list) + setTotalCount(total) + } catch { + setPractices([]) + setTotalCount(0) + setLoadError("Could not load practices for this catalog course.") + toast.error("Failed to load practices") + } finally { + setLoading(false) + } + }, [catalogCourseId, validCourse]) + + useEffect(() => { + void load() + }, [load]) + + const filteredPractices = useMemo( + () => + filterBySearchAndPublishStatus(practices, { + search: listSearch, + publishStatusFilter, + getSearchFields: (p) => [p.title, p.story_description, p.quick_tips], + getPublishStatus: (p) => p.publish_status, + }), + [listSearch, practices, publishStatusFilter], + ) + + const editPracticeHref = (practiceId: number) => + `/new-content/courses/${programType}/${catalogCourseId}/edit-practice/${practiceId}?backTo=courses` + + const handlePracticePublishStatus = async ( + practiceId: number, + nextStatus: PracticePublishStatus, + ) => { + setPublishStatusUpdatingId(practiceId) + try { + await setExamPrepPracticePublishStatus(practiceId, { + publish_status: nextStatus, + }) + setPractices((prev) => + prev.map((p) => + p.id === practiceId ? { ...p, publish_status: nextStatus } : p, + ), + ) + toast.success( + nextStatus === "PUBLISHED" ? "Practice published" : "Practice saved as draft", + ) + } catch (e: unknown) { + const err = e as { response?: { data?: { message?: string } } } + toast.error(err.response?.data?.message || "Failed to update practice status") + } finally { + setPublishStatusUpdatingId(null) + } + } + + const confirmDeletePractice = async () => { + if (!practiceToDelete) return + setDeleting(true) + try { + await deleteExamPrepPractice(practiceToDelete.id) + toast.success("Practice deleted") + setPracticeToDelete(null) + await load() + } catch (e: unknown) { + const err = e as { response?: { data?: { message?: string } } } + toast.error(err.response?.data?.message || "Failed to delete practice") + } finally { + setDeleting(false) + } + } + + if (loading) { + return ( +
+ +

Loading practices…

+
+ ) + } + + if (loadError) { + return ( +
+

{loadError}

+ +
+ ) + } + + if (practices.length === 0) { + return ( +
+ +

No practices for {courseName} yet

+

+ Add a practice to attach questions directly to this catalog course. +

+
+ ) + } + + return ( +
+

+ {totalCount} practice{totalCount === 1 ? "" : "s"} linked to this catalog course +

+ + + {filteredPractices.length === 0 ? ( +
+

+ No practices match your search or status filter +

+
+ ) : ( +
+ {filteredPractices.map((practice, index) => ( + navigate(editPracticeHref(practice.id))} + onDelete={() => setPracticeToDelete(practice)} + onTogglePublishStatus={(nextStatus) => + void handlePracticePublishStatus(practice.id, nextStatus) + } + publishStatusUpdating={publishStatusUpdatingId === practice.id} + /> + ))} +
+ )} + + !open && setPracticeToDelete(null)}> + + + Delete practice? + +

+ This permanently removes{" "} + + {practiceToDelete?.title || "this practice"} + + . +

+ + + + +
+
+
+ ) +} diff --git a/src/pages/content-management/components/UnitPracticesPanel.tsx b/src/pages/content-management/components/UnitPracticesPanel.tsx new file mode 100644 index 0000000..96231bf --- /dev/null +++ b/src/pages/content-management/components/UnitPracticesPanel.tsx @@ -0,0 +1,400 @@ +import { useCallback, useEffect, useMemo, useState } from "react" +import { useNavigate } from "react-router-dom" +import { BookOpen, Clock, Edit2, Hash, Loader2, RefreshCw, Trash2 } from "lucide-react" +import { toast } from "sonner" +import { + deleteExamPrepPractice, + getExamPrepUnitPractices, + setExamPrepPracticePublishStatus, +} from "../../../api/courses.api" +import { unwrapPracticesList } from "../../../lib/parentContextPractice" +import { Badge } from "../../../components/ui/badge" +import { Button } from "../../../components/ui/button" +import { Card, CardContent } from "../../../components/ui/card" +import { + Dialog, + DialogContent, + DialogFooter, + DialogHeader, + DialogTitle, +} from "../../../components/ui/dialog" +import { ContentListSearchFilterBar } from "./ContentListSearchFilterBar" +import { ContentPublishStatusChip } from "./ContentPublishStatusChip" +import { + filterBySearchAndPublishStatus, + type PublishStatusFilter, +} from "../../../lib/contentListFilters" +import { resolveThumbnailForPreview } from "../../../lib/videoPreview" +import { cn } from "../../../lib/utils" +import type { + ParentContextPractice, + PracticePublishStatus, +} from "../../../types/course.types" + +function extractPracticesPage( + res: Awaited>, +): { practices: ParentContextPractice[]; totalCount: number } { + const body = res.data + const data = + body && typeof body === "object" && "data" in body + ? (body as { data?: { practices?: unknown[]; total_count?: number } }).data + : null + const practices = unwrapPracticesList(res) + const totalCount = + typeof data?.total_count === "number" ? data.total_count : practices.length + return { practices, totalCount } +} + +function formatPracticeDate(iso: string): string { + const d = new Date(iso) + return Number.isNaN(d.getTime()) + ? iso + : d.toLocaleString(undefined, { dateStyle: "medium", timeStyle: "short" }) +} + +function PracticeCard({ + practice, + index, + total, + onEdit, + onDelete, + onTogglePublishStatus, + publishStatusUpdating, +}: { + practice: ParentContextPractice + index: number + total: number + onEdit?: () => void + onDelete?: () => void + onTogglePublishStatus?: (nextStatus: PracticePublishStatus) => void + publishStatusUpdating?: boolean +}) { + const [imgFailed, setImgFailed] = useState(false) + const thumb = resolveThumbnailForPreview(practice.story_image) + const showThumb = Boolean(thumb) && !imgFailed + + return ( + + +
+
+
+ {showThumb ? ( + <> + setImgFailed(true)} + /> +
+ + ) : ( +
+
+ +
+ + No cover image + +
+ )} +
+
+ {index + 1} / {total} +
+
+ +
+
+
+

+ {practice.title || "Untitled practice"} +

+ {practice.story_description ? ( +

+ {practice.story_description} +

+ ) : null} +
+ {onTogglePublishStatus ? ( + + ) : null} +
+ + {practice.quick_tips?.trim() ? ( +
+

+ Quick tips +

+

+ {practice.quick_tips} +

+
+ ) : null} + +
+ + + Question set {practice.question_set_id} + + + + {formatPracticeDate(practice.created_at)} + + {onEdit ? ( + + ) : null} + {onDelete ? ( + + ) : null} +
+
+
+ + + ) +} + +type UnitPracticesPanelProps = { + unitId: number + programType: string + courseId: number + unitName: string +} + +export function UnitPracticesPanel({ + unitId, + programType, + courseId, + unitName, +}: UnitPracticesPanelProps) { + const navigate = useNavigate() + const [practices, setPractices] = useState([]) + const [totalCount, setTotalCount] = useState(0) + const [loading, setLoading] = useState(true) + const [loadError, setLoadError] = useState(null) + const [practiceToDelete, setPracticeToDelete] = useState(null) + const [deleting, setDeleting] = useState(false) + const [publishStatusUpdatingId, setPublishStatusUpdatingId] = useState(null) + const [listSearch, setListSearch] = useState("") + const [publishStatusFilter, setPublishStatusFilter] = useState("all") + + const validUnit = Number.isFinite(unitId) && unitId > 0 + + const load = useCallback(async () => { + if (!validUnit) { + setLoading(false) + setLoadError("Invalid unit.") + setPractices([]) + return + } + setLoading(true) + setLoadError(null) + try { + const res = await getExamPrepUnitPractices(unitId, { + limit: 20, + offset: 0, + }) + const { practices: list, totalCount: total } = extractPracticesPage(res) + setPractices(list) + setTotalCount(total) + } catch { + setPractices([]) + setTotalCount(0) + setLoadError("Could not load practices for this unit.") + toast.error("Failed to load practices") + } finally { + setLoading(false) + } + }, [unitId, validUnit]) + + useEffect(() => { + void load() + }, [load]) + + const filteredPractices = useMemo( + () => + filterBySearchAndPublishStatus(practices, { + search: listSearch, + publishStatusFilter, + getSearchFields: (p) => [p.title, p.story_description, p.quick_tips], + getPublishStatus: (p) => p.publish_status, + }), + [listSearch, practices, publishStatusFilter], + ) + + const editPracticeHref = (practiceId: number) => + `/new-content/courses/${programType}/${courseId}/${unitId}/edit-practice/${practiceId}?backTo=unit` + + const handlePracticePublishStatus = async ( + practiceId: number, + nextStatus: PracticePublishStatus, + ) => { + setPublishStatusUpdatingId(practiceId) + try { + await setExamPrepPracticePublishStatus(practiceId, { + publish_status: nextStatus, + }) + setPractices((prev) => + prev.map((p) => + p.id === practiceId ? { ...p, publish_status: nextStatus } : p, + ), + ) + toast.success( + nextStatus === "PUBLISHED" ? "Practice published" : "Practice saved as draft", + ) + } catch (e: unknown) { + const err = e as { response?: { data?: { message?: string } } } + toast.error(err.response?.data?.message || "Failed to update practice status") + } finally { + setPublishStatusUpdatingId(null) + } + } + + const confirmDeletePractice = async () => { + if (!practiceToDelete) return + setDeleting(true) + try { + await deleteExamPrepPractice(practiceToDelete.id) + toast.success("Practice deleted") + setPracticeToDelete(null) + await load() + } catch (e: unknown) { + const err = e as { response?: { data?: { message?: string } } } + toast.error(err.response?.data?.message || "Failed to delete practice") + } finally { + setDeleting(false) + } + } + + if (loading) { + return ( +
+ +

Loading practices…

+
+ ) + } + + if (loadError) { + return ( +
+

{loadError}

+ +
+ ) + } + + if (practices.length === 0) { + return ( +
+ +

No practices for {unitName} yet

+

+ Add a practice to attach questions directly to this unit. +

+
+ ) + } + + return ( +
+

+ {totalCount} practice{totalCount === 1 ? "" : "s"} linked to this unit +

+ + + {filteredPractices.length === 0 ? ( +
+

+ No practices match your search or status filter +

+
+ ) : ( +
+ {filteredPractices.map((practice, index) => ( + navigate(editPracticeHref(practice.id))} + onDelete={() => setPracticeToDelete(practice)} + onTogglePublishStatus={(nextStatus) => + void handlePracticePublishStatus(practice.id, nextStatus) + } + publishStatusUpdating={publishStatusUpdatingId === practice.id} + /> + ))} +
+ )} + + !open && setPracticeToDelete(null)}> + + + Delete practice? + +

+ This permanently removes{" "} + + {practiceToDelete?.title || "this practice"} + + . +

+ + + + +
+
+
+ ) +} diff --git a/src/types/course.types.ts b/src/types/course.types.ts index 831eb10..fa5f217 100644 --- a/src/types/course.types.ts +++ b/src/types/course.types.ts @@ -381,6 +381,70 @@ export interface PublishExamPrepModuleLessonRequest { publish_status: PracticePublishStatus } +/** POST /exam-prep/catalog-courses/:catalogCourseId/practices */ +export interface CreateExamPrepCatalogCoursePracticeRequest { + title: string + story_description: string + story_image: string + persona_id: number + question_set_id: number + quick_tips: string + publish_status?: PracticePublishStatus +} + +export interface CreateExamPrepCatalogCoursePracticeResponse { + message: string + data: ParentContextPractice + success: boolean + status_code: number + metadata: unknown | null +} + +export interface GetExamPrepCatalogCoursePracticesResponse { + message: string + data: { + practices: ParentContextPractice[] + total_count: number + limit: number + offset: number + } + success: boolean + status_code: number + metadata: unknown | null +} + +/** POST /exam-prep/units/:unitId/practices */ +export interface CreateExamPrepUnitPracticeRequest { + title: string + story_description: string + story_image: string + persona_id: number + question_set_id: number + quick_tips: string + publish_status?: PracticePublishStatus +} + +export interface CreateExamPrepUnitPracticeResponse { + message: string + data: ParentContextPractice + success: boolean + status_code: number + metadata: unknown | null +} + +export interface GetExamPrepUnitPracticesResponse { + message: string + data: { + practices: ParentContextPractice[] + total_count: number + limit: number + offset: number + } + success: boolean + status_code: number + metadata: unknown | null +} + /** POST /exam-prep/lessons/:lessonId/practices */ export interface CreateExamPrepLessonPracticeRequest { title: string @@ -586,7 +650,12 @@ export interface GetPracticesListParams { unlinked_only?: boolean } -export type PracticeParentKind = "COURSE" | "MODULE" | "LESSON" +export type PracticeParentKind = + | "COURSE" + | "MODULE" + | "LESSON" + | "CATALOG_COURSE" + | "UNIT" export interface PracticeParent { parent_kind: PracticeParentKind