'use client' import { useCallback, useEffect, useState } from 'react' import Link from 'next/link' import { useLocale, useTranslations } from 'next-intl' import { ClipboardCheck } from 'lucide-react' import { Checkbox } from '@/components/ui/checkbox' import { Skeleton } from '@/components/ui/skeleton' import { QUIET_LINK_CLASS, HOVER_REVEAL_CLASS } from '@/components/ui/dry-table' import { useToast } from '@/components/ui/use-toast' import { cn, formatDate } from '@/lib/utils' import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message' import type { BokslutChecklist as Checklist, ChecklistGroup, ChecklistItem, ChecklistState } from '@/lib/bokslut/checklist' /** * The bokslut checklist on the wizard's Kontroll step: every closing step * grouped as the work goes, the system-judged ones read-only with their * computed state, the manual ones ticked here and stored per period * (bokslut_checklist_items). Any step can be marked not applicable. */ const GROUP_ORDER: ChecklistGroup[] = ['avstamning', 'periodisering', 'vardering', 'dispositioner', 'kontroll', 'rapportering'] interface BokslutChecklistProps { periodId: string } export function BokslutChecklist({ periodId }: BokslutChecklistProps) { const t = useTranslations('bokslut_checklist') const locale = useLocale() const { toast } = useToast() const [checklist, setChecklist] = useState(null) const [failed, setFailed] = useState(false) const [busy, setBusy] = useState(null) const base = `/api/bookkeeping/fiscal-periods/${periodId}/bokslut-checklist` const load = useCallback(async () => { try { const res = await fetch(base) if (!res.ok) { setFailed(true) return } const json = await res.json() setChecklist(json.data as Checklist) setFailed(false) } catch { setFailed(true) } }, [base]) useEffect(() => { void load() }, [load]) async function set(item: ChecklistItem, state: ChecklistState) { setBusy(item.key) try { const res = await fetch(base, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ item_key: item.key, state }), }) const json = await res.json().catch(() => ({})) if (!res.ok) { toast({ title: t('save_failed'), description: getUserErrorMessage(json, { statusCode: res.status }), variant: 'destructive' }) return } setChecklist(json.data as Checklist) } finally { setBusy(null) } } if (failed) return null if (!checklist) { return (
) } const label = (item: ChecklistItem) => (locale === 'en' ? item.label_en : item.label_sv) return (
) }