From 5f1d7a6ed4ed998b90961b6e4021cb91ed31ee55 Mon Sep 17 00:00:00 2001 From: weeihan Date: Thu, 15 Oct 2026 11:50:00 +0800 Subject: [PATCH] feat(report): filter truck list by selected site Co-Authored-By: Claude Sonnet 5.5 --- app/report/page.tsx | 2 + components/incidents/report-form.tsx | 20 ++++-- .../incidents/report-form-trucks.test.tsx | 64 +++++++++++++++++++ 3 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 tests/components/incidents/report-form-trucks.test.tsx diff --git a/app/report/page.tsx b/app/report/page.tsx index f3c0f67..bd23bf4 100644 --- a/app/report/page.tsx +++ b/app/report/page.tsx @@ -44,6 +44,7 @@ export default async function ReportPage({ searchParams }: Props) { id: trucks.id, truckNo: trucks.truckNo, carrier: trucks.carrier, + siteId: trucks.siteId, }) .from(trucks) .where(eq(trucks.active, true)) @@ -54,6 +55,7 @@ export default async function ReportPage({ searchParams }: Props) { id: t.id, truck_no: t.truckNo, carrier: t.carrier ?? null, + site_id: t.siteId ?? null, })) const zoneRows = await asAdmin(db => diff --git a/components/incidents/report-form.tsx b/components/incidents/report-form.tsx index fe64adb..f75116f 100644 --- a/components/incidents/report-form.tsx +++ b/components/incidents/report-form.tsx @@ -1,11 +1,12 @@ 'use client' -import { useState, useEffect } from 'react' +import { useState, useEffect, useMemo } from 'react' 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' +import { zonesForSite, resolveZoneToken, siteIdForToken, type SiteOption } from '@/lib/report/zones' +import { trucksForSite, siteForTruck } from '@/lib/report/trucks' interface Props { zoneToken: string | null @@ -13,7 +14,7 @@ interface Props { siteName: string | null sites: SiteOption[] defaultSiteId: string - trucks: Array<{ id: string; truck_no: string; carrier: string | null }> + trucks: Array<{ id: string; truck_no: string; carrier: string | null; site_id: string | null }> initialTruckId?: string | null } @@ -55,11 +56,16 @@ export function ReportForm({ zoneToken, sites, defaultSiteId, trucks, initialTru } | null>(null) const [overrideQuality, setOverrideQuality] = useState(false) + const truckSite = !zoneToken ? siteForTruck(trucks, initialTruckId) : null const [siteId, setSiteId] = useState( - sites.some(s => s.id === defaultSiteId) ? defaultSiteId : (sites.length === 1 ? sites[0].id : '') + truckSite && sites.some(s => s.id === truckSite) + ? truckSite + : sites.some(s => s.id === defaultSiteId) ? defaultSiteId : (sites.length === 1 ? sites[0].id : '') ) const [zoneId, setZoneId] = useState('') const effectiveZoneToken = resolveZoneToken({ zoneToken, sites, zoneId }) + const effectiveSiteId = zoneToken ? siteIdForToken(sites, zoneToken) : siteId + const truckOptions = useMemo(() => trucksForSite(trucks, effectiveSiteId), [trucks, effectiveSiteId]) const [form, setForm] = useState({ incident_type: (initialTruckId ? 'transport' : '') as IncidentType | '', @@ -69,6 +75,10 @@ export function ReportForm({ zoneToken, sites, defaultSiteId, trucks, initialTru asset_involved: false, truck_id: initialTruckId ?? '', }) + useEffect(() => { + // eslint-disable-next-line react-hooks/set-state-in-effect + setForm(f => (f.truck_id && !truckOptions.some(tk => tk.id === f.truck_id) ? { ...f, truck_id: '' } : f)) + }, [truckOptions]) const [typeDetails, setTypeDetails] = useState>({}) const setDetail = (key: string, value: string | boolean) => @@ -326,7 +336,7 @@ export function ReportForm({ zoneToken, sites, defaultSiteId, trucks, initialTru onChange={e => setForm(f => ({ ...f, truck_id: e.target.value }))} > - {trucks.map(tk => ( + {truckOptions.map(tk => ( diff --git a/tests/components/incidents/report-form-trucks.test.tsx b/tests/components/incidents/report-form-trucks.test.tsx new file mode 100644 index 0000000..2ca356e --- /dev/null +++ b/tests/components/incidents/report-form-trucks.test.tsx @@ -0,0 +1,64 @@ +import { describe, it, expect, vi } from 'vitest' +import { render, screen, fireEvent, within } from '@testing-library/react' +import { ReportForm } from '@/components/incidents/report-form' +import { I18nProvider } from '@/lib/i18n/context' +import en from '@/messages/en.json' + +vi.mock('next/navigation', () => ({ useRouter: () => ({ push: vi.fn(), refresh: vi.fn() }) })) + +const sites = [ + { id: 'site-a', name: 'Site A', zones: [{ id: 'z1', name: 'Dock', qr_code_token: 'tok-a' }] }, + { id: 'site-b', name: 'Site B', zones: [{ id: 'z2', name: 'Yard', qr_code_token: 'tok-b' }] }, +] +const trucks = [ + { id: 't1', truck_no: 'AAA 1', carrier: null, site_id: 'site-a' }, + { id: 't2', truck_no: 'BBB 2', carrier: null, site_id: 'site-b' }, + { id: 't3', truck_no: 'FREE 3', carrier: null, site_id: null }, +] + +function renderForm(props: Partial> = {}) { + return render( + + + , + ) +} + +const selectWith = (text: string) => + screen.getAllByRole('combobox').find(s => within(s).queryByText(text)) as HTMLSelectElement + +const truckNos = () => + Array.from(selectWith(en.ReportForm.truckPlaceholder).options).map(o => o.textContent) + +describe('ReportForm truck filter', () => { + it('defaults the site to the preselected truck site and filters trucks', () => { + renderForm() + expect(selectWith(en.ReportForm.sitePlaceholder).value).toBe('site-a') + expect(truckNos()).toEqual([en.ReportForm.truckPlaceholder, 'AAA 1', 'FREE 3']) + }) + + it('changing the site refilters and clears a truck that no longer fits', () => { + renderForm() + expect(selectWith(en.ReportForm.truckPlaceholder).value).toBe('t1') + fireEvent.change(selectWith(en.ReportForm.sitePlaceholder), { target: { value: 'site-b' } }) + expect(truckNos()).toEqual([en.ReportForm.truckPlaceholder, 'BBB 2', 'FREE 3']) + expect(selectWith(en.ReportForm.truckPlaceholder).value).toBe('') + }) + + it('uses the QR zone site when a zone token is present', () => { + renderForm({ zoneToken: 'tok-b', zoneName: 'Yard', siteName: 'Site B', initialTruckId: null }) + fireEvent.change(screen.getAllByRole('combobox').find(s => within(s).queryByText(en.IncidentType.transport))!, { + target: { value: 'transport' }, + }) + expect(truckNos()).toEqual([en.ReportForm.truckPlaceholder, 'BBB 2', 'FREE 3']) + }) +})