import { useEffect, useState } from 'react'; import { Link, useParams } from 'react-router-dom'; import type { Card, LessonTreeNode } from '@flashcard/shared'; import { cardsApi } from '../api/cards.js'; import { lessonsApi } from '../api/lessons.js'; import { adminLessonsApi } from '../api/admin-lessons.js'; import { useAuth } from '../stores/authStore.js'; import { useLessons } from '../stores/lessonsStore.js'; import { CardTable } from '../components/CardTable.js'; import { ImportDialog } from '../components/ImportDialog.js'; import { ApiClientError } from '../api/client.js'; function findLesson(tree: LessonTreeNode[], id: number): LessonTreeNode | null { for (const n of tree) { if (n.id === id) return n; const found = findLesson(n.children, id); if (found) return found; } return null; } export function AdminLessonPage() { const { id } = useParams(); const lessonId = Number(id); const user = useAuth((s) => s.user); const { tree, refresh: refreshTree } = useLessons(); const [cards, setCards] = useState([]); const [showImport, setShowImport] = useState(false); const [busy, setBusy] = useState(false); const node = findLesson(tree, lessonId); const isOwner = node?.ownerId === user?.id; const visibility = node?.visibility ?? 'private'; const isCurated = node?.isCurated ?? false; async function refresh() { try { setCards(await cardsApi.list(lessonId)); } catch (e) { if (e instanceof ApiClientError && e.status === 403) setCards([]); else throw e; } } useEffect(() => { refresh(); refreshTree(); }, [lessonId]); async function toggleVisibility() { setBusy(true); try { const next = visibility === 'shared' ? 'private' : 'shared'; await lessonsApi.setVisibility(lessonId, next); await refreshTree(); } finally { setBusy(false); } } async function toggleCurated() { if (!user || user.role !== 'sysadmin') return; setBusy(true); try { await adminLessonsApi.setCurated(lessonId, !isCurated); await refreshTree(); } finally { setBusy(false); } } async function forkThis() { setBusy(true); try { const fork = await lessonsApi.fork(lessonId); await refreshTree(); window.location.href = `/admin/lessons/${fork.id}`; } finally { setBusy(false); } } async function unsubscribeThis() { setBusy(true); try { await lessonsApi.unsubscribe(lessonId); await refreshTree(); window.location.href = '/admin'; } finally { setBusy(false); } } return (
← Terug naar lessen

Kaartenbeheer {!isOwner && πŸ“₯ Geabonneerd} {isCurated && ⭐ Curated}

{cards.length} {cards.length === 1 ? 'kaart' : 'kaarten'} in deze les

{isOwner ? ( <> {user?.role === 'sysadmin' && visibility === 'shared' && ( )} πŸ“€ Exporteer Start oefenen β†’ ) : ( <> Start oefenen β†’ )}
{!isOwner && (
Je bent geabonneerd op deze les en kunt kaarten alleen bekijken. Klik op 🍴 Fork voor een eigen, bewerkbare kopie.
)}
{showImport && setShowImport(false)} onDone={refresh} />}
); }