feat(report): show all trucks regardless of site
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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<Record<string, string | boolean>>({})
|
||||
|
||||
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 }))}
|
||||
>
|
||||
<option value="">{t.truckPlaceholder}</option>
|
||||
{truckOptions.map(tk => (
|
||||
{trucks.map(tk => (
|
||||
<option key={tk.id} value={tk.id}>
|
||||
{tk.truck_no}{tk.carrier ? ` — ${tk.carrier}` : ''}
|
||||
</option>
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user