From cf5719883cad29dcf000cad699b488faa4412136 Mon Sep 17 00:00:00 2001 From: weeihan Date: Wed, 14 Oct 2026 11:08:18 +0800 Subject: [PATCH] feat(report): site and zone picker when no QR zone is detected Co-Authored-By: Claude Sonnet 5.5 --- app/report/page.tsx | 33 ++++++++++++++---- components/incidents/report-form.tsx | 51 +++++++++++++++++++++++++--- lib/report/zones.ts | 33 ++++++++++++++++++ messages/en.json | 6 +++- messages/ms.json | 6 +++- messages/zh.json | 6 +++- tests/lib/report/zones.test.ts | 46 +++++++++++++++++++++++++ 7 files changed, 168 insertions(+), 13 deletions(-) create mode 100644 lib/report/zones.ts create mode 100644 tests/lib/report/zones.test.ts diff --git a/app/report/page.tsx b/app/report/page.tsx index 5dab647..f3c0f67 100644 --- a/app/report/page.tsx +++ b/app/report/page.tsx @@ -3,8 +3,9 @@ export const dynamic = 'force-dynamic' import { redirect } from 'next/navigation' import { asAdmin } from '@/lib/db/with-user' import { getSession } from '@/lib/auth/get-session' -import { zones, sites, trucks } from '@/lib/db/schema' -import { eq, asc } from 'drizzle-orm' +import { zones, sites, trucks, users } from '@/lib/db/schema' +import { eq, asc, and } from 'drizzle-orm' +import { groupSites } from '@/lib/report/zones' import { ReportForm } from '@/components/incidents/report-form' import { LanguageSwitcher } from '@/components/language-switcher' import { OfflineSync } from '@/components/incidents/offline-sync' @@ -55,6 +56,26 @@ export default async function ReportPage({ searchParams }: Props) { carrier: t.carrier ?? null, })) + const zoneRows = await asAdmin(db => + db.select({ + siteId: sites.id, + siteName: sites.name, + zoneId: zones.id, + zoneName: zones.name, + token: zones.qrCodeToken, + }) + .from(zones) + .innerJoin(sites, eq(zones.siteId, sites.id)) + .where(and(eq(zones.active, true), eq(sites.active, true))) + .orderBy(asc(sites.name), asc(zones.name)) + ) + const siteOptions = groupSites(zoneRows) + + const [me] = await asAdmin(db => + db.select({ siteId: users.siteId }).from(users).where(eq(users.id, session.sub)).limit(1) + ) + const defaultSiteId = me?.siteId ?? '' + return (
@@ -62,18 +83,18 @@ export default async function ReportPage({ searchParams }: Props) {

{/* will be translated via ReportForm */}Report an Incident

- {zoneName ? ( + {zoneName && (

{siteName ?? 'Unknown Site'} — {zoneName}

- ) : ( -

No zone detected — zone will not be recorded

)} 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 && ( +
+
+ + +
+
+ + +
+
+ )} +