105 lines
3.0 KiB
TypeScript
105 lines
3.0 KiB
TypeScript
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, 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'
|
|
|
|
interface Props {
|
|
searchParams: Promise<{ zone?: string; truck_id?: string }>
|
|
}
|
|
|
|
export default async function ReportPage({ searchParams }: Props) {
|
|
const { zone, truck_id } = await searchParams
|
|
const session = await getSession()
|
|
if (!session) redirect(`/login?redirect=/report${zone ? `?zone=${zone}` : ''}`)
|
|
|
|
let zoneName: string | null = null
|
|
let siteName: string | null = null
|
|
|
|
if (zone) {
|
|
const [zd] = await asAdmin(db =>
|
|
db.select({
|
|
name: zones.name,
|
|
siteName: sites.name,
|
|
})
|
|
.from(zones)
|
|
.leftJoin(sites, eq(zones.siteId, sites.id))
|
|
.where(eq(zones.qrCodeToken, zone))
|
|
.limit(1)
|
|
)
|
|
if (zd) {
|
|
zoneName = zd.name
|
|
siteName = zd.siteName ?? null
|
|
}
|
|
}
|
|
|
|
const truckRows = await asAdmin(db =>
|
|
db.select({
|
|
id: trucks.id,
|
|
truckNo: trucks.truckNo,
|
|
carrier: trucks.carrier,
|
|
})
|
|
.from(trucks)
|
|
.where(eq(trucks.active, true))
|
|
.orderBy(asc(trucks.truckNo))
|
|
)
|
|
|
|
const truckList = truckRows.map(t => ({
|
|
id: t.id,
|
|
truck_no: t.truckNo,
|
|
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">
|
|
<div className="flex items-center justify-between">
|
|
<h1 className="text-2xl font-bold text-gray-900">{/* will be translated via ReportForm */}Report an Incident</h1>
|
|
<LanguageSwitcher />
|
|
</div>
|
|
{zoneName && (
|
|
<p className="text-sm text-gray-600 mt-1">
|
|
{siteName ?? 'Unknown Site'} — {zoneName}
|
|
</p>
|
|
)}
|
|
</div>
|
|
<ReportForm
|
|
zoneToken={zoneName ? (zone ?? null) : null}
|
|
zoneName={zoneName}
|
|
siteName={siteName}
|
|
sites={siteOptions}
|
|
defaultSiteId={defaultSiteId}
|
|
trucks={truckList}
|
|
initialTruckId={truck_id ?? null}
|
|
/>
|
|
<OfflineSync />
|
|
</main>
|
|
)
|
|
}
|