Add exam-prep catalog course and unit practice management.
Wire dedicated create/list APIs, Units/Practices tabs, attach flows, and routes so practices attach at catalog course and unit levels instead of program or module. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
076edee45d
commit
35452f3a97
|
|
@ -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<CreateExamPrepCatalogCoursePracticeResponse>(
|
||||
`/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<GetExamPrepCatalogCoursePracticesResponse>(
|
||||
`/exam-prep/catalog-courses/${catalogCourseId}/practices`,
|
||||
{ params },
|
||||
)
|
||||
|
||||
/** POST /exam-prep/units/:unitId/practices */
|
||||
export const createExamPrepUnitPractice = (
|
||||
unitId: number,
|
||||
data: CreateExamPrepUnitPracticeRequest,
|
||||
) =>
|
||||
http.post<CreateExamPrepUnitPracticeResponse>(
|
||||
`/exam-prep/units/${unitId}/practices`,
|
||||
data,
|
||||
)
|
||||
|
||||
/** GET /exam-prep/units/:unitId/practices */
|
||||
export const getExamPrepUnitPractices = (
|
||||
unitId: number,
|
||||
params?: { limit?: number; offset?: number },
|
||||
) =>
|
||||
http.get<GetExamPrepUnitPracticesResponse>(
|
||||
`/exam-prep/units/${unitId}/practices`,
|
||||
{ params },
|
||||
)
|
||||
|
||||
/** POST /exam-prep/lessons/:lessonId/practices */
|
||||
export const createExamPrepLessonPractice = (
|
||||
lessonId: number,
|
||||
|
|
|
|||
|
|
@ -221,6 +221,14 @@ export function AppRoutes() {
|
|||
path="/new-content/courses/:programType/:courseId/attach-practice"
|
||||
element={<LinkExistingPracticeFlow />}
|
||||
/>
|
||||
<Route
|
||||
path="/new-content/courses/:programType/:courseId/:unitId/add-practice"
|
||||
element={<AddPracticeFlow />}
|
||||
/>
|
||||
<Route
|
||||
path="/new-content/courses/:programType/:courseId/:unitId/attach-practice"
|
||||
element={<LinkExistingPracticeFlow />}
|
||||
/>
|
||||
<Route
|
||||
path="/new-content/courses/:programType/:courseId/:unitId/:moduleId/add-practice"
|
||||
element={<AddPracticeFlow />}
|
||||
|
|
@ -241,6 +249,14 @@ export function AppRoutes() {
|
|||
path="/new-content/courses/:programType/:courseId/:unitId/:moduleId"
|
||||
element={<CourseModuleDetailPage />}
|
||||
/>
|
||||
<Route
|
||||
path="/new-content/courses/:programType/:courseId/edit-practice/:practiceId"
|
||||
element={<EditPracticeFlow />}
|
||||
/>
|
||||
<Route
|
||||
path="/new-content/courses/:programType/:courseId/:unitId/edit-practice/:practiceId"
|
||||
element={<EditPracticeFlow />}
|
||||
/>
|
||||
<Route
|
||||
path="/new-content/courses/:programType/:courseId/:unitId/:moduleId/lessons/:lessonId/practices"
|
||||
element={<LessonPracticesPage />}
|
||||
|
|
|
|||
|
|
@ -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}`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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() {
|
|||
</ContentPageDescription>
|
||||
) : (
|
||||
<p className="max-w-2xl text-[15px] font-medium leading-relaxed text-grayScale-500">
|
||||
Manage units and modules inside {courseDisplayName}
|
||||
Manage units and practices inside {courseDisplayName}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-3 pt-2">
|
||||
<div className="flex flex-wrap items-center gap-3 pt-2">
|
||||
<Dialog
|
||||
open={addUnitOpen}
|
||||
onOpenChange={(open) => {
|
||||
|
|
@ -704,6 +719,45 @@ export function CourseManagementPage() {
|
|||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
<PracticeActionButton
|
||||
variant="outline"
|
||||
className="h-10 px-6 rounded-[6px] border-brand-500 text-brand-500 font-bold flex items-center gap-2 shadow-sm"
|
||||
pathOptions={catalogCoursePracticePathOptions}
|
||||
parentLabel={courseDisplayName}
|
||||
>
|
||||
<FileText className="h-5 w-5" />
|
||||
Attach Practice
|
||||
</PracticeActionButton>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Tabs */}
|
||||
<div className="border-b border-grayScale-200">
|
||||
<div className="flex gap-10">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setActiveTab("units")}
|
||||
className={cn(
|
||||
"pb-4 text-[16px] font-medium transition-all relative",
|
||||
activeTab === "units"
|
||||
? "text-brand-500 after:absolute after:bottom-0 after:left-0 after:right-0 after:h-[3px] after:bg-brand-500 after:rounded-t-full"
|
||||
: "text-grayScale-400 hover:text-grayScale-600",
|
||||
)}
|
||||
>
|
||||
Units
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setActiveTab("practices")}
|
||||
className={cn(
|
||||
"pb-4 text-[16px] font-medium transition-all relative",
|
||||
activeTab === "practices"
|
||||
? "text-brand-500 after:absolute after:bottom-0 after:left-0 after:right-0 after:h-[3px] after:bg-brand-500 after:rounded-t-full"
|
||||
: "text-grayScale-400 hover:text-grayScale-600",
|
||||
)}
|
||||
>
|
||||
Practices
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
@ -722,8 +776,16 @@ export function CourseManagementPage() {
|
|||
</div>
|
||||
</div>
|
||||
|
||||
{/* Grid of Units */}
|
||||
{/* Grid of Units / Practices */}
|
||||
<div className="space-y-4 pt-4">
|
||||
{activeTab === "practices" ? (
|
||||
<CatalogCoursePracticesPanel
|
||||
catalogCourseId={catalogCourseId}
|
||||
programType={programType ?? "proficiency"}
|
||||
courseName={courseDisplayName}
|
||||
/>
|
||||
) : (
|
||||
<>
|
||||
{!unitsLoading && units.length > 0 ? (
|
||||
<ContentListSearchFilterBar
|
||||
search={listSearch}
|
||||
|
|
@ -864,6 +926,8 @@ export function CourseManagementPage() {
|
|||
))
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Dialog
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { ArrowLeft, Plus, FileText, Video } from "lucide-react";
|
||||
import { ArrowLeft, Plus, Video } from "lucide-react";
|
||||
import { Link, useNavigate, useParams } from "react-router-dom";
|
||||
import { Button } from "../../components/ui/button";
|
||||
import { PracticeActionButton } from "./components/PracticeActionButton";
|
||||
import { PracticeActionChoiceDialog } from "./components/PracticeActionChoiceDialog";
|
||||
import {
|
||||
buildPracticeContentPaths,
|
||||
|
|
@ -55,23 +54,6 @@ const LESSON_THUMB_GRADIENTS = [
|
|||
"from-[#FCE7F3] to-[#F9A8D4]",
|
||||
] as const;
|
||||
|
||||
const MOCK_PRACTICES = [
|
||||
{
|
||||
id: "p1",
|
||||
title: "1.1 Conversation Practice",
|
||||
duration: "08:45",
|
||||
status: "Published",
|
||||
thumbnailColor: "bg-[#E0F2FE]",
|
||||
},
|
||||
{
|
||||
id: "p2",
|
||||
title: "1.2 Roleplay Scenario",
|
||||
duration: "08:45",
|
||||
status: "Draft",
|
||||
thumbnailColor: "bg-[#F0FDF4]",
|
||||
},
|
||||
];
|
||||
|
||||
export function CourseModuleDetailPage() {
|
||||
const navigate = useNavigate();
|
||||
const { programType, courseId, unitId, moduleId } = useParams<{
|
||||
|
|
@ -83,7 +65,6 @@ export function CourseModuleDetailPage() {
|
|||
const parsedModuleId = Number(moduleId);
|
||||
const parsedUnitId = Number(unitId);
|
||||
|
||||
const [activeTab, setActiveTab] = useState<"video" | "practice">("video");
|
||||
const [lessonPracticeChoice, setLessonPracticeChoice] =
|
||||
useState<PracticeContentPathOptions | null>(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() {
|
|||
</div>
|
||||
|
||||
<div className="flex items-center gap-3 pt-2">
|
||||
<PracticeActionButton
|
||||
variant="outline"
|
||||
className="h-10 px-6 rounded-[6px] border-brand-500 text-brand-500 font-bold hover:bg-brand-50 transition-all flex items-center gap-2 shadow-sm"
|
||||
pathOptions={modulePracticePathOptions}
|
||||
parentLabel={moduleTitle}
|
||||
>
|
||||
<FileText className="h-5 w-5" />
|
||||
Add Practice
|
||||
</PracticeActionButton>
|
||||
<Dialog
|
||||
open={createLessonOpen}
|
||||
onOpenChange={(open) => {
|
||||
|
|
@ -893,38 +854,9 @@ export function CourseModuleDetailPage() {
|
|||
</div>
|
||||
</div>
|
||||
|
||||
{/* Tabs */}
|
||||
<div className="border-b border-grayScale-200">
|
||||
<div className="flex gap-10">
|
||||
<button
|
||||
onClick={() => setActiveTab("video")}
|
||||
className={cn(
|
||||
"pb-4 text-[16px] font-medium transition-all relative",
|
||||
activeTab === "video"
|
||||
? "text-brand-500 after:absolute after:bottom-0 after:left-0 after:right-0 after:h-[3px] after:bg-brand-500 after:rounded-t-full"
|
||||
: "text-grayScale-400 hover:text-grayScale-600",
|
||||
)}
|
||||
>
|
||||
Lesson
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setActiveTab("practice")}
|
||||
className={cn(
|
||||
"pb-4 text-[16px] font-medium transition-all relative",
|
||||
activeTab === "practice"
|
||||
? "text-brand-500 after:absolute after:bottom-0 after:left-0 after:right-0 after:h-[3px] after:bg-brand-500 after:rounded-t-full"
|
||||
: "text-grayScale-400 hover:text-grayScale-600",
|
||||
)}
|
||||
>
|
||||
Practice
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
{/* Lessons */}
|
||||
<div className="mt-8">
|
||||
{activeTab === "video" ? (
|
||||
lessonsLoading ? (
|
||||
{lessonsLoading ? (
|
||||
<div className="flex flex-col items-center justify-center py-24 text-grayScale-500 text-[15px] font-medium">
|
||||
Loading lessons…
|
||||
</div>
|
||||
|
|
@ -1016,13 +948,7 @@ export function CourseModuleDetailPage() {
|
|||
</Button>
|
||||
</div>
|
||||
)
|
||||
) : (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
|
||||
{MOCK_PRACTICES.map((item) => (
|
||||
<PracticeCard key={item.id} {...item} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
}
|
||||
</div>
|
||||
|
||||
<Dialog
|
||||
|
|
@ -1247,68 +1173,3 @@ export function CourseModuleDetailPage() {
|
|||
);
|
||||
}
|
||||
|
||||
function PracticeCard({
|
||||
title,
|
||||
duration,
|
||||
status,
|
||||
thumbnailColor,
|
||||
}: {
|
||||
title: string;
|
||||
duration: string;
|
||||
status: string;
|
||||
thumbnailColor: string;
|
||||
}) {
|
||||
return (
|
||||
<Card className="group flex flex-col bg-white rounded-[20px] border border-grayScale-50 overflow-hidden shadow-sm hover:shadow-xl hover:shadow-grayScale-400/5 transition-all">
|
||||
{/* Thumbnail Area */}
|
||||
<div className={cn("h-44 w-full relative", thumbnailColor)}>
|
||||
<div className="absolute bottom-3 right-3 bg-black/60 text-white text-[11px] font-bold px-2 py-0.5 rounded backdrop-blur-sm">
|
||||
{duration}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="p-5 flex flex-col flex-1 space-y-5">
|
||||
<div className="flex items-center justify-between">
|
||||
<div
|
||||
className={cn(
|
||||
"px-3 py-1 rounded-full text-[10px] font-bold uppercase tracking-wider flex items-center gap-2 border",
|
||||
status === "Published"
|
||||
? "bg-[#F0FDF4] text-[#16A34A] border-[#DCFCE7]"
|
||||
: "bg-grayScale-50 text-grayScale-400 border-grayScale-100",
|
||||
)}
|
||||
>
|
||||
<div
|
||||
className={cn(
|
||||
"h-1.5 w-1.5 rounded-full",
|
||||
status === "Published" ? "bg-[#16A34A]" : "bg-grayScale-300",
|
||||
)}
|
||||
/>
|
||||
{status}
|
||||
</div>
|
||||
<div />
|
||||
</div>
|
||||
|
||||
<h3 className="text-[14px] font-bold text-[#0F172A] line-clamp-2 leading-snug">
|
||||
{title}
|
||||
</h3>
|
||||
|
||||
<div className="pt-2 grid grid-cols-1 gap-2 mt-auto">
|
||||
<Button variant="outline" className="w-full h-10 rounded-[10px] border-grayScale-200 text-grayScale-600 font-bold text-xs">
|
||||
Edit
|
||||
</Button>
|
||||
<Button
|
||||
className={cn(
|
||||
"w-full h-10 rounded-[10px] font-bold text-xs shadow-sm",
|
||||
status === "Published"
|
||||
? "bg-[#ECD5E9] text-[#9E2891] hover:bg-[#EBD0E7]"
|
||||
: "bg-brand-500 text-white hover:bg-brand-600",
|
||||
)}
|
||||
>
|
||||
{status === "Published" ? "Published" : "Publish"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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}`;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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}`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() {
|
|||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
||||
<PracticeActionButton
|
||||
variant="outline"
|
||||
className="h-10 px-6 rounded-[6px] border-brand-500 text-brand-500 font-bold flex items-center gap-2"
|
||||
pathOptions={{ isExamPrep: true, programType }}
|
||||
>
|
||||
<FileText className="h-5 w-5" />
|
||||
Add Practice
|
||||
</PracticeActionButton>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
|
|
@ -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<PublishStatusFilter>("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}
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap items-center gap-3 pt-2">
|
||||
<Dialog
|
||||
open={addModuleOpen}
|
||||
onOpenChange={(open) => {
|
||||
|
|
@ -796,6 +813,46 @@ export function UnitManagementPage() {
|
|||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
<PracticeActionButton
|
||||
variant="outline"
|
||||
className="h-10 px-6 rounded-[6px] border-brand-500 text-brand-500 font-bold flex items-center gap-2 shadow-sm"
|
||||
pathOptions={unitPracticePathOptions}
|
||||
parentLabel={unitDisplayName}
|
||||
>
|
||||
<FileText className="h-5 w-5" />
|
||||
Attach Practice
|
||||
</PracticeActionButton>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Tabs */}
|
||||
<div className="border-b border-grayScale-200">
|
||||
<div className="flex gap-10">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setActiveTab("modules")}
|
||||
className={cn(
|
||||
"pb-4 text-[16px] font-medium transition-all relative",
|
||||
activeTab === "modules"
|
||||
? "text-brand-500 after:absolute after:bottom-0 after:left-0 after:right-0 after:h-[3px] after:bg-brand-500 after:rounded-t-full"
|
||||
: "text-grayScale-400 hover:text-grayScale-600",
|
||||
)}
|
||||
>
|
||||
Modules
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setActiveTab("practices")}
|
||||
className={cn(
|
||||
"pb-4 text-[16px] font-medium transition-all relative",
|
||||
activeTab === "practices"
|
||||
? "text-brand-500 after:absolute after:bottom-0 after:left-0 after:right-0 after:h-[3px] after:bg-brand-500 after:rounded-t-full"
|
||||
: "text-grayScale-400 hover:text-grayScale-600",
|
||||
)}
|
||||
>
|
||||
Practices
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Gradient Divider */}
|
||||
|
|
@ -814,6 +871,14 @@ export function UnitManagementPage() {
|
|||
</div>
|
||||
|
||||
{/* Grid of Modules */}
|
||||
{activeTab === "practices" ? (
|
||||
<UnitPracticesPanel
|
||||
unitId={parsedUnitId}
|
||||
programType={programType ?? ""}
|
||||
courseId={catalogCourseId}
|
||||
unitName={unitDisplayName}
|
||||
/>
|
||||
) : (
|
||||
<div className="space-y-4 pt-4">
|
||||
{!modulesLoading && modules.length > 0 ? (
|
||||
<ContentListSearchFilterBar
|
||||
|
|
@ -967,6 +1032,7 @@ export function UnitManagementPage() {
|
|||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Dialog
|
||||
open={editingModuleId !== null}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,398 @@
|
|||
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,
|
||||
getExamPrepCatalogCoursePractices,
|
||||
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<ReturnType<typeof getExamPrepCatalogCoursePractices>>,
|
||||
): { 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 (
|
||||
<Card
|
||||
className={cn(
|
||||
"overflow-hidden border-grayScale-100/90 bg-white shadow-sm transition-all duration-300",
|
||||
"hover:border-brand-200/60 hover:shadow-md hover:shadow-brand-500/5",
|
||||
)}
|
||||
>
|
||||
<CardContent className="p-0">
|
||||
<div className="flex flex-col lg:flex-row lg:items-stretch">
|
||||
<div className="relative shrink-0 lg:w-[280px]">
|
||||
<div
|
||||
className={cn(
|
||||
"relative aspect-[16/10] w-full overflow-hidden bg-gradient-to-br from-grayScale-100 to-grayScale-50 lg:aspect-auto lg:h-full lg:min-h-[220px]",
|
||||
!showThumb && "grid min-h-[180px] place-items-center lg:min-h-[220px]",
|
||||
)}
|
||||
>
|
||||
{showThumb ? (
|
||||
<>
|
||||
<img
|
||||
src={thumb!}
|
||||
alt=""
|
||||
className="h-full w-full object-cover"
|
||||
onError={() => setImgFailed(true)}
|
||||
/>
|
||||
<div className="pointer-events-none absolute inset-0 bg-gradient-to-t from-black/25 via-transparent to-black/10" />
|
||||
</>
|
||||
) : (
|
||||
<div className="flex flex-col items-center gap-2 text-grayScale-400">
|
||||
<div className="flex h-14 w-14 items-center justify-center rounded-2xl bg-white/80 shadow-inner ring-1 ring-grayScale-200/80">
|
||||
<BookOpen className="h-7 w-7" />
|
||||
</div>
|
||||
<span className="text-[11px] font-semibold uppercase tracking-wider">
|
||||
No cover image
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="absolute left-3 top-3 rounded-full bg-white/90 px-2.5 py-1 text-[10px] font-bold uppercase tracking-wider text-grayScale-600 shadow-sm ring-1 ring-black/5">
|
||||
{index + 1} / {total}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex min-w-0 flex-1 flex-col p-5 sm:p-6">
|
||||
<div className="mb-3 flex flex-wrap items-start justify-between gap-3">
|
||||
<div className="min-w-0 flex-1">
|
||||
<h3 className="text-lg font-bold tracking-tight text-grayScale-900">
|
||||
{practice.title || "Untitled practice"}
|
||||
</h3>
|
||||
{practice.story_description ? (
|
||||
<p className="mt-1.5 line-clamp-3 text-sm leading-relaxed text-grayScale-500">
|
||||
{practice.story_description}
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
{onTogglePublishStatus ? (
|
||||
<ContentPublishStatusChip
|
||||
publishStatus={practice.publish_status}
|
||||
updating={publishStatusUpdating}
|
||||
contentLabel="practice"
|
||||
onToggle={onTogglePublishStatus}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{practice.quick_tips?.trim() ? (
|
||||
<div className="rounded-xl border border-amber-100/80 bg-amber-50/60 px-4 py-3">
|
||||
<p className="text-[10px] font-bold uppercase tracking-wider text-amber-900/75">
|
||||
Quick tips
|
||||
</p>
|
||||
<p className="mt-1.5 whitespace-pre-line text-[13px] leading-relaxed text-grayScale-800">
|
||||
{practice.quick_tips}
|
||||
</p>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
<div className="mt-6 flex flex-wrap items-center gap-2 border-t border-grayScale-100 pt-5">
|
||||
<Badge variant="secondary" className="gap-1.5 py-1 pl-2 pr-2.5 font-medium normal-case">
|
||||
<Hash className="h-3 w-3 opacity-70" aria-hidden />
|
||||
Question set {practice.question_set_id}
|
||||
</Badge>
|
||||
<Badge variant="secondary" className="gap-1.5 py-1 pl-2 pr-2.5 font-medium normal-case">
|
||||
<Clock className="h-3 w-3 opacity-70" aria-hidden />
|
||||
{formatPracticeDate(practice.created_at)}
|
||||
</Badge>
|
||||
{onEdit ? (
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="ml-auto h-9 rounded-[10px] border-brand-500 text-xs font-bold text-brand-500 hover:bg-brand-50"
|
||||
onClick={onEdit}
|
||||
>
|
||||
<Edit2 className="mr-1.5 h-3.5 w-3.5" />
|
||||
Edit
|
||||
</Button>
|
||||
) : null}
|
||||
{onDelete ? (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-9 text-destructive hover:bg-destructive/10 hover:text-destructive"
|
||||
onClick={onDelete}
|
||||
>
|
||||
<Trash2 className="mr-1.5 h-3.5 w-3.5" />
|
||||
Delete
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
type CatalogCoursePracticesPanelProps = {
|
||||
catalogCourseId: number
|
||||
programType: string
|
||||
courseName: string
|
||||
}
|
||||
|
||||
export function CatalogCoursePracticesPanel({
|
||||
catalogCourseId,
|
||||
programType,
|
||||
courseName,
|
||||
}: CatalogCoursePracticesPanelProps) {
|
||||
const navigate = useNavigate()
|
||||
const [practices, setPractices] = useState<ParentContextPractice[]>([])
|
||||
const [totalCount, setTotalCount] = useState(0)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [loadError, setLoadError] = useState<string | null>(null)
|
||||
const [practiceToDelete, setPracticeToDelete] = useState<ParentContextPractice | null>(null)
|
||||
const [deleting, setDeleting] = useState(false)
|
||||
const [publishStatusUpdatingId, setPublishStatusUpdatingId] = useState<number | null>(null)
|
||||
const [listSearch, setListSearch] = useState("")
|
||||
const [publishStatusFilter, setPublishStatusFilter] = useState<PublishStatusFilter>("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 (
|
||||
<div className="flex flex-col items-center justify-center py-24 text-grayScale-500">
|
||||
<Loader2 className="mb-3 h-8 w-8 animate-spin text-brand-500" />
|
||||
<p className="text-sm font-medium">Loading practices…</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (loadError) {
|
||||
return (
|
||||
<div className="rounded-2xl border border-amber-100 bg-amber-50/80 px-6 py-8 text-center">
|
||||
<p className="text-sm text-amber-900">{loadError}</p>
|
||||
<Button variant="outline" size="sm" className="mt-4" onClick={() => void load()}>
|
||||
<RefreshCw className="mr-2 h-4 w-4" />
|
||||
Retry
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (practices.length === 0) {
|
||||
return (
|
||||
<div className="rounded-xl border border-dashed border-grayScale-200 bg-grayScale-50/50 px-6 py-14 text-center">
|
||||
<BookOpen className="mx-auto mb-3 h-10 w-10 text-grayScale-300" />
|
||||
<p className="text-sm font-medium text-grayScale-600">No practices for {courseName} yet</p>
|
||||
<p className="mt-1 text-sm text-grayScale-400">
|
||||
Add a practice to attach questions directly to this catalog course.
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<p className="text-sm text-grayScale-500">
|
||||
{totalCount} practice{totalCount === 1 ? "" : "s"} linked to this catalog course
|
||||
</p>
|
||||
<ContentListSearchFilterBar
|
||||
search={listSearch}
|
||||
onSearchChange={setListSearch}
|
||||
publishStatusFilter={publishStatusFilter}
|
||||
onPublishStatusFilterChange={setPublishStatusFilter}
|
||||
searchPlaceholder="Search practices by title or description…"
|
||||
searchAriaLabel="Search catalog course practices"
|
||||
/>
|
||||
|
||||
{filteredPractices.length === 0 ? (
|
||||
<div className="rounded-xl border border-dashed border-grayScale-200 bg-grayScale-50/50 px-6 py-14 text-center">
|
||||
<p className="text-sm font-medium text-grayScale-600">
|
||||
No practices match your search or status filter
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-5">
|
||||
{filteredPractices.map((practice, index) => (
|
||||
<PracticeCard
|
||||
key={practice.id}
|
||||
practice={practice}
|
||||
index={index}
|
||||
total={filteredPractices.length}
|
||||
onEdit={() => navigate(editPracticeHref(practice.id))}
|
||||
onDelete={() => setPracticeToDelete(practice)}
|
||||
onTogglePublishStatus={(nextStatus) =>
|
||||
void handlePracticePublishStatus(practice.id, nextStatus)
|
||||
}
|
||||
publishStatusUpdating={publishStatusUpdatingId === practice.id}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Dialog open={practiceToDelete != null} onOpenChange={(open) => !open && setPracticeToDelete(null)}>
|
||||
<DialogContent className="max-w-md rounded-2xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Delete practice?</DialogTitle>
|
||||
</DialogHeader>
|
||||
<p className="text-sm text-grayScale-600">
|
||||
This permanently removes{" "}
|
||||
<span className="font-semibold text-grayScale-800">
|
||||
{practiceToDelete?.title || "this practice"}
|
||||
</span>
|
||||
.
|
||||
</p>
|
||||
<DialogFooter className="gap-2 sm:gap-0">
|
||||
<Button variant="outline" onClick={() => setPracticeToDelete(null)} disabled={deleting}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button variant="destructive" onClick={() => void confirmDeletePractice()} disabled={deleting}>
|
||||
{deleting ? "Deleting…" : "Delete"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
400
src/pages/content-management/components/UnitPracticesPanel.tsx
Normal file
400
src/pages/content-management/components/UnitPracticesPanel.tsx
Normal file
|
|
@ -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<ReturnType<typeof getExamPrepUnitPractices>>,
|
||||
): { 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 (
|
||||
<Card
|
||||
className={cn(
|
||||
"overflow-hidden border-grayScale-100/90 bg-white shadow-sm transition-all duration-300",
|
||||
"hover:border-brand-200/60 hover:shadow-md hover:shadow-brand-500/5",
|
||||
)}
|
||||
>
|
||||
<CardContent className="p-0">
|
||||
<div className="flex flex-col lg:flex-row lg:items-stretch">
|
||||
<div className="relative shrink-0 lg:w-[280px]">
|
||||
<div
|
||||
className={cn(
|
||||
"relative aspect-[16/10] w-full overflow-hidden bg-gradient-to-br from-grayScale-100 to-grayScale-50 lg:aspect-auto lg:h-full lg:min-h-[220px]",
|
||||
!showThumb && "grid min-h-[180px] place-items-center lg:min-h-[220px]",
|
||||
)}
|
||||
>
|
||||
{showThumb ? (
|
||||
<>
|
||||
<img
|
||||
src={thumb!}
|
||||
alt=""
|
||||
className="h-full w-full object-cover"
|
||||
onError={() => setImgFailed(true)}
|
||||
/>
|
||||
<div className="pointer-events-none absolute inset-0 bg-gradient-to-t from-black/25 via-transparent to-black/10" />
|
||||
</>
|
||||
) : (
|
||||
<div className="flex flex-col items-center gap-2 text-grayScale-400">
|
||||
<div className="flex h-14 w-14 items-center justify-center rounded-2xl bg-white/80 shadow-inner ring-1 ring-grayScale-200/80">
|
||||
<BookOpen className="h-7 w-7" />
|
||||
</div>
|
||||
<span className="text-[11px] font-semibold uppercase tracking-wider">
|
||||
No cover image
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="absolute left-3 top-3 rounded-full bg-white/90 px-2.5 py-1 text-[10px] font-bold uppercase tracking-wider text-grayScale-600 shadow-sm ring-1 ring-black/5">
|
||||
{index + 1} / {total}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex min-w-0 flex-1 flex-col p-5 sm:p-6">
|
||||
<div className="mb-3 flex flex-wrap items-start justify-between gap-3">
|
||||
<div className="min-w-0 flex-1">
|
||||
<h3 className="text-lg font-bold tracking-tight text-grayScale-900">
|
||||
{practice.title || "Untitled practice"}
|
||||
</h3>
|
||||
{practice.story_description ? (
|
||||
<p className="mt-1.5 line-clamp-3 text-sm leading-relaxed text-grayScale-500">
|
||||
{practice.story_description}
|
||||
</p>
|
||||
) : null}
|
||||
</div>
|
||||
{onTogglePublishStatus ? (
|
||||
<ContentPublishStatusChip
|
||||
publishStatus={practice.publish_status}
|
||||
updating={publishStatusUpdating}
|
||||
contentLabel="practice"
|
||||
onToggle={onTogglePublishStatus}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{practice.quick_tips?.trim() ? (
|
||||
<div className="rounded-xl border border-amber-100/80 bg-amber-50/60 px-4 py-3">
|
||||
<p className="text-[10px] font-bold uppercase tracking-wider text-amber-900/75">
|
||||
Quick tips
|
||||
</p>
|
||||
<p className="mt-1.5 whitespace-pre-line text-[13px] leading-relaxed text-grayScale-800">
|
||||
{practice.quick_tips}
|
||||
</p>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
<div className="mt-6 flex flex-wrap items-center gap-2 border-t border-grayScale-100 pt-5">
|
||||
<Badge variant="secondary" className="gap-1.5 py-1 pl-2 pr-2.5 font-medium normal-case">
|
||||
<Hash className="h-3 w-3 opacity-70" aria-hidden />
|
||||
Question set {practice.question_set_id}
|
||||
</Badge>
|
||||
<Badge variant="secondary" className="gap-1.5 py-1 pl-2 pr-2.5 font-medium normal-case">
|
||||
<Clock className="h-3 w-3 opacity-70" aria-hidden />
|
||||
{formatPracticeDate(practice.created_at)}
|
||||
</Badge>
|
||||
{onEdit ? (
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="ml-auto h-9 rounded-[10px] border-brand-500 text-xs font-bold text-brand-500 hover:bg-brand-50"
|
||||
onClick={onEdit}
|
||||
>
|
||||
<Edit2 className="mr-1.5 h-3.5 w-3.5" />
|
||||
Edit
|
||||
</Button>
|
||||
) : null}
|
||||
{onDelete ? (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-9 text-destructive hover:bg-destructive/10 hover:text-destructive"
|
||||
onClick={onDelete}
|
||||
>
|
||||
<Trash2 className="mr-1.5 h-3.5 w-3.5" />
|
||||
Delete
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
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<ParentContextPractice[]>([])
|
||||
const [totalCount, setTotalCount] = useState(0)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [loadError, setLoadError] = useState<string | null>(null)
|
||||
const [practiceToDelete, setPracticeToDelete] = useState<ParentContextPractice | null>(null)
|
||||
const [deleting, setDeleting] = useState(false)
|
||||
const [publishStatusUpdatingId, setPublishStatusUpdatingId] = useState<number | null>(null)
|
||||
const [listSearch, setListSearch] = useState("")
|
||||
const [publishStatusFilter, setPublishStatusFilter] = useState<PublishStatusFilter>("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 (
|
||||
<div className="flex flex-col items-center justify-center py-24 text-grayScale-500">
|
||||
<Loader2 className="mb-3 h-8 w-8 animate-spin text-brand-500" />
|
||||
<p className="text-sm font-medium">Loading practices…</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (loadError) {
|
||||
return (
|
||||
<div className="rounded-2xl border border-amber-100 bg-amber-50/80 px-6 py-8 text-center">
|
||||
<p className="text-sm text-amber-900">{loadError}</p>
|
||||
<Button variant="outline" size="sm" className="mt-4" onClick={() => void load()}>
|
||||
<RefreshCw className="mr-2 h-4 w-4" />
|
||||
Retry
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (practices.length === 0) {
|
||||
return (
|
||||
<div className="rounded-xl border border-dashed border-grayScale-200 bg-grayScale-50/50 px-6 py-14 text-center">
|
||||
<BookOpen className="mx-auto mb-3 h-10 w-10 text-grayScale-300" />
|
||||
<p className="text-sm font-medium text-grayScale-600">No practices for {unitName} yet</p>
|
||||
<p className="mt-1 text-sm text-grayScale-400">
|
||||
Add a practice to attach questions directly to this unit.
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<p className="text-sm text-grayScale-500">
|
||||
{totalCount} practice{totalCount === 1 ? "" : "s"} linked to this unit
|
||||
</p>
|
||||
<ContentListSearchFilterBar
|
||||
search={listSearch}
|
||||
onSearchChange={setListSearch}
|
||||
publishStatusFilter={publishStatusFilter}
|
||||
onPublishStatusFilterChange={setPublishStatusFilter}
|
||||
searchPlaceholder="Search practices by title or description…"
|
||||
searchAriaLabel="Search unit practices"
|
||||
/>
|
||||
|
||||
{filteredPractices.length === 0 ? (
|
||||
<div className="rounded-xl border border-dashed border-grayScale-200 bg-grayScale-50/50 px-6 py-14 text-center">
|
||||
<p className="text-sm font-medium text-grayScale-600">
|
||||
No practices match your search or status filter
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-5">
|
||||
{filteredPractices.map((practice, index) => (
|
||||
<PracticeCard
|
||||
key={practice.id}
|
||||
practice={practice}
|
||||
index={index}
|
||||
total={filteredPractices.length}
|
||||
onEdit={() => navigate(editPracticeHref(practice.id))}
|
||||
onDelete={() => setPracticeToDelete(practice)}
|
||||
onTogglePublishStatus={(nextStatus) =>
|
||||
void handlePracticePublishStatus(practice.id, nextStatus)
|
||||
}
|
||||
publishStatusUpdating={publishStatusUpdatingId === practice.id}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Dialog open={practiceToDelete != null} onOpenChange={(open) => !open && setPracticeToDelete(null)}>
|
||||
<DialogContent className="max-w-md rounded-2xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Delete practice?</DialogTitle>
|
||||
</DialogHeader>
|
||||
<p className="text-sm text-grayScale-600">
|
||||
This permanently removes{" "}
|
||||
<span className="font-semibold text-grayScale-800">
|
||||
{practiceToDelete?.title || "this practice"}
|
||||
</span>
|
||||
.
|
||||
</p>
|
||||
<DialogFooter className="gap-2 sm:gap-0">
|
||||
<Button variant="outline" onClick={() => setPracticeToDelete(null)} disabled={deleting}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button variant="destructive" onClick={() => void confirmDeletePractice()} disabled={deleting}>
|
||||
{deleting ? "Deleting…" : "Delete"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user