From a8243e3ae4a1a4541399092118015600f861cc0a Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Mon, 29 Jun 2026 08:14:43 -0700 Subject: [PATCH] Support nullable-parent practice shells for LMS and exam prep. Add exam-prep create/list/parents APIs, unlinked attach flow, optional placement on create, and exam-prep location editing with catalog course, unit, and lesson parent kinds. Co-authored-by: Cursor --- src/api/courses.api.ts | 21 ++++++++++++++ src/lib/attachPracticeToParent.ts | 15 +++++++--- src/lib/fetchAllPractices.ts | 10 +++++-- src/lib/practiceCreationOrchestrator.ts | 14 +++++++++ src/lib/practiceEditOrchestrator.ts | 6 ++++ src/lib/practiceParents.ts | 11 +++++-- .../content-management/AddPracticeFlow.tsx | 22 +++++++++++--- .../content-management/EditPracticeFlow.tsx | 29 +++++++++++++------ .../LinkExistingPracticeFlow.tsx | 3 +- .../components/CreatePracticeWizard.tsx | 29 +++++++++---------- .../components/practice-steps/ContextStep.tsx | 10 ++++++- .../practice-steps/PracticeParentsField.tsx | 24 +++++++++++---- .../SelectPracticeToAttachStep.tsx | 12 +++++--- src/types/course.types.ts | 25 ++++++++++++++++ 14 files changed, 184 insertions(+), 47 deletions(-) diff --git a/src/api/courses.api.ts b/src/api/courses.api.ts index 7583a29..7675fef 100644 --- a/src/api/courses.api.ts +++ b/src/api/courses.api.ts @@ -105,7 +105,10 @@ import type { CreateExamPrepCatalogCoursePracticeResponse, CreateExamPrepUnitPracticeRequest, CreateExamPrepUnitPracticeResponse, + CreateExamPrepPracticeRequest, + CreateExamPrepPracticeResponse, GetExamPrepUnitPracticesResponse, + GetExamPrepPracticesListResponse, GetExamPrepCatalogCoursePracticesResponse, GetExamPrepLessonPracticesResponse, GetExamPrepModuleLessonsResponse, @@ -711,6 +714,24 @@ export const getExamPrepLessonPractices = ( { params }, ) +/** POST /exam-prep/practices — create practice with optional parents. */ +export const createExamPrepPractice = (data: CreateExamPrepPracticeRequest) => + http.post("/exam-prep/practices", data) + +/** GET /exam-prep/practices — list practices (supports unlinked_only). */ +export const getExamPrepPractices = (params?: GetPracticesListParams) => + http.get("/exam-prep/practices", { params }) + +/** PUT /exam-prep/practices/:id/parents — replace all parent links. */ +export const updateExamPrepPracticeParents = ( + practiceId: number, + data: UpdatePracticeParentsRequest, +) => + http.put( + `/exam-prep/practices/${practiceId}/parents`, + data, + ) + /** DELETE /exam-prep/practices/:practiceId */ export const deleteExamPrepPractice = (practiceId: number) => http.delete<{ message: string; success: boolean; status_code: number; metadata: unknown }>( diff --git a/src/lib/attachPracticeToParent.ts b/src/lib/attachPracticeToParent.ts index a837562..f23f15a 100644 --- a/src/lib/attachPracticeToParent.ts +++ b/src/lib/attachPracticeToParent.ts @@ -1,4 +1,7 @@ -import { updatePracticeParents } from "../api/courses.api" +import { + updateExamPrepPracticeParents, + updatePracticeParents, +} from "../api/courses.api" import type { ParentContextPractice, PracticeParent } from "../types/course.types" import { dedupeParents, parentsFromPractice } from "./practiceParents" @@ -22,11 +25,15 @@ export function mergedParentsForAttach( export async function attachPracticeToParent( practice: ParentContextPractice, newParent: PracticeParent, + options?: { isExamPrep?: boolean }, ): Promise { if (practiceAlreadyLinkedToParent(practice, newParent)) { throw new Error("This practice is already linked to the selected location.") } - await updatePracticeParents(practice.id, { - parents: mergedParentsForAttach(practice, newParent), - }) + const parents = mergedParentsForAttach(practice, newParent) + if (options?.isExamPrep) { + await updateExamPrepPracticeParents(practice.id, { parents }) + return + } + await updatePracticeParents(practice.id, { parents }) } diff --git a/src/lib/fetchAllPractices.ts b/src/lib/fetchAllPractices.ts index 7384841..436ca0d 100644 --- a/src/lib/fetchAllPractices.ts +++ b/src/lib/fetchAllPractices.ts @@ -1,17 +1,23 @@ -import { getPractices } from "../api/courses.api" +import { getExamPrepPractices, getPractices } from "../api/courses.api" import type { ParentContextPractice } from "../types/course.types" const FETCH_PAGE_SIZE = 100 +export type FetchAllPracticesOptions = { + isExamPrep?: boolean +} + export async function fetchAllPractices( unlinkedOnly: boolean, + options?: FetchAllPracticesOptions, ): Promise { + const listPractices = options?.isExamPrep ? getExamPrepPractices : getPractices const all: ParentContextPractice[] = [] let offset = 0 let totalCount = Number.POSITIVE_INFINITY while (offset < totalCount) { - const res = await getPractices({ + const res = await listPractices({ limit: FETCH_PAGE_SIZE, offset, unlinked_only: unlinkedOnly, diff --git a/src/lib/practiceCreationOrchestrator.ts b/src/lib/practiceCreationOrchestrator.ts index a104f1b..3e3af55 100644 --- a/src/lib/practiceCreationOrchestrator.ts +++ b/src/lib/practiceCreationOrchestrator.ts @@ -3,6 +3,7 @@ import { addQuestionToSet, createExamPrepCatalogCoursePractice, createExamPrepLessonPractice, + createExamPrepPractice, createExamPrepUnitPractice, createParentLinkedPractice, createQuestion, @@ -50,6 +51,8 @@ export interface PracticeCreationInput { examPrepCatalogCourseId?: number /** English proficiency unit practices use POST /exam-prep/units/:id/practices. */ examPrepUnitId?: number + /** When true, fall back to POST /exam-prep/practices when no nested route applies. */ + isExamPrep?: boolean } function extractCreatedResourceId( @@ -216,6 +219,17 @@ export async function executePracticeCreation( quick_tips: opts.quickTips.trim(), publish_status: opts.status, }) + : opts.isExamPrep + ? await createExamPrepPractice({ + parents: createParents, + 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(), diff --git a/src/lib/practiceEditOrchestrator.ts b/src/lib/practiceEditOrchestrator.ts index ed3fe42..349c665 100644 --- a/src/lib/practiceEditOrchestrator.ts +++ b/src/lib/practiceEditOrchestrator.ts @@ -1,5 +1,6 @@ import { updateExamPrepPracticeFull, + updateExamPrepPracticeParents, updateLearnEnglishPracticeFull, updatePracticeParents, } from "../api/courses.api" @@ -44,6 +45,11 @@ export async function executePracticeUpdate( if (opts.isExamPrep) { await updateExamPrepPracticeFull(opts.practiceId, payload) + if (opts.parentsChanged && opts.parents) { + await updateExamPrepPracticeParents(opts.practiceId, { + parents: dedupeParents(opts.parents), + }) + } return } diff --git a/src/lib/practiceParents.ts b/src/lib/practiceParents.ts index 1056222..3e0b08b 100644 --- a/src/lib/practiceParents.ts +++ b/src/lib/practiceParents.ts @@ -110,9 +110,16 @@ export function formatPracticeParentLabel(parent: PracticeParent): string { } } -export function formatPracticeParentsSummary(parents: PracticeParent[]): string { +export function formatPracticeParentsSummary( + parents: PracticeParent[], + options?: { isExamPrep?: boolean }, +): string { const list = dedupeParents(parents) - if (list.length === 0) return "Not attached to any course, module, or lesson" + if (list.length === 0) { + return options?.isExamPrep + ? "Not attached to any catalog course, unit, or lesson" + : "Not attached to any course, module, or lesson" + } return list.map(formatPracticeParentLabel).join(" · ") } diff --git a/src/pages/content-management/AddPracticeFlow.tsx b/src/pages/content-management/AddPracticeFlow.tsx index 9fb010a..b4a20a3 100644 --- a/src/pages/content-management/AddPracticeFlow.tsx +++ b/src/pages/content-management/AddPracticeFlow.tsx @@ -344,7 +344,7 @@ export function AddPracticeFlow() { : formData.parents, ); const parentsErr = validatePracticeParents(parents, { - required: !isFromQuestionType, + required: !isFromQuestionType && !isExamPrep, }); if (parentsErr) { toast.error("Check practice locations", { description: parentsErr }); @@ -447,6 +447,7 @@ export function AddPracticeFlow() { ? parentContext!.id : undefined, examPrepUnitId: useExamPrepUnitApi ? parentContext!.id : undefined, + isExamPrep, status, questionSetTitle: isLearnEnglishLessonPractice ? lessonDefaultTitle @@ -504,7 +505,11 @@ export function AddPracticeFlow() { ? dedupeParents(formData.parents).length > 0 ? "Your practice is saved with the selected locations. You can edit questions or attach more locations anytime." : "Your practice is saved as a draft shell. Attach it to courses, modules, or lessons when you are ready." - : lessonId + : isExamPrep + ? parentContext + ? "Your speaking practice is saved for the linked catalog course, unit, or lesson." + : "Your practice is saved as a draft shell. Attach it to catalog courses, units, or lessons when you are ready." + : lessonId ? "Your speaking practice is saved and linked to this lesson’s question set." : "Your speaking practice is saved for the linked course or module."}

@@ -697,9 +702,9 @@ export function AddPracticeFlow() { prevStep={prevStep} onEditContext={() => setCurrentStep(1)} onEditQuestions={() => setCurrentStep(3)} - parentSummary={parentSummary} + parentSummary={parentSummary ?? (isExamPrep ? formatPracticeParentsSummary([], { isExamPrep: true }) : null)} typeDefinitions={typeDefinitions} - canPublish={parentContext !== null} + canPublish={isExamPrep || parentContext !== null} submitting={submitting} onSaveDraft={() => void submitPractice("DRAFT")} onPublish={() => void submitPractice("PUBLISHED")} @@ -751,6 +756,15 @@ export function AddPracticeFlow() {

) : null} + {isExamPrep && !parentContext && !lessonId ? ( +
+

No placement yet

+

+ This practice will be created without catalog course, unit, or lesson links. Attach + locations later from the attach-practice flow or practice editor. +

+
+ ) : null} {lessonId ? (

Lesson practice

diff --git a/src/pages/content-management/EditPracticeFlow.tsx b/src/pages/content-management/EditPracticeFlow.tsx index 8e26dc6..3ac272d 100644 --- a/src/pages/content-management/EditPracticeFlow.tsx +++ b/src/pages/content-management/EditPracticeFlow.tsx @@ -404,6 +404,12 @@ export function EditPracticeFlow() { toast.error("Check practice locations", { description: parentsErr }); return; } + } else { + const parentsErr = validatePracticeParents(formData.parents, { required: false }); + if (parentsErr) { + toast.error("Check practice locations", { description: parentsErr }); + return; + } } setSubmitting(true); @@ -423,16 +429,15 @@ export function EditPracticeFlow() { isLearnEnglishLessonPractice, lessonDefaultTitle, parents: dedupeParents(formData.parents), - parentsChanged: - !isExamPrep && - !practiceParentsEqual(formData.parents, initialParentsRef.current), + parentsChanged: !practiceParentsEqual( + formData.parents, + initialParentsRef.current, + ), }); toast.success( status === "PUBLISHED" ? "Practice updated and published" : "Practice saved as draft", ); - if (!isExamPrep) { - initialParentsRef.current = dedupeParents(formData.parents); - } + initialParentsRef.current = dedupeParents(formData.parents); setIsSaved(true); } catch (e) { toast.error("Could not update practice", { @@ -511,12 +516,15 @@ export function EditPracticeFlow() { const reviewParentSummary = formData.parents.length > 0 - ? formatPracticeParentsSummary(formData.parents) + ? formatPracticeParentsSummary(formData.parents, { isExamPrep }) : parentSummary; const renderStep = () => { const useContextStep = - isModuleContext || isCourseContext || isLessonContext; + isModuleContext || + isCourseContext || + isLessonContext || + isUnitContext; if (useContextStep) { switch (currentStep) { @@ -530,7 +538,10 @@ export function EditPracticeFlow() { isLessonPractice={isLearnEnglishLessonPractice} lessonTitle={lessonTitleDisplay} parentSummary={reviewParentSummary} - showParentsEditor={!isExamPrep} + showParentsEditor={!isLearnEnglishLessonPractice} + isExamPrepParents={isExamPrep} + parentsOptional={isExamPrep} + parentsCollapsedDefault={isExamPrep} /> ); case 2: diff --git a/src/pages/content-management/LinkExistingPracticeFlow.tsx b/src/pages/content-management/LinkExistingPracticeFlow.tsx index 2544e7c..5151577 100644 --- a/src/pages/content-management/LinkExistingPracticeFlow.tsx +++ b/src/pages/content-management/LinkExistingPracticeFlow.tsx @@ -211,7 +211,7 @@ export function LinkExistingPracticeFlow() { if (!selectedPractice) return setSubmitting(true) try { - await attachPracticeToParent(selectedPractice, targetParent) + await attachPracticeToParent(selectedPractice, targetParent, { isExamPrep }) toast.success("Practice linked successfully") setIsComplete(true) } catch (e) { @@ -298,6 +298,7 @@ export function LinkExistingPracticeFlow() {
{currentStep === 1 ? ( (null) - const canUseWizard = parent != null - useEffect(() => { if (step === 4 && setTitle.trim() && !practiceTitle.trim()) { setPracticeTitle(setTitle.trim()) @@ -181,7 +179,7 @@ export function CreatePracticeWizard({ parent, onCreated }: Props) { } const handleStep4 = async (status: PracticePublishStatus) => { - if (!parent || questionSetId == null) return + if (questionSetId == null) return if (!practiceTitle.trim() || !storyDescription.trim() || !storyImage.trim()) { toast.error("Title, story description, and story image are required") return @@ -190,7 +188,9 @@ export function CreatePracticeWizard({ parent, onCreated }: Props) { setSaving(true) try { await createParentLinkedPractice({ - parents: [{ parent_kind: parent.kind, parent_id: parent.id }], + parents: parent + ? [{ parent_kind: parent.kind, parent_id: parent.id }] + : null, title: practiceTitle.trim(), story_description: storyDescription.trim(), story_image: storyImage.trim(), @@ -218,7 +218,7 @@ export function CreatePracticeWizard({ parent, onCreated }: Props) { Create a new practice

Four steps: create a question set, add audio questions, attach them, then set the practice - story. Select the course, module, or lesson above first. + story. Locations are optional — attach a course, module, or lesson later if needed.

    {STEPS.map((s) => { @@ -242,15 +242,14 @@ export function CreatePracticeWizard({ parent, onCreated }: Props) {
- {!canUseWizard && ( -

- Choose a program, course, and the target (course / module / lesson) in the "Look up - practice" section, then return here. The practice is created for the same selection - (course id, module id, or lesson id). + {!parent ? ( +

+ No placement selected — the practice shell will be created unlinked. Attach it to a + course, module, or lesson later.

- )} + ) : null} - {canUseWizard && step === 1 && ( + {step === 1 && (

@@ -273,7 +272,7 @@ export function CreatePracticeWizard({ parent, onCreated }: Props) {

)} - {canUseWizard && step === 2 && ( + {step === 2 && (

Add one or more audio questions to question set #{questionSetId}. @@ -382,7 +381,7 @@ export function CreatePracticeWizard({ parent, onCreated }: Props) {

)} - {canUseWizard && step === 3 && ( + {step === 3 && (

Confirm the order of questions in the set. @@ -414,7 +413,7 @@ export function CreatePracticeWizard({ parent, onCreated }: Props) {

)} - {canUseWizard && step === 4 && parent && ( + {step === 4 && (

Linked to {parent.kind.toLowerCase()} #{parent.id} · question set #{questionSetId} diff --git a/src/pages/content-management/components/practice-steps/ContextStep.tsx b/src/pages/content-management/components/practice-steps/ContextStep.tsx index 14e1fe8..ee5e9b2 100644 --- a/src/pages/content-management/components/practice-steps/ContextStep.tsx +++ b/src/pages/content-management/components/practice-steps/ContextStep.tsx @@ -31,6 +31,8 @@ interface ContextStepProps { lockedParentKey?: string | null; /** When true, locations are optional and the picker can stay collapsed. */ parentsOptional?: boolean; + /** Use exam-prep parent kinds in the locations editor. */ + isExamPrepParents?: boolean; parentsCollapsedDefault?: boolean; } @@ -49,6 +51,7 @@ export function ContextStep({ lockedParentKey = null, parentsOptional = false, parentsCollapsedDefault = false, + isExamPrepParents = false, }: ContextStepProps) { const storyFileRef = useRef(null); const [uploadingStory, setUploadingStory] = useState(false); @@ -133,7 +136,10 @@ export function ContextStep({

Locations optional

- You can create this practice now and attach it to courses, modules, or lessons + You can create this practice now and attach it to{" "} + {isExamPrepParents + ? "catalog courses, units, or lessons" + : "courses, modules, or lessons"}{" "} later from the practice editor.

@@ -162,6 +168,7 @@ export function ContextStep({ parents={formData.parents ?? []} lockedParentKey={lockedParentKey} optional + isExamPrep={isExamPrepParents} onChange={(parents) => setFormData({ ...formData, parents })} /> ) : null} @@ -170,6 +177,7 @@ export function ContextStep({ setFormData({ ...formData, parents })} /> ) diff --git a/src/pages/content-management/components/practice-steps/PracticeParentsField.tsx b/src/pages/content-management/components/practice-steps/PracticeParentsField.tsx index e06a2b0..54bb961 100644 --- a/src/pages/content-management/components/practice-steps/PracticeParentsField.tsx +++ b/src/pages/content-management/components/practice-steps/PracticeParentsField.tsx @@ -4,12 +4,18 @@ import { Input } from "../../../../components/ui/input" import type { PracticeParent, PracticeParentKind } from "../../../../types/course.types" import { dedupeParents, newParentRow } from "../../../../lib/practiceParents" -const KIND_OPTIONS: { value: PracticeParentKind; label: string }[] = [ +const LMS_KIND_OPTIONS: { value: PracticeParentKind; label: string }[] = [ { value: "COURSE", label: "Course" }, { value: "MODULE", label: "Module" }, { value: "LESSON", label: "Lesson" }, ] +const EXAM_PREP_KIND_OPTIONS: { value: PracticeParentKind; label: string }[] = [ + { value: "CATALOG_COURSE", label: "Catalog course" }, + { value: "UNIT", label: "Unit" }, + { value: "LESSON", label: "Lesson" }, +] + interface PracticeParentsFieldProps { parents: PracticeParent[] onChange: (parents: PracticeParent[]) => void @@ -18,6 +24,8 @@ interface PracticeParentsFieldProps { lockedParentKey?: string | null /** When true, all rows can be cleared (unlinked practice). */ optional?: boolean + /** Use exam-prep parent kinds instead of Learn English. */ + isExamPrep?: boolean } function parentKey(parent: PracticeParent): string { @@ -30,8 +38,10 @@ export function PracticeParentsField({ disabled = false, lockedParentKey = null, optional = false, + isExamPrep = false, }: PracticeParentsFieldProps) { - const rows = parents.length > 0 ? parents : [newParentRow()] + const kindOptions = isExamPrep ? EXAM_PREP_KIND_OPTIONS : LMS_KIND_OPTIONS + const rows = parents.length > 0 ? parents : [newParentRow(isExamPrep ? "CATALOG_COURSE" : "LESSON")] const updateRow = (index: number, patch: Partial) => { const next = rows.map((row, i) => (i === index ? { ...row, ...patch } : row)) @@ -58,8 +68,12 @@ export function PracticeParentsField({

Attached locations

{optional - ? "Attach now or leave empty and link this practice later." - : "Link this practice to one or more courses, modules, or lessons. Each location can only have one practice."} + ? isExamPrep + ? "Attach now or leave empty and link this practice to catalog courses, units, or lessons later." + : "Attach now or leave empty and link this practice later." + : isExamPrep + ? "Link this practice to one or more catalog courses, units, or lessons." + : "Link this practice to one or more courses, modules, or lessons. Each location can only have one practice."}

@@ -80,7 +94,7 @@ export function PracticeParentsField({ } className="h-9 rounded-[8px] border border-grayScale-200 bg-white px-2.5 text-sm font-medium text-grayScale-700 focus:outline-none focus:ring-2 focus:ring-brand-200" > - {KIND_OPTIONS.map((opt) => ( + {kindOptions.map((opt) => ( diff --git a/src/pages/content-management/components/practice-steps/SelectPracticeToAttachStep.tsx b/src/pages/content-management/components/practice-steps/SelectPracticeToAttachStep.tsx index f9e93c6..9ace920 100644 --- a/src/pages/content-management/components/practice-steps/SelectPracticeToAttachStep.tsx +++ b/src/pages/content-management/components/practice-steps/SelectPracticeToAttachStep.tsx @@ -22,6 +22,7 @@ interface SelectPracticeToAttachStepProps { nextStep: () => void onCancel: () => void variant?: "page" | "dialog" + isExamPrep?: boolean } function publishStatusClass(status?: string | null): string { @@ -42,7 +43,7 @@ function matchesPracticeSearch( practice.story_description ?? "", String(practice.id), String(practice.question_set_id), - formatPracticeParentsSummary(practice.parents ?? []), + formatPracticeParentsSummary(practice.parents ?? [], { isExamPrep }), ] .join(" ") .toLowerCase() @@ -57,6 +58,7 @@ export function SelectPracticeToAttachStep({ nextStep, onCancel, variant = "page", + isExamPrep = false, }: SelectPracticeToAttachStepProps) { const isDialog = variant === "dialog" const [loading, setLoading] = useState(true) @@ -69,7 +71,7 @@ export function SelectPracticeToAttachStep({ const load = useCallback(async () => { setLoading(true) try { - const practices = await fetchAllPractices(unlinkedOnly) + const practices = await fetchAllPractices(unlinkedOnly, { isExamPrep }) setAllPractices(practices) } catch (e) { toast.error("Could not load practices", { @@ -79,7 +81,7 @@ export function SelectPracticeToAttachStep({ } finally { setLoading(false) } - }, [unlinkedOnly]) + }, [unlinkedOnly, isExamPrep]) useEffect(() => { void load() @@ -187,7 +189,9 @@ export function SelectPracticeToAttachStep({

No practices found

{unlinkedOnly - ? "No unlinked practices match your search. Try all practices or create a new shell from Question Types." + ? isExamPrep + ? "No unlinked exam-prep practices match your search. Try all practices or create a new shell first." + : "No unlinked practices match your search. Try all practices or create a new shell from Question Types." : "Try a different search, switch to unlinked only, or create a new practice instead."}

diff --git a/src/types/course.types.ts b/src/types/course.types.ts index fa5f217..9953f33 100644 --- a/src/types/course.types.ts +++ b/src/types/course.types.ts @@ -445,6 +445,28 @@ export interface GetExamPrepUnitPracticesResponse { metadata: unknown | null } +/** POST /exam-prep/practices — create with optional parents (unlinked shell). */ +export interface CreateExamPrepPracticeRequest { + question_set_id: number + parents?: PracticeParent[] | null + parent_kind?: PracticeParentKind + parent_id?: number + title?: string + story_description?: string + story_image?: string + persona_id?: number + quick_tips?: string + publish_status?: PracticePublishStatus +} + +export interface CreateExamPrepPracticeResponse { + message: string + data: ParentContextPractice + success: boolean + status_code: number + metadata: unknown | null +} + /** POST /exam-prep/lessons/:lessonId/practices */ export interface CreateExamPrepLessonPracticeRequest { title: string @@ -642,6 +664,9 @@ export interface GetPracticesByParentContextResponse { /** GET /practices — paginated list of all Learn English practices. */ export type GetPracticesListResponse = GetPracticesByParentContextResponse +/** GET /exam-prep/practices — paginated list of exam-prep practices. */ +export type GetExamPrepPracticesListResponse = GetPracticesByParentContextResponse + export interface GetPracticesListParams { limit?: number offset?: number