(null)
+
+ const check = validateCapaSubmission(note, files)
+
+ function addFiles(list: FileList | null) {
+ if (!list) return
+ setFiles(prev => [...prev, ...Array.from(list)].slice(0, MAX_CAPA_PHOTOS))
+ }
+
+ async function handleSubmit(e: React.FormEvent) {
+ e.preventDefault()
+ if (!check.ok) return
+ setLoading(true)
+ setError(null)
+ try {
+ const fd = new FormData()
+ fd.append('note', check.note)
+ files.forEach(f => fd.append('files', f))
+ const res = await fetch(`/api/capa/${capaId}/submit`, { method: 'POST', body: fd })
+ if (!res.ok) {
+ const data = await res.json().catch(() => ({}))
+ setError(Array.isArray(data.details) ? data.details.join('. ') : (data.error ?? 'Submit failed. Please try again.'))
+ setLoading(false)
+ return
+ }
+ router.refresh()
+ return
+ } catch {
+ setError('Network error. Please try again.')
+ }
+ setLoading(false)
+ }
+
+ return (
+
+ )
+}
diff --git a/components/incidents/report-form.tsx b/components/incidents/report-form.tsx
index c8e3a22..fe64adb 100644
--- a/components/incidents/report-form.tsx
+++ b/components/incidents/report-form.tsx
@@ -5,16 +5,19 @@ import { useRouter } from 'next/navigation'
import { FileUpload } from '@/components/incidents/file-upload'
import type { IncidentType, MedicalStatus } from '@/lib/incidents/validate'
import { useTranslations } from '@/lib/i18n/context'
+import { zonesForSite, resolveZoneToken, type SiteOption } from '@/lib/report/zones'
interface Props {
zoneToken: string | null
zoneName: string | null
siteName: string | null
+ sites: SiteOption[]
+ defaultSiteId: string
trucks: Array<{ id: string; truck_no: string; carrier: string | null }>
initialTruckId?: string | null
}
-export function ReportForm({ zoneToken, trucks, initialTruckId }: Props) {
+export function ReportForm({ zoneToken, sites, defaultSiteId, trucks, initialTruckId }: Props) {
const router = useRouter()
const t = useTranslations('ReportForm')
const itLabels = useTranslations('IncidentType')
@@ -52,6 +55,12 @@ export function ReportForm({ zoneToken, trucks, initialTruckId }: Props) {
} | null>(null)
const [overrideQuality, setOverrideQuality] = useState(false)
+ const [siteId, setSiteId] = useState(
+ sites.some(s => s.id === defaultSiteId) ? defaultSiteId : (sites.length === 1 ? sites[0].id : '')
+ )
+ const [zoneId, setZoneId] = useState('')
+ const effectiveZoneToken = resolveZoneToken({ zoneToken, sites, zoneId })
+
const [form, setForm] = useState({
incident_type: (initialTruckId ? 'transport' : '') as IncidentType | '',
description: '',
@@ -113,7 +122,7 @@ export function ReportForm({ zoneToken, trucks, initialTruckId }: Props) {
try {
const { addPendingReport } = await import('@/lib/offline/db')
await addPendingReport({
- zone_token: zoneToken ?? '',
+ zone_token: effectiveZoneToken ?? '',
incident_type: form.incident_type as IncidentType,
description: form.description,
injury_involved: form.injury_involved,
@@ -162,7 +171,7 @@ export function ReportForm({ zoneToken, trucks, initialTruckId }: Props) {
try {
const fd = new FormData()
- if (zoneToken) fd.append('zone_token', zoneToken)
+ if (effectiveZoneToken) fd.append('zone_token', effectiveZoneToken)
fd.append('incident_type', form.incident_type)
fd.append('description', form.description)
fd.append('injury_involved', String(form.injury_involved))
@@ -225,6 +234,40 @@ export function ReportForm({ zoneToken, trucks, initialTruckId }: Props) {
)}
+ {!zoneToken && (
+
+
+
+
+
+
+
+
+
+
+ )}
+