import { useEffect, useState } from "react" import { Link, useNavigate, useParams } from "react-router-dom" import { ArrowLeft, BookOpen, ChevronRight } from "lucide-react" import spinnerSrc from "../../assets/Circular-indeterminate progress indicator.svg" import alertSrc from "../../assets/Alert.svg" import { Badge } from "../../components/ui/badge" import { getCoursesBySubCategoryId, getSubCategoriesByCategoryId } from "../../api/courses.api" import type { CategorySubCategoryListItem, SubCategoryCourseListItem } from "../../types/course.types" import { cn } from "../../lib/utils" export function SubCategoryCoursesPage() { const { categoryId, subCategoryId } = useParams<{ categoryId: string subCategoryId: string }>() const navigate = useNavigate() const [subCategory, setSubCategory] = useState(null) const [courses, setCourses] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { const run = async () => { if (!categoryId || !subCategoryId) return const cid = Number(categoryId) const sid = Number(subCategoryId) if (!Number.isFinite(cid) || !Number.isFinite(sid)) { setError("Invalid route parameters") setLoading(false) return } setLoading(true) setError(null) try { const [subRes, coursesRes] = await Promise.all([ getSubCategoriesByCategoryId(cid), getCoursesBySubCategoryId(sid), ]) const list = subRes.data?.data?.sub_categories ?? [] const found = Array.isArray(list) ? list.find((s) => s.id === sid) : undefined setSubCategory(found ?? null) const raw = coursesRes.data?.data?.courses setCourses(Array.isArray(raw) ? raw : []) } catch (e) { console.error(e) setError("Failed to load courses for this sub-category") setCourses([]) } finally { setLoading(false) } } void run() }, [categoryId, subCategoryId]) if (loading) { return (
) } if (error) { return (

{error}

) } const label = subCategory?.name ?? "Sub-category" return (

Sub-category

{label}

{courses.length} course{courses.length !== 1 ? "s" : ""} — open a course to manage sub-modules

{courses.length === 0 ? (

No courses in this sub-category yet

Add a course from your authoring flow or API.

) : (
{courses.map((c) => ( ))}
)}
) }