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:
+27
-6
@@ -3,8 +3,9 @@ export const dynamic = 'force-dynamic'
|
||||
import { redirect } from 'next/navigation'
|
||||
import { asAdmin } from '@/lib/db/with-user'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { zones, sites, trucks } from '@/lib/db/schema'
|
||||
import { eq, asc } from 'drizzle-orm'
|
||||
import { zones, sites, trucks, users } from '@/lib/db/schema'
|
||||
import { eq, asc, and } from 'drizzle-orm'
|
||||
import { groupSites } from '@/lib/report/zones'
|
||||
import { ReportForm } from '@/components/incidents/report-form'
|
||||
import { LanguageSwitcher } from '@/components/language-switcher'
|
||||
import { OfflineSync } from '@/components/incidents/offline-sync'
|
||||
@@ -55,6 +56,26 @@ export default async function ReportPage({ searchParams }: Props) {
|
||||
carrier: t.carrier ?? null,
|
||||
}))
|
||||
|
||||
const zoneRows = await asAdmin(db =>
|
||||
db.select({
|
||||
siteId: sites.id,
|
||||
siteName: sites.name,
|
||||
zoneId: zones.id,
|
||||
zoneName: zones.name,
|
||||
token: zones.qrCodeToken,
|
||||
})
|
||||
.from(zones)
|
||||
.innerJoin(sites, eq(zones.siteId, sites.id))
|
||||
.where(and(eq(zones.active, true), eq(sites.active, true)))
|
||||
.orderBy(asc(sites.name), asc(zones.name))
|
||||
)
|
||||
const siteOptions = groupSites(zoneRows)
|
||||
|
||||
const [me] = await asAdmin(db =>
|
||||
db.select({ siteId: users.siteId }).from(users).where(eq(users.id, session.sub)).limit(1)
|
||||
)
|
||||
const defaultSiteId = me?.siteId ?? ''
|
||||
|
||||
return (
|
||||
<main className="min-h-screen bg-gray-50 py-6 px-4 max-w-lg mx-auto">
|
||||
<div className="mb-6">
|
||||
@@ -62,18 +83,18 @@ export default async function ReportPage({ searchParams }: Props) {
|
||||
<h1 className="text-2xl font-bold text-gray-900">{/* will be translated via ReportForm */}Report an Incident</h1>
|
||||
<LanguageSwitcher />
|
||||
</div>
|
||||
{zoneName ? (
|
||||
{zoneName && (
|
||||
<p className="text-sm text-gray-600 mt-1">
|
||||
{siteName ?? 'Unknown Site'} — {zoneName}
|
||||
</p>
|
||||
) : (
|
||||
<p className="text-sm text-accent-700 mt-1">No zone detected — zone will not be recorded</p>
|
||||
)}
|
||||
</div>
|
||||
<ReportForm
|
||||
zoneToken={zone ?? null}
|
||||
zoneToken={zoneName ? (zone ?? null) : null}
|
||||
zoneName={zoneName}
|
||||
siteName={siteName}
|
||||
sites={siteOptions}
|
||||
defaultSiteId={defaultSiteId}
|
||||
trucks={truckList}
|
||||
initialTruckId={truck_id ?? null}
|
||||
/>
|
||||
|
||||
@@ -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"
|
||||
>
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
export type ZoneOption = { id: string; name: string; qr_code_token: string }
|
||||
export type SiteOption = { id: string; name: string; zones: ZoneOption[] }
|
||||
export type ZoneRow = { siteId: string; siteName: string; zoneId: string; zoneName: string; token: string }
|
||||
|
||||
export function groupSites(rows: ZoneRow[]): SiteOption[] {
|
||||
const bySite = new Map<string, SiteOption>()
|
||||
for (const r of rows) {
|
||||
let site = bySite.get(r.siteId)
|
||||
if (!site) {
|
||||
site = { id: r.siteId, name: r.siteName, zones: [] }
|
||||
bySite.set(r.siteId, site)
|
||||
}
|
||||
site.zones.push({ id: r.zoneId, name: r.zoneName, qr_code_token: r.token })
|
||||
}
|
||||
return [...bySite.values()]
|
||||
}
|
||||
|
||||
export function zonesForSite(sites: SiteOption[], siteId: string): ZoneOption[] {
|
||||
return sites.find(s => s.id === siteId)?.zones ?? []
|
||||
}
|
||||
|
||||
export function resolveZoneToken(args: {
|
||||
zoneToken: string | null
|
||||
sites: SiteOption[]
|
||||
zoneId: string
|
||||
}): string | null {
|
||||
if (args.zoneToken) return args.zoneToken
|
||||
for (const site of args.sites) {
|
||||
const zone = site.zones.find(z => z.id === args.zoneId)
|
||||
if (zone) return zone.qr_code_token
|
||||
}
|
||||
return null
|
||||
}
|
||||
+5
-1
@@ -18,7 +18,11 @@
|
||||
"savedOffline": "Report saved. It will be submitted automatically when you're back online.",
|
||||
"offlineBanner": "You're offline. Your report will be saved and submitted when you reconnect.",
|
||||
"truckLabel": "Truck",
|
||||
"truckPlaceholder": "Select truck…"
|
||||
"truckPlaceholder": "Select truck…",
|
||||
"siteLabel": "Site",
|
||||
"sitePlaceholder": "Select site…",
|
||||
"zoneLabel": "Zone",
|
||||
"zonePlaceholder": "Select zone…"
|
||||
},
|
||||
"IncidentType": {
|
||||
"injury": "Injury / Medical",
|
||||
|
||||
+5
-1
@@ -18,7 +18,11 @@
|
||||
"savedOffline": "Laporan disimpan. Ia akan dihantar secara automatik apabila anda dalam talian semula.",
|
||||
"offlineBanner": "Anda tiada sambungan. Laporan anda akan disimpan dan dihantar apabila disambungkan semula.",
|
||||
"truckLabel": "Trak",
|
||||
"truckPlaceholder": "Pilih trak…"
|
||||
"truckPlaceholder": "Pilih trak…",
|
||||
"siteLabel": "Tapak",
|
||||
"sitePlaceholder": "Pilih tapak…",
|
||||
"zoneLabel": "Zon",
|
||||
"zonePlaceholder": "Pilih zon…"
|
||||
},
|
||||
"IncidentType": {
|
||||
"injury": "Kecederaan / Perubatan",
|
||||
|
||||
+5
-1
@@ -18,7 +18,11 @@
|
||||
"savedOffline": "报告已保存。当您重新联网时将自动提交。",
|
||||
"offlineBanner": "您处于离线状态。您的报告将在重新联网时自动提交。",
|
||||
"truckLabel": "货车",
|
||||
"truckPlaceholder": "选择货车…"
|
||||
"truckPlaceholder": "选择货车…",
|
||||
"siteLabel": "场地",
|
||||
"sitePlaceholder": "选择场地…",
|
||||
"zoneLabel": "区域",
|
||||
"zonePlaceholder": "选择区域…"
|
||||
},
|
||||
"IncidentType": {
|
||||
"injury": "受伤/医疗",
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { groupSites, zonesForSite, resolveZoneToken, type ZoneRow } from '@/lib/report/zones'
|
||||
|
||||
const rows: ZoneRow[] = [
|
||||
{ siteId: 's1', siteName: 'SC1', zoneId: 'z1', zoneName: 'Dock A', token: 'tok-a' },
|
||||
{ siteId: 's1', siteName: 'SC1', zoneId: 'z2', zoneName: 'Dock B', token: 'tok-b' },
|
||||
{ siteId: 's2', siteName: 'SC2', zoneId: 'z3', zoneName: 'Cold Room', token: 'tok-c' },
|
||||
]
|
||||
|
||||
describe('groupSites', () => {
|
||||
it('groups zone rows under their site, keeping order', () => {
|
||||
const sites = groupSites(rows)
|
||||
expect(sites.map(s => s.name)).toEqual(['SC1', 'SC2'])
|
||||
expect(sites[0].zones.map(z => z.name)).toEqual(['Dock A', 'Dock B'])
|
||||
expect(sites[1].zones[0]).toEqual({ id: 'z3', name: 'Cold Room', qr_code_token: 'tok-c' })
|
||||
})
|
||||
|
||||
it('returns [] for no rows', () => {
|
||||
expect(groupSites([])).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('zonesForSite', () => {
|
||||
const sites = groupSites(rows)
|
||||
it('returns the zones of the given site', () => {
|
||||
expect(zonesForSite(sites, 's2').map(z => z.id)).toEqual(['z3'])
|
||||
})
|
||||
it('returns [] for an unknown or empty site id', () => {
|
||||
expect(zonesForSite(sites, 'nope')).toEqual([])
|
||||
expect(zonesForSite(sites, '')).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('resolveZoneToken', () => {
|
||||
const sites = groupSites(rows)
|
||||
it('prefers the QR token', () => {
|
||||
expect(resolveZoneToken({ zoneToken: 'qr-1', sites, zoneId: 'z1' })).toBe('qr-1')
|
||||
})
|
||||
it('falls back to the selected zone token', () => {
|
||||
expect(resolveZoneToken({ zoneToken: null, sites, zoneId: 'z2' })).toBe('tok-b')
|
||||
})
|
||||
it('returns null when nothing is chosen or the zone is unknown', () => {
|
||||
expect(resolveZoneToken({ zoneToken: null, sites, zoneId: '' })).toBeNull()
|
||||
expect(resolveZoneToken({ zoneToken: null, sites, zoneId: 'zz' })).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user