feat(report): site and zone picker when no QR zone is detected
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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) {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!zoneToken && (
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">
|
||||
{t.siteLabel} <span className="text-red-500">*</span>
|
||||
</label>
|
||||
<select
|
||||
required
|
||||
className="w-full border border-divider px-3 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-accent"
|
||||
value={siteId}
|
||||
onChange={e => { setSiteId(e.target.value); setZoneId('') }}
|
||||
>
|
||||
<option value="">{t.sitePlaceholder}</option>
|
||||
{sites.map(s => <option key={s.id} value={s.id}>{s.name}</option>)}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">
|
||||
{t.zoneLabel} <span className="text-red-500">*</span>
|
||||
</label>
|
||||
<select
|
||||
required
|
||||
disabled={!siteId}
|
||||
className="w-full border border-divider px-3 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-accent disabled:opacity-50"
|
||||
value={zoneId}
|
||||
onChange={e => setZoneId(e.target.value)}
|
||||
>
|
||||
<option value="">{t.zonePlaceholder}</option>
|
||||
{zonesForSite(sites, siteId).map(z => <option key={z.id} value={z.id}>{z.name}</option>)}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">
|
||||
{t.incidentTypeLabel} <span className="text-red-500">*</span>
|
||||
@@ -382,7 +425,7 @@ export function ReportForm({ zoneToken, trucks, initialTruckId }: Props) {
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={submitting || !form.incident_type || (form.incident_type === 'transport' && !form.truck_id)}
|
||||
disabled={submitting || !effectiveZoneToken || !form.incident_type || (form.incident_type === 'transport' && !form.truck_id)}
|
||||
className="w-full bg-accent text-white py-3 font-medium text-sm
|
||||
hover:bg-accent-600 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user