'use client'; import { useCallback, useEffect, useState } from 'react'; import Link from 'next/link'; import { api, errMsg } from '@/lib/api'; import { useSession } from '@/lib/session'; import { Alert, Empty, fmt, OrderStatusBadge, type Price, PriceText } from '@/components/ui'; interface O { id: string; number: string; orgName: string; status: string; createdAt: string; failureNote: string | null; failureAmbiguous: boolean; note: string | null; items: { id: string; quantity: number; contractId: string | null; snapshot: Price & { name: string; customerType: string } }[] } export default function Bestellungen() { const { can, me } = useSession(); const staff = can('orders.read'); const approver = can('orders.approve'); const [list, setList] = useState(null); const [msg, setMsg] = useState<{ k: 'ok' | 'err'; t: string } | null>(null); const [busy, setBusy] = useState(''); const [chk, setChk] = useState>({}); const load = useCallback(() => api('GET', '/orders').then(setList).catch((e) => setMsg({ k: 'err', t: errMsg(e) })), []); useEffect(() => { void load(); const t = setInterval(() => void load(), 5000); return () => clearInterval(t); }, [load]); async function act(o: O, path: string, body?: unknown, ok = 'Erledigt.') { setBusy(o.id); try { await api('POST', path, body); setMsg({ k: 'ok', t: ok }); void load(); } catch (x) { setMsg({ k: 'err', t: errMsg(x) }); } finally { setBusy(''); } } const canOrder = !!me && (staff ? can('orders.write') : me.organizations.some((o) => o.role !== 'member')); return (<>

Bestellungen

{canOrder && Neue Bestellung}
{msg && {msg.t}} {list === null ?

Wird geladen …

: list.length === 0 ?
: list.map((o) => (

{o.number}{staff && · {o.orgName}}

Bestellt am {fmt(o.createdAt)}{o.note ? ` · „${o.note}“` : ''}

{o.items.map((i) =>
{i.quantity} × {i.snapshot.name}{i.contractId && <> · Vertrag}
)} {o.status === 'failed' && o.failureNote && {o.failureNote}} {o.status === 'provisioning' &&

Die Bereitstellung läuft im Hintergrund. Diese Seite aktualisiert sich automatisch.

}
{approver && o.status === 'pending_approval' && <> } {approver && o.status === 'failed' && <>{o.failureAmbiguous && } } {(o.status === 'pending_approval' || (can('orders.write') && ['approved', 'failed'].includes(o.status))) && }
))} ); }