194 lines
7.7 KiB
TypeScript
194 lines
7.7 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import { useRouter } from 'next/navigation'
|
|
import { Modal } from '@/components/ui/modal'
|
|
import { INPUT_CLASS as inp } from '@/components/ui/field'
|
|
|
|
export type Truck = {
|
|
id: string
|
|
truck_no: string
|
|
carrier: string | null
|
|
active: boolean
|
|
}
|
|
|
|
interface Props {
|
|
trucks: Truck[]
|
|
}
|
|
|
|
const lbl = 'block text-xs font-medium text-gray-600 mb-1'
|
|
|
|
export function TruckManager({ trucks }: Props) {
|
|
const router = useRouter()
|
|
const [truckNo, setTruckNo] = useState('')
|
|
const [carrier, setCarrier] = useState('')
|
|
const [submitting, setSubmitting] = useState(false)
|
|
const [busyId, setBusyId] = useState<string | null>(null)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [showAdd, setShowAdd] = useState(false)
|
|
const [confirmDelete, setConfirmDelete] = useState<Truck | null>(null)
|
|
|
|
const handleAdd = async () => {
|
|
const no = truckNo.trim()
|
|
if (!no) return
|
|
setSubmitting(true)
|
|
setError(null)
|
|
try {
|
|
const res = await fetch('/api/admin/trucks', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ truck_no: no, carrier: carrier.trim() || null }),
|
|
})
|
|
const data = await res.json()
|
|
if (!res.ok) { setError(data.error ?? 'Failed to add truck'); return }
|
|
setTruckNo('')
|
|
setCarrier('')
|
|
setShowAdd(false)
|
|
router.refresh()
|
|
} catch {
|
|
setError('Network error')
|
|
} finally {
|
|
setSubmitting(false)
|
|
}
|
|
}
|
|
|
|
const send = async (method: 'PATCH' | 'DELETE', payload: Record<string, unknown>) => {
|
|
const id = payload.id as string
|
|
setBusyId(id)
|
|
setError(null)
|
|
try {
|
|
const res = await fetch('/api/admin/trucks', {
|
|
method,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(payload),
|
|
})
|
|
if (!res.ok) {
|
|
const data = await res.json().catch(() => ({}))
|
|
setError(data.error ?? 'Action failed')
|
|
return false
|
|
}
|
|
router.refresh()
|
|
return true
|
|
} catch {
|
|
setError('Network error')
|
|
return false
|
|
} finally {
|
|
setBusyId(null)
|
|
}
|
|
}
|
|
|
|
const handleDelete = async (tk: Truck) => {
|
|
await send('DELETE', { id: tk.id })
|
|
setConfirmDelete(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">
|
|
<div>
|
|
<h2 className="text-sm font-semibold text-gray-900">Trucks</h2>
|
|
<p className="text-xs text-gray-400 mt-0.5">{trucks.length} vehicle{trucks.length !== 1 ? 's' : ''}</p>
|
|
</div>
|
|
<button onClick={() => { setShowAdd(true); setError(null) }}
|
|
className="flex items-center gap-1.5 px-3 py-1.5 bg-accent text-white text-xs font-semibold hover:bg-accent-600 transition-colors cursor-pointer">
|
|
<svg className="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2.5}>
|
|
<path strokeLinecap="round" strokeLinejoin="round" d="M12 4v16m8-8H4" />
|
|
</svg>
|
|
Add Truck
|
|
</button>
|
|
</div>
|
|
|
|
{error && <div className="px-5 py-2 bg-red-50 border-b border-red-100 text-xs text-red-600">{error}</div>}
|
|
|
|
<div className="divide-y divide-gray-50">
|
|
{trucks.map(tk => (
|
|
<div key={tk.id} className={`flex items-center justify-between px-5 py-3 hover:bg-gray-50/50 transition-colors ${!tk.active ? 'opacity-50' : ''}`}>
|
|
<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>}
|
|
</div>
|
|
<div className="flex items-center gap-3">
|
|
<a href={`/report?truck_id=${tk.id}`} target="_blank"
|
|
className="text-xs text-accent-700 hover:underline cursor-pointer">
|
|
Report link
|
|
</a>
|
|
<button disabled={busyId === tk.id}
|
|
onClick={() => send('PATCH', { id: tk.id, active: !tk.active })}
|
|
className={`text-xs font-medium px-2.5 py-1 border transition-colors cursor-pointer disabled:opacity-50 ${
|
|
tk.active
|
|
? 'bg-neutral-100 text-ink border-divider hover:bg-neutral-200'
|
|
: 'bg-gray-50 text-gray-500 border-divider hover:bg-gray-100'
|
|
}`}>
|
|
{tk.active ? 'Active' : 'Inactive'}
|
|
</button>
|
|
<button disabled={busyId === tk.id} onClick={() => setConfirmDelete(tk)}
|
|
className="text-xs text-red-500 hover:text-red-700 hover:underline cursor-pointer transition-colors disabled:opacity-50">
|
|
Delete
|
|
</button>
|
|
</div>
|
|
</div>
|
|
))}
|
|
{trucks.length === 0 && (
|
|
<div className="px-5 py-10 text-center text-sm text-gray-400">No trucks yet</div>
|
|
)}
|
|
</div>
|
|
|
|
{/* Add Truck Modal */}
|
|
<Modal open={showAdd} onClose={() => { setShowAdd(false); setTruckNo(''); setCarrier(''); setError(null) }}
|
|
title="Add Truck" width="sm"
|
|
footer={
|
|
<>
|
|
<button onClick={() => { setShowAdd(false); setTruckNo(''); setCarrier(''); 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={submitting || !truckNo.trim()} onClick={handleAdd}
|
|
className="px-4 py-2 bg-accent text-white text-sm font-semibold hover:bg-accent-600 disabled:opacity-50 cursor-pointer transition-colors">
|
|
{submitting ? 'Adding…' : 'Add Truck'}
|
|
</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="add-truck-no">Truck Number *</label>
|
|
<input id="add-truck-no" type="text" placeholder="e.g. WXY 1234"
|
|
value={truckNo} onChange={e => setTruckNo(e.target.value)}
|
|
onKeyDown={e => e.key === 'Enter' && handleAdd()}
|
|
className={inp} autoFocus />
|
|
</div>
|
|
<div>
|
|
<label className={lbl} htmlFor="add-truck-carrier">Carrier (optional)</label>
|
|
<input id="add-truck-carrier" type="text" placeholder="e.g. Setia Logistics"
|
|
value={carrier} onChange={e => setCarrier(e.target.value)}
|
|
onKeyDown={e => e.key === 'Enter' && handleAdd()}
|
|
className={inp} />
|
|
</div>
|
|
</div>
|
|
</Modal>
|
|
|
|
{/* Delete Confirm Modal */}
|
|
<Modal open={!!confirmDelete} onClose={() => setConfirmDelete(null)} title="Delete Truck" width="sm"
|
|
footer={
|
|
<>
|
|
<button onClick={() => setConfirmDelete(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 === confirmDelete?.id}
|
|
onClick={() => confirmDelete && handleDelete(confirmDelete)}
|
|
className="px-4 py-2 bg-red-600 text-white text-sm font-semibold hover:bg-red-700 disabled:opacity-50 cursor-pointer transition-colors">
|
|
{busyId === confirmDelete?.id ? 'Deleting…' : 'Delete'}
|
|
</button>
|
|
</>
|
|
}
|
|
>
|
|
<p className="text-sm text-gray-700">
|
|
Delete truck <span className="font-semibold">{confirmDelete?.truck_no}</span>? This cannot be undone.
|
|
</p>
|
|
</Modal>
|
|
</div>
|
|
)
|
|
}
|