feat(trucks): site select, site label and edit modal in admin
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
import { useState } from 'react'
|
||||
import { UserManager, type AdminUser, type SiteOption } from './user-manager'
|
||||
import { SiteZoneManager, type SiteWithZones } from './site-zone-manager'
|
||||
import { TruckManager, type Truck } from './truck-manager'
|
||||
import { TruckManager, type Truck, type TruckSite } from './truck-manager'
|
||||
|
||||
type Tab = 'users' | 'sites' | 'trucks'
|
||||
|
||||
@@ -12,9 +12,10 @@ interface Props {
|
||||
sites: SiteWithZones[]
|
||||
siteOptions: SiteOption[]
|
||||
trucks: Truck[]
|
||||
truckSites: TruckSite[]
|
||||
}
|
||||
|
||||
export function AdminPanel({ users, sites, siteOptions, trucks }: Props) {
|
||||
export function AdminPanel({ users, sites, siteOptions, trucks, truckSites }: Props) {
|
||||
const [tab, setTab] = useState<Tab>('users')
|
||||
|
||||
const tabs: { id: Tab; label: string; count: number }[] = [
|
||||
@@ -45,7 +46,7 @@ export function AdminPanel({ users, sites, siteOptions, trucks }: Props) {
|
||||
</div>
|
||||
{tab === 'users' && <UserManager users={users} sites={siteOptions} />}
|
||||
{tab === 'sites' && <SiteZoneManager sites={sites} />}
|
||||
{tab === 'trucks' && <TruckManager trucks={trucks} />}
|
||||
{tab === 'trucks' && <TruckManager trucks={trucks} sites={truckSites} />}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -9,16 +9,20 @@ export type Truck = {
|
||||
id: string
|
||||
truck_no: string
|
||||
carrier: string | null
|
||||
site_id: string | null
|
||||
active: boolean
|
||||
}
|
||||
|
||||
export type TruckSite = { id: string; name: string; active: boolean }
|
||||
|
||||
interface Props {
|
||||
trucks: Truck[]
|
||||
sites: TruckSite[]
|
||||
}
|
||||
|
||||
const lbl = 'block text-xs font-medium text-gray-600 mb-1'
|
||||
|
||||
export function TruckManager({ trucks }: Props) {
|
||||
export function TruckManager({ trucks, sites }: Props) {
|
||||
const router = useRouter()
|
||||
const [truckNo, setTruckNo] = useState('')
|
||||
const [carrier, setCarrier] = useState('')
|
||||
@@ -27,6 +31,22 @@ export function TruckManager({ trucks }: Props) {
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [showAdd, setShowAdd] = useState(false)
|
||||
const [confirmDelete, setConfirmDelete] = useState<Truck | null>(null)
|
||||
const [siteId, setSiteId] = useState('')
|
||||
const [editing, setEditing] = useState<Truck | null>(null)
|
||||
const [editNo, setEditNo] = useState('')
|
||||
const [editCarrier, setEditCarrier] = useState('')
|
||||
const [editSiteId, setEditSiteId] = useState('')
|
||||
|
||||
const siteName = (id: string | null) => sites.find(s => s.id === id)?.name ?? null
|
||||
const siteChoices = (currentId: string) => sites.filter(s => s.active || s.id === currentId)
|
||||
|
||||
const openEdit = (tk: Truck) => {
|
||||
setEditing(tk)
|
||||
setEditNo(tk.truck_no)
|
||||
setEditCarrier(tk.carrier ?? '')
|
||||
setEditSiteId(tk.site_id ?? '')
|
||||
setError(null)
|
||||
}
|
||||
|
||||
const handleAdd = async () => {
|
||||
const no = truckNo.trim()
|
||||
@@ -37,12 +57,13 @@ export function TruckManager({ trucks }: Props) {
|
||||
const res = await fetch('/api/admin/trucks', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ truck_no: no, carrier: carrier.trim() || null }),
|
||||
body: JSON.stringify({ truck_no: no, carrier: carrier.trim() || null, site_id: siteId || null }),
|
||||
})
|
||||
const data = await res.json()
|
||||
if (!res.ok) { setError(data.error ?? 'Failed to add truck'); return }
|
||||
setTruckNo('')
|
||||
setCarrier('')
|
||||
setSiteId('')
|
||||
setShowAdd(false)
|
||||
router.refresh()
|
||||
} catch {
|
||||
@@ -82,6 +103,19 @@ export function TruckManager({ trucks }: Props) {
|
||||
setConfirmDelete(null)
|
||||
}
|
||||
|
||||
const handleSaveEdit = async () => {
|
||||
if (!editing) return
|
||||
const no = editNo.trim()
|
||||
if (!no) return
|
||||
const ok = await send('PATCH', {
|
||||
id: editing.id,
|
||||
truck_no: no,
|
||||
carrier: editCarrier.trim() || null,
|
||||
site_id: editSiteId || null,
|
||||
})
|
||||
if (ok) setEditing(null)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="bg-surface border border-divider shadow-sm overflow-hidden">
|
||||
<div className="flex items-center justify-between px-5 py-4 border-b border-divider">
|
||||
@@ -106,8 +140,15 @@ export function TruckManager({ trucks }: Props) {
|
||||
<div className="flex items-center gap-3">
|
||||
<span className="text-sm font-semibold text-gray-900">{tk.truck_no}</span>
|
||||
{tk.carrier && <span className="text-xs text-gray-400">{tk.carrier}</span>}
|
||||
<span className={`text-xs px-2 py-0.5 ${tk.site_id ? 'bg-neutral-100 text-ink' : 'bg-gray-100 text-gray-500'}`}>
|
||||
{siteName(tk.site_id) ?? 'Unassigned'}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-3">
|
||||
<button disabled={busyId === tk.id} onClick={() => openEdit(tk)}
|
||||
className="text-xs text-accent-700 hover:underline cursor-pointer disabled:opacity-50">
|
||||
Edit
|
||||
</button>
|
||||
<a href={`/report?truck_id=${tk.id}`} target="_blank"
|
||||
className="text-xs text-accent-700 hover:underline cursor-pointer">
|
||||
Report link
|
||||
@@ -134,11 +175,11 @@ export function TruckManager({ trucks }: Props) {
|
||||
</div>
|
||||
|
||||
{/* Add Truck Modal */}
|
||||
<Modal open={showAdd} onClose={() => { setShowAdd(false); setTruckNo(''); setCarrier(''); setError(null) }}
|
||||
<Modal open={showAdd} onClose={() => { setShowAdd(false); setTruckNo(''); setCarrier(''); setSiteId(''); setError(null) }}
|
||||
title="Add Truck" width="sm"
|
||||
footer={
|
||||
<>
|
||||
<button onClick={() => { setShowAdd(false); setTruckNo(''); setCarrier(''); setError(null) }}
|
||||
<button onClick={() => { setShowAdd(false); setTruckNo(''); setCarrier(''); setSiteId(''); setError(null) }}
|
||||
className="px-4 py-2 text-sm font-medium border border-divider text-gray-700 hover:bg-gray-50 cursor-pointer transition-colors">
|
||||
Cancel
|
||||
</button>
|
||||
@@ -165,6 +206,49 @@ export function TruckManager({ trucks }: Props) {
|
||||
onKeyDown={e => e.key === 'Enter' && handleAdd()}
|
||||
className={inp} />
|
||||
</div>
|
||||
<div>
|
||||
<label className={lbl} htmlFor="add-truck-site">Site</label>
|
||||
<select id="add-truck-site" value={siteId} onChange={e => setSiteId(e.target.value)} className={inp}>
|
||||
<option value="">No site</option>
|
||||
{siteChoices('').map(s => <option key={s.id} value={s.id}>{s.name}</option>)}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
{/* Edit Truck Modal */}
|
||||
<Modal open={!!editing} onClose={() => { setEditing(null); setError(null) }}
|
||||
title="Edit Truck" width="sm"
|
||||
footer={
|
||||
<>
|
||||
<button onClick={() => { setEditing(null); setError(null) }}
|
||||
className="px-4 py-2 text-sm font-medium border border-divider text-gray-700 hover:bg-gray-50 cursor-pointer transition-colors">
|
||||
Cancel
|
||||
</button>
|
||||
<button disabled={busyId === editing?.id || !editNo.trim()} onClick={handleSaveEdit}
|
||||
className="px-4 py-2 bg-accent text-white text-sm font-semibold hover:bg-accent-600 disabled:opacity-50 cursor-pointer transition-colors">
|
||||
{busyId === editing?.id ? 'Saving…' : 'Save'}
|
||||
</button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
{error && <p className="text-xs text-red-600 bg-red-50 px-3 py-2 mb-3">{error}</p>}
|
||||
<div className="space-y-4">
|
||||
<div>
|
||||
<label className={lbl} htmlFor="edit-truck-no">Truck Number *</label>
|
||||
<input id="edit-truck-no" type="text" value={editNo} onChange={e => setEditNo(e.target.value)} className={inp} />
|
||||
</div>
|
||||
<div>
|
||||
<label className={lbl} htmlFor="edit-truck-carrier">Carrier (optional)</label>
|
||||
<input id="edit-truck-carrier" type="text" value={editCarrier} onChange={e => setEditCarrier(e.target.value)} className={inp} />
|
||||
</div>
|
||||
<div>
|
||||
<label className={lbl} htmlFor="edit-truck-site">Site</label>
|
||||
<select id="edit-truck-site" value={editSiteId} onChange={e => setEditSiteId(e.target.value)} className={inp}>
|
||||
<option value="">No site</option>
|
||||
{siteChoices(editing?.site_id ?? '').map(s => <option key={s.id} value={s.id}>{s.name}</option>)}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user