import { useState } from 'react' import { createFileRoute, useNavigate } from '@tanstack/react-router' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { lessonPlanTemplateListOptions, lessonPlanTemplateMutations, lessonPlanTemplateKeys } from '@/api/lessons' import { usePagination } from '@/hooks/use-pagination' import { DataTable, type Column } from '@/components/shared/data-table' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { Input } from '@/components/ui/input' import { Plus, Search, Trash2 } from 'lucide-react' import { toast } from 'sonner' import { useAuthStore } from '@/stores/auth.store' import type { LessonPlanTemplate } from '@/types/lesson' export const Route = createFileRoute('/_authenticated/lessons/templates/')({ validateSearch: (search: Record) => ({ page: Number(search.page) || 1, limit: Number(search.limit) || 25, q: (search.q as string) || undefined, sort: (search.sort as string) || undefined, order: (search.order as 'asc' | 'desc') || 'asc', }), component: TemplatesListPage, }) const SKILL_LABELS: Record = { beginner: 'Beginner', intermediate: 'Intermediate', advanced: 'Advanced', all_levels: 'All Levels', } const SKILL_VARIANTS: Record = { beginner: 'outline', intermediate: 'secondary', advanced: 'default', all_levels: 'outline', } const columns: Column[] = [ { key: 'name', header: 'Name', sortable: true, render: (t) => {t.name} }, { key: 'instrument', header: 'Instrument', render: (t) => <>{t.instrument ?? } }, { key: 'skill_level', header: 'Level', sortable: true, render: (t) => {SKILL_LABELS[t.skillLevel] ?? t.skillLevel}, }, { key: 'sections', header: 'Sections', render: (t) => <>{t.sections?.length ?? 0} sections, }, { key: 'is_active', header: 'Status', render: (t) => {t.isActive ? 'Active' : 'Inactive'}, }, ] function TemplatesListPage() { const navigate = useNavigate() const queryClient = useQueryClient() const hasPermission = useAuthStore((s) => s.hasPermission) const canAdmin = hasPermission('lessons.admin') const { params, setPage, setSearch, setSort } = usePagination() const [searchInput, setSearchInput] = useState(params.q ?? '') const { data, isLoading } = useQuery(lessonPlanTemplateListOptions(params)) const deleteMutation = useMutation({ mutationFn: lessonPlanTemplateMutations.delete, onSuccess: () => { queryClient.invalidateQueries({ queryKey: lessonPlanTemplateKeys.all }) toast.success('Template deleted') }, onError: (err) => toast.error(err.message), }) function handleSearchSubmit(e: React.FormEvent) { e.preventDefault() setSearch(searchInput) } const columnsWithActions: Column[] = [ ...columns, ...(canAdmin ? [{ key: 'actions', header: '', render: (t: LessonPlanTemplate) => ( ), }] : []), ] return (

Lesson Plan Templates

{canAdmin && ( )}
setSearchInput(e.target.value)} className="pl-9" />
navigate({ to: '/lessons/templates/$templateId', params: { templateId: t.id }, search: { page: 1, limit: 25, q: undefined, sort: undefined, order: 'asc' as const } })} />
) }