From 701fefd286e523be8c978cdfe4f4d407a795d62b Mon Sep 17 00:00:00 2001 From: weeihan Date: Thu, 15 Oct 2026 13:52:46 +0800 Subject: [PATCH] feat(report): show all trucks regardless of site Co-Authored-By: Claude Sonnet 5.5 --- components/incidents/report-form.tsx | 14 ++++--------- .../incidents/report-form-trucks.test.tsx | 20 +++++++++---------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/components/incidents/report-form.tsx b/components/incidents/report-form.tsx index f75116f..0b957b3 100644 --- a/components/incidents/report-form.tsx +++ b/components/incidents/report-form.tsx @@ -1,12 +1,12 @@ 'use client' -import { useState, useEffect, useMemo } from 'react' +import { useState, useEffect } 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, siteIdForToken, type SiteOption } from '@/lib/report/zones' -import { trucksForSite, siteForTruck } from '@/lib/report/trucks' +import { zonesForSite, resolveZoneToken, type SiteOption } from '@/lib/report/zones' +import { siteForTruck } from '@/lib/report/trucks' interface Props { zoneToken: string | null @@ -64,8 +64,6 @@ export function ReportForm({ zoneToken, sites, defaultSiteId, trucks, initialTru ) 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 | '', @@ -75,10 +73,6 @@ 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) => @@ -336,7 +330,7 @@ export function ReportForm({ zoneToken, sites, defaultSiteId, trucks, initialTru onChange={e => setForm(f => ({ ...f, truck_id: e.target.value }))} > - {truckOptions.map(tk => ( + {trucks.map(tk => ( diff --git a/tests/components/incidents/report-form-trucks.test.tsx b/tests/components/incidents/report-form-trucks.test.tsx index 2ca356e..992b07a 100644 --- a/tests/components/incidents/report-form-trucks.test.tsx +++ b/tests/components/incidents/report-form-trucks.test.tsx @@ -39,26 +39,26 @@ const selectWith = (text: string) => 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', () => { +describe('ReportForm truck list', () => { + const all = [en.ReportForm.truckPlaceholder, 'AAA 1', 'BBB 2', 'FREE 3'] + + it('shows every truck regardless of site', () => { renderForm() - expect(selectWith(en.ReportForm.sitePlaceholder).value).toBe('site-a') - expect(truckNos()).toEqual([en.ReportForm.truckPlaceholder, 'AAA 1', 'FREE 3']) + expect(truckNos()).toEqual(all) }) - it('changing the site refilters and clears a truck that no longer fits', () => { + it('keeps the chosen truck when the site changes', () => { 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('') + expect(truckNos()).toEqual(all) + expect(selectWith(en.ReportForm.truckPlaceholder).value).toBe('t1') }) - it('uses the QR zone site when a zone token is present', () => { + it('shows every truck with a QR zone token', () => { 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']) + expect(truckNos()).toEqual(all) }) })