import { useCallback, useEffect, useState } from "react" import { Check, ChevronLeft, ChevronRight, ListOrdered, Plus, Trash2 } from "lucide-react" import { toast } from "sonner" import { Button } from "../../../components/ui/button" import { Card, CardContent, CardHeader, CardTitle } from "../../../components/ui/card" import { Input } from "../../../components/ui/input" import { Textarea } from "../../../components/ui/textarea" import { addQuestionToSet, createParentLinkedPractice, createQuestion, createQuestionSet, } from "../../../api/courses.api" import type { CreateQuestionRequest, PracticeParentKind } from "../../../types/course.types" import { cn } from "../../../lib/utils" import { SpinnerIcon } from "../../../components/ui/spinner-icon" export type CreatePracticeWizardParent = { kind: PracticeParentKind id: number } | null const STEPS = [ { n: 1, label: "Question set" }, { n: 2, label: "Questions" }, { n: 3, label: "Attach" }, { n: 4, label: "Practice" }, ] as const type QuestionDraft = { question_text: string voice_prompt: string sample_answer_voice_prompt: string audio_correct_answer_text: string } const emptyQuestion = (): QuestionDraft => ({ question_text: "", voice_prompt: "", sample_answer_voice_prompt: "", audio_correct_answer_text: "", }) type Props = { parent: CreatePracticeWizardParent onCreated?: () => void } export function CreatePracticeWizard({ parent, onCreated }: Props) { const [step, setStep] = useState(1) const [saving, setSaving] = useState(false) const [setTitle, setSetTitle] = useState("") const [questionSetId, setQuestionSetId] = useState(null) const [questionRows, setQuestionRows] = useState([emptyQuestion()]) const [createdQuestionIds, setCreatedQuestionIds] = useState([]) const [practiceTitle, setPracticeTitle] = useState("") const [storyDescription, setStoryDescription] = useState("") const [storyImage, setStoryImage] = useState("") const [quickTips, setQuickTips] = useState("") const canUseWizard = parent != null useEffect(() => { if (step === 4 && setTitle.trim() && !practiceTitle.trim()) { setPracticeTitle(setTitle.trim()) } }, [step, setTitle, practiceTitle]) const resetAll = useCallback(() => { setStep(1) setSetTitle("") setQuestionSetId(null) setQuestionRows([emptyQuestion()]) setCreatedQuestionIds([]) setPracticeTitle("") setStoryDescription("") setStoryImage("") setQuickTips("") }, []) const handleStep1 = async () => { if (!setTitle.trim()) { toast.error("Enter a title for the question set") return } setSaving(true) try { const res = await createQuestionSet({ title: setTitle.trim(), set_type: "PRACTICE", }) const id = res.data?.data?.id if (id == null) { throw new Error("No question set id in response") } setQuestionSetId(id) toast.success("Question set created") setStep(2) } catch (e: unknown) { const err = e as { response?: { data?: { message?: string } }; message?: string } toast.error(err.response?.data?.message || err.message || "Failed to create question set") } finally { setSaving(false) } } const handleStep2 = async () => { for (let i = 0; i < questionRows.length; i++) { const r = questionRows[i] if (!r.question_text.trim()) { toast.error(`Question ${i + 1}: enter question text`) return } if (!r.voice_prompt.trim() || !r.sample_answer_voice_prompt.trim()) { toast.error(`Question ${i + 1}: enter voice prompt URLs`) return } if (!r.audio_correct_answer_text.trim()) { toast.error(`Question ${i + 1}: enter the correct answer text`) return } } if (questionSetId == null) return setSaving(true) try { const ids: number[] = [] for (const r of questionRows) { const body: CreateQuestionRequest = { question_text: r.question_text.trim(), question_type: "AUDIO", voice_prompt: r.voice_prompt.trim(), sample_answer_voice_prompt: r.sample_answer_voice_prompt.trim(), audio_correct_answer_text: r.audio_correct_answer_text.trim(), } const res = await createQuestion(body) const qid = res.data?.data?.id if (qid == null) { throw new Error("A question was created but no id was returned") } ids.push(qid) } setCreatedQuestionIds(ids) toast.success(`Created ${ids.length} question(s)`) setStep(3) } catch (e: unknown) { const err = e as { response?: { data?: { message?: string } }; message?: string } toast.error(err.response?.data?.message || err.message || "Failed to create questions") } finally { setSaving(false) } } const handleStep3 = async () => { if (questionSetId == null || createdQuestionIds.length === 0) return setSaving(true) try { for (let i = 0; i < createdQuestionIds.length; i++) { await addQuestionToSet(questionSetId, { question_id: createdQuestionIds[i], display_order: i + 1, }) } toast.success("Questions linked to the set") setStep(4) } catch (e: unknown) { const err = e as { response?: { data?: { message?: string } }; message?: string } toast.error(err.response?.data?.message || err.message || "Failed to attach questions") } finally { setSaving(false) } } const handleStep4 = async () => { if (!parent || questionSetId == null) return if (!practiceTitle.trim() || !storyDescription.trim() || !storyImage.trim()) { toast.error("Title, story description, and story image are required") return } setSaving(true) try { await createParentLinkedPractice({ parent_kind: parent.kind, parent_id: parent.id, title: practiceTitle.trim(), story_description: storyDescription.trim(), story_image: storyImage.trim(), question_set_id: questionSetId, quick_tips: quickTips.trim(), }) toast.success("Practice created successfully") resetAll() onCreated?.() } catch (e: unknown) { const err = e as { response?: { data?: { message?: string } }; message?: string } toast.error(err.response?.data?.message || err.message || "Failed to create practice") } finally { setSaving(false) } } return ( 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.

    {STEPS.map((s) => { const done = step > s.n const active = step === s.n return (
  1. {done ? : {s.n}} {s.label}
  2. ) })}
{!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).

)} {canUseWizard && step === 1 && (

Question set title

setSetTitle(e.target.value)} placeholder='e.g. "Course-A1 practice"' disabled={saving} />

This calls POST /question-sets with{" "} set_type: PRACTICE.

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

Set id #{questionSetId} — add one or more AUDIO questions. Each is created via{" "} POST /questions.

{questionRows.map((row, idx) => (
Question {idx + 1} {questionRows.length > 1 && ( )}

Question text