Separate retired learning fixtures from the makeup academy catalog

This commit is contained in:
Yared Yemane 2026-11-03 12:17:17 +03:00
parent b08ab8fc4d
commit 7ae57b93f4
2 changed files with 18 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import type { QuestionSetQuestion } from "../types/course.types"
export type LearningKind = "course" | "chapter" | "lesson" | "quiz"
export type LearningItem = {
id: number
is_archived?: boolean
course_id?: number
chapter_id?: number
name?: string

View File

@ -32,6 +32,7 @@ export function LearningHierarchyPage() {
const [failed, setFailed] = useState(false)
const [busy, setBusy] = useState(false)
const [search, setSearch] = useState("")
const [showLegacy, setShowLegacy] = useState(false)
const [editor, setEditor] = useState<Editor | null>(null)
const [deleting, setDeleting] = useState<{ item: Item; kind: Kind } | null>(null)
const [form, setForm] = useState({ name: "", description: "", thumbnail: "", video_url: "", access_tier: "FREE" })
@ -100,9 +101,10 @@ export function LearningHierarchyPage() {
catch (error) { notifyApiError(error, "Unable to delete content") }
finally { setBusy(false) }
}
const movableIndices = items.flatMap((item, index) => kind !== "course" || !item.is_archived ? [index] : [])
async function move(index: number, direction: number) {
const ordered = [...items]; const target = index + direction
if (target < 0 || target >= ordered.length) return
const ordered = [...items]; const target = movableIndices[movableIndices.indexOf(index) + direction]
if (target === undefined) return
;[ordered[index], ordered[target]] = [ordered[target], ordered[index]]
setBusy(true)
try { await http.put(`${path}/reorder`, { ordered_ids: ordered.map(item => item.id) }); setItems(ordered) }
@ -110,15 +112,15 @@ export function LearningHierarchyPage() {
finally { setBusy(false) }
}
const title = chapter?.name ?? course?.name ?? "Courses"
const matches = (item: Item) => (item.name ?? item.title ?? "").toLowerCase().includes(search.toLowerCase())
const matches = (item: Item) => (kind !== "course" || !!item.is_archived === showLegacy) && (item.name ?? item.title ?? "").toLowerCase().includes(search.toLowerCase())
function card(item: Item, itemKind: Kind, index?: number) {
return <article key={item.id} className="rounded-xl border border-grayScale-200 bg-white p-5 dark:bg-grayScale-50">
<div className="flex flex-wrap items-start justify-between gap-3">
<div className="min-w-0"><h3 className="text-lg font-semibold">{item.name ?? item.title}</h3><p className="mt-1 text-sm text-grayScale-500">{item.publish_status === "PUBLISHED" ? "Published" : "Draft"}{item.access_tier ? ` · ${item.access_tier.toLowerCase()}` : ""}</p></div>
<div className="flex gap-1">
{index !== undefined && !search && <><Button size="icon" variant="ghost" disabled={busy || index === 0} onClick={() => void move(index, -1)} aria-label="Move up"><ArrowUp size={16} /></Button><Button size="icon" variant="ghost" disabled={busy || index === items.length - 1} onClick={() => void move(index, 1)} aria-label="Move down"><ArrowDown size={16} /></Button></>}
<Button size="icon" variant="ghost" disabled={busy} onClick={() => openEditor({ kind: itemKind, item })} aria-label="Edit"><Pencil size={16} /></Button>
<Button size="icon" variant="ghost" disabled={busy} onClick={() => setDeleting({ item, kind: itemKind })} aria-label="Delete"><Trash2 size={16} /></Button>
{index !== undefined && !search && !showLegacy && <><Button size="icon" variant="ghost" disabled={busy || index === movableIndices[0]} onClick={() => void move(index, -1)} aria-label="Move up"><ArrowUp size={16} /></Button><Button size="icon" variant="ghost" disabled={busy || index === movableIndices[movableIndices.length - 1]} onClick={() => void move(index, 1)} aria-label="Move down"><ArrowDown size={16} /></Button></>}
<Button size="icon" variant="ghost" disabled={busy || item.is_archived} onClick={() => openEditor({ kind: itemKind, item })} aria-label="Edit"><Pencil size={16} /></Button>
<Button size="icon" variant="ghost" disabled={busy || item.is_archived} onClick={() => setDeleting({ item, kind: itemKind })} aria-label="Delete"><Trash2 size={16} /></Button>
</div>
</div>
{item.description && <p className="mt-3 line-clamp-3 text-sm text-grayScale-600">{item.description}</p>}
@ -127,7 +129,7 @@ export function LearningHierarchyPage() {
{itemKind === "course" && <Button asChild variant="outline"><Link to={`/new-content/courses/${item.id}`}>Chapters</Link></Button>}
{itemKind === "chapter" && <Button asChild variant="outline"><Link to={`/new-content/courses/${courseId}/chapters/${item.id}`}>Lessons & quizzes</Link></Button>}
{itemKind === "quiz" && <Button asChild variant="outline"><Link to={`/content/question-sets/${item.question_set_id}/questions`} state={{ from: `/new-content/courses/${courseId}/chapters/${chapterId}` }}>Edit questions</Link></Button>}
<Button variant="outline" disabled={busy} onClick={() => void publish(item, itemKind)}>{item.publish_status === "PUBLISHED" ? "Unpublish" : "Publish"}</Button>
<Button variant="outline" disabled={busy || item.is_archived} onClick={() => void publish(item, itemKind)}>{item.is_archived ? "Archived" : item.publish_status === "PUBLISHED" ? "Unpublish" : "Publish"}</Button>
</div>
</article>
}
@ -135,9 +137,16 @@ export function LearningHierarchyPage() {
<nav className="flex flex-wrap items-center gap-2 text-sm text-grayScale-500"><Link to="/new-content/courses">Courses</Link>{courseId && <><span>/</span><Link to={`/new-content/courses/${courseId}`}>{course?.name ?? "Course"}</Link></>}{chapterId && <><span>/</span><span>{chapter?.name ?? "Chapter"}</span></>}</nav>
<header className="flex flex-wrap items-center justify-between gap-4"><div><h1 className="flex items-center gap-3 text-2xl font-bold"><BookOpen />{title}</h1><p className="mt-2 text-grayScale-500">{chapterId ? "Manage the lessons and quizzes in this chapter." : courseId ? "Organize this course into chapters." : "Create courses, organize chapters, and add lessons and chapter quizzes."}</p></div><Button disabled={busy || loading || failed} onClick={() => openEditor({ kind })}><Plus size={16} className="mr-2" />Add {kind}</Button></header>
{courseId && <Button asChild variant="ghost"><Link to={chapterId ? `/new-content/courses/${courseId}` : "/new-content/courses"}><ArrowLeft size={16} className="mr-2" />Back to {chapterId ? "chapters" : "courses"}</Link></Button>}
{kind === "course" && <div className="space-y-3">
<div className="flex flex-wrap gap-2" role="group" aria-label="Course catalog view">
<Button variant={showLegacy ? "outline" : "default"} aria-pressed={!showLegacy} onClick={() => setShowLegacy(false)}>Academy courses</Button>
<Button variant={showLegacy ? "default" : "outline"} aria-pressed={showLegacy} onClick={() => setShowLegacy(true)}>Legacy archive ({items.filter(item => item.is_archived).length})</Button>
</div>
{showLegacy && <p className="text-sm text-grayScale-500">Retired general-learning fixtures are kept here to preserve historical records and learner progress. They are unavailable in the learner catalog.</p>}
</div>}
<Input aria-label="Search content" placeholder="Search by title…" value={search} onChange={event => setSearch(event.target.value)} />
{loading ? <p role="status">Loading content…</p> : failed ? <div role="alert"><p>Content could not be loaded.</p><Button onClick={() => void load()}>Try again</Button></div> : <>
<section className="space-y-4"><h2 className="text-xl font-semibold capitalize">{collection[kind]}</h2><div className="grid gap-4 md:grid-cols-2">{items.map((item, index) => matches(item) ? card(item, kind, index) : null)}</div>{!items.some(matches) && <p className="rounded-xl border border-dashed p-8 text-center text-grayScale-500">{search ? "No matching content." : `No ${collection[kind]} yet. Add your first ${kind} to get started.`}</p>}</section>
<section className="space-y-4"><h2 className="text-xl font-semibold capitalize">{collection[kind]}</h2><div className="grid gap-4 md:grid-cols-2">{items.map((item, index) => matches(item) ? card(item, kind, index) : null)}</div>{!items.some(matches) && <p className="rounded-xl border border-dashed p-8 text-center text-grayScale-500">{search ? "No matching content." : showLegacy ? "No archived courses." : `No ${collection[kind]} yet. Add your first ${kind} to get started.`}</p>}</section>
{chapterId && <section className="space-y-4 border-t pt-6"><div className="flex items-center justify-between"><h2 className="text-xl font-semibold">Chapter quizzes</h2><Button disabled={busy} onClick={() => openEditor({ kind: "quiz" })}><Plus size={16} className="mr-2" />Add quiz</Button></div><div className="grid gap-4 md:grid-cols-2">{quizzes.filter(matches).map(item => card(item, "quiz"))}</div>{!quizzes.some(matches) && <p className="text-grayScale-500">{search ? "No matching quizzes." : "No quizzes yet. Create a quiz, add its questions, then publish it."}</p>}</section>}
</>}
<Dialog open={!!editor} onOpenChange={open => { if (!open && !busy) setEditor(null) }}><DialogContent><DialogHeader><DialogTitle>{editor?.item ? "Edit" : "Add"} {editor?.kind}</DialogTitle></DialogHeader><form onSubmit={save} className="space-y-4"><label className="block space-y-2"><span>Title</span><Input autoFocus required value={form.name} onChange={event => setForm({ ...form, name: event.target.value })} /></label>