'use client' import { useState } from 'react' import { useRouter } from 'next/navigation' export type SiteWithZones = { id: string name: string address: string | null active: boolean zones: Array<{ id: string; name: string; qr_code_token: string; active: boolean }> } interface Props { sites: SiteWithZones[] } export function SiteZoneManager({ sites }: Props) { const router = useRouter() const [siteName, setSiteName] = useState('') const [zoneName, setZoneName] = useState('') const [zoneSiteId, setZoneSiteId] = useState('') const [busy, setBusy] = useState(false) const [busyId, setBusyId] = useState(null) const [error, setError] = useState(null) const post = async (payload: Record) => { setBusy(true) setError(null) const res = await fetch('/api/admin/sites', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }) setBusy(false) if (!res.ok) { const data = await res.json().catch(() => ({})) setError(data.error ?? 'Save failed') return false } router.refresh() return true } const send = async (method: 'PATCH' | 'DELETE', payload: Record) => { const id = payload.id as string setBusyId(id) setError(null) const res = await fetch('/api/admin/sites', { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }) setBusyId(null) if (!res.ok) { const data = await res.json().catch(() => ({})) setError(data.error ?? 'Action failed') return false } router.refresh() return true } return (

Sites & Zones

setSiteName(e.target.value)} className="border border-gray-300 rounded-lg px-3 py-2 text-sm w-48" />
setZoneName(e.target.value)} className="border border-gray-300 rounded-lg px-3 py-2 text-sm w-48" />
{error &&

{error}

}
{sites.map(site => (

{site.name}

{site.address &&

{site.address}

}
{site.zones.length > 0 ? (
    {site.zones.map(z => (
  • {z.name}
    Report link / QR target
  • ))}
) : (

No zones

)}
))} {sites.length === 0 &&

No sites yet

}
) }