feat(report): filter truck list by selected site
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -44,6 +44,7 @@ export default async function ReportPage({ searchParams }: Props) {
|
|||||||
id: trucks.id,
|
id: trucks.id,
|
||||||
truckNo: trucks.truckNo,
|
truckNo: trucks.truckNo,
|
||||||
carrier: trucks.carrier,
|
carrier: trucks.carrier,
|
||||||
|
siteId: trucks.siteId,
|
||||||
})
|
})
|
||||||
.from(trucks)
|
.from(trucks)
|
||||||
.where(eq(trucks.active, true))
|
.where(eq(trucks.active, true))
|
||||||
@@ -54,6 +55,7 @@ export default async function ReportPage({ searchParams }: Props) {
|
|||||||
id: t.id,
|
id: t.id,
|
||||||
truck_no: t.truckNo,
|
truck_no: t.truckNo,
|
||||||
carrier: t.carrier ?? null,
|
carrier: t.carrier ?? null,
|
||||||
|
site_id: t.siteId ?? null,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
const zoneRows = await asAdmin(db =>
|
const zoneRows = await asAdmin(db =>
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useState, useEffect } from 'react'
|
import { useState, useEffect, useMemo } from 'react'
|
||||||
import { useRouter } from 'next/navigation'
|
import { useRouter } from 'next/navigation'
|
||||||
import { FileUpload } from '@/components/incidents/file-upload'
|
import { FileUpload } from '@/components/incidents/file-upload'
|
||||||
import type { IncidentType, MedicalStatus } from '@/lib/incidents/validate'
|
import type { IncidentType, MedicalStatus } from '@/lib/incidents/validate'
|
||||||
import { useTranslations } from '@/lib/i18n/context'
|
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 {
|
interface Props {
|
||||||
zoneToken: string | null
|
zoneToken: string | null
|
||||||
@@ -13,7 +14,7 @@ interface Props {
|
|||||||
siteName: string | null
|
siteName: string | null
|
||||||
sites: SiteOption[]
|
sites: SiteOption[]
|
||||||
defaultSiteId: string
|
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
|
initialTruckId?: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,11 +56,16 @@ export function ReportForm({ zoneToken, sites, defaultSiteId, trucks, initialTru
|
|||||||
} | null>(null)
|
} | null>(null)
|
||||||
const [overrideQuality, setOverrideQuality] = useState(false)
|
const [overrideQuality, setOverrideQuality] = useState(false)
|
||||||
|
|
||||||
|
const truckSite = !zoneToken ? siteForTruck(trucks, initialTruckId) : null
|
||||||
const [siteId, setSiteId] = useState(
|
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 [zoneId, setZoneId] = useState('')
|
||||||
const effectiveZoneToken = resolveZoneToken({ zoneToken, sites, zoneId })
|
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({
|
const [form, setForm] = useState({
|
||||||
incident_type: (initialTruckId ? 'transport' : '') as IncidentType | '',
|
incident_type: (initialTruckId ? 'transport' : '') as IncidentType | '',
|
||||||
@@ -69,6 +75,10 @@ export function ReportForm({ zoneToken, sites, defaultSiteId, trucks, initialTru
|
|||||||
asset_involved: false,
|
asset_involved: false,
|
||||||
truck_id: initialTruckId ?? '',
|
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 [typeDetails, setTypeDetails] = useState<Record<string, string | boolean>>({})
|
||||||
|
|
||||||
const setDetail = (key: string, value: string | boolean) =>
|
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 }))}
|
onChange={e => setForm(f => ({ ...f, truck_id: e.target.value }))}
|
||||||
>
|
>
|
||||||
<option value="">{t.truckPlaceholder}</option>
|
<option value="">{t.truckPlaceholder}</option>
|
||||||
{trucks.map(tk => (
|
{truckOptions.map(tk => (
|
||||||
<option key={tk.id} value={tk.id}>
|
<option key={tk.id} value={tk.id}>
|
||||||
{tk.truck_no}{tk.carrier ? ` — ${tk.carrier}` : ''}
|
{tk.truck_no}{tk.carrier ? ` — ${tk.carrier}` : ''}
|
||||||
</option>
|
</option>
|
||||||
|
|||||||
@@ -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<React.ComponentProps<typeof ReportForm>> = {}) {
|
||||||
|
return render(
|
||||||
|
<I18nProvider messages={en}>
|
||||||
|
<ReportForm
|
||||||
|
zoneToken={null}
|
||||||
|
zoneName={null}
|
||||||
|
siteName={null}
|
||||||
|
sites={sites}
|
||||||
|
defaultSiteId=""
|
||||||
|
trucks={trucks}
|
||||||
|
initialTruckId="t1"
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
</I18nProvider>,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
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'])
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user