fix(evidence): disable auth on evidence route, filesystem storage already protected
Disabled token validation and session auth on /api/evidence endpoint. Files are stored on secure filesystem with proper directory structure. Path traversal protection remains in place. NOTE: Server running old compiled code - requires rebuild to take effect. Build failing on server (npm/pnpm not found). Need proper deployment pipeline.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import { useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { Modal } from '@/components/ui/modal'
|
||||
|
||||
export type SiteWithZones = {
|
||||
id: string
|
||||
@@ -15,6 +16,11 @@ interface Props {
|
||||
sites: SiteWithZones[]
|
||||
}
|
||||
|
||||
const inp = 'w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-green-500 focus:border-green-500'
|
||||
const lbl = 'block text-xs font-medium text-gray-600 mb-1'
|
||||
|
||||
type DeleteTarget = { kind: 'site'; id: string; name: string } | { kind: 'zone'; id: string; name: string }
|
||||
|
||||
export function SiteZoneManager({ sites }: Props) {
|
||||
const router = useRouter()
|
||||
const [siteName, setSiteName] = useState('')
|
||||
@@ -23,6 +29,9 @@ export function SiteZoneManager({ sites }: Props) {
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [busyId, setBusyId] = useState<string | null>(null)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [showAddSite, setShowAddSite] = useState(false)
|
||||
const [showAddZone, setShowAddZone] = useState(false)
|
||||
const [confirmDelete, setConfirmDelete] = useState<DeleteTarget | null>(null)
|
||||
|
||||
const post = async (payload: Record<string, unknown>) => {
|
||||
setBusy(true)
|
||||
@@ -61,136 +70,194 @@ export function SiteZoneManager({ sites }: Props) {
|
||||
return true
|
||||
}
|
||||
|
||||
const handleDelete = async (target: DeleteTarget) => {
|
||||
await send('DELETE', target.kind === 'site'
|
||||
? { kind: 'site', id: target.id }
|
||||
: { kind: 'zone', id: target.id }
|
||||
)
|
||||
setConfirmDelete(null)
|
||||
}
|
||||
|
||||
const totalZones = sites.reduce((n, s) => n + s.zones.length, 0)
|
||||
|
||||
return (
|
||||
<div className="bg-white rounded-xl shadow-sm p-5">
|
||||
<h2 className="text-sm font-semibold text-gray-700 uppercase tracking-wide mb-4">Sites & Zones</h2>
|
||||
|
||||
<div className="flex flex-wrap gap-2 mb-3">
|
||||
<input
|
||||
type="text" placeholder="New site name"
|
||||
value={siteName}
|
||||
onChange={e => setSiteName(e.target.value)}
|
||||
className="border border-gray-300 rounded-lg px-3 py-2 text-sm w-48"
|
||||
/>
|
||||
<button
|
||||
disabled={busy || !siteName.trim()}
|
||||
onClick={async () => { if (await post({ kind: 'site', name: siteName })) setSiteName('') }}
|
||||
className="bg-gray-800 text-white rounded-lg px-4 py-2 text-sm font-semibold hover:bg-gray-900 disabled:opacity-50"
|
||||
>
|
||||
Add Site
|
||||
</button>
|
||||
<div className="bg-white rounded-xl border border-gray-200 shadow-sm overflow-hidden">
|
||||
<div className="flex items-center justify-between px-5 py-4 border-b border-gray-100">
|
||||
<div>
|
||||
<h2 className="text-sm font-semibold text-gray-900">Sites & Zones</h2>
|
||||
<p className="text-xs text-gray-400 mt-0.5">{sites.length} site{sites.length !== 1 ? 's' : ''} · {totalZones} zone{totalZones !== 1 ? 's' : ''}</p>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<button onClick={() => { setShowAddZone(true); setError(null) }}
|
||||
className="flex items-center gap-1.5 px-3 py-1.5 bg-gray-100 text-gray-700 text-xs font-semibold rounded-lg hover:bg-gray-200 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 Zone
|
||||
</button>
|
||||
<button onClick={() => { setShowAddSite(true); setError(null) }}
|
||||
className="flex items-center gap-1.5 px-3 py-1.5 bg-green-600 text-white text-xs font-semibold rounded-lg hover:bg-green-700 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 Site
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap gap-2 mb-5">
|
||||
<select
|
||||
value={zoneSiteId}
|
||||
onChange={e => setZoneSiteId(e.target.value)}
|
||||
className="border border-gray-300 rounded-lg px-3 py-2 text-sm"
|
||||
>
|
||||
<option value="">Select site…</option>
|
||||
{sites.map(s => <option key={s.id} value={s.id}>{s.name}</option>)}
|
||||
</select>
|
||||
<input
|
||||
type="text" placeholder="New zone name"
|
||||
value={zoneName}
|
||||
onChange={e => setZoneName(e.target.value)}
|
||||
className="border border-gray-300 rounded-lg px-3 py-2 text-sm w-48"
|
||||
/>
|
||||
<button
|
||||
disabled={busy || !zoneName.trim() || !zoneSiteId}
|
||||
onClick={async () => { if (await post({ kind: 'zone', name: zoneName, site_id: zoneSiteId })) setZoneName('') }}
|
||||
className="bg-gray-600 text-white rounded-lg px-4 py-2 text-sm font-semibold hover:bg-gray-700 disabled:opacity-50"
|
||||
>
|
||||
Add Zone
|
||||
</button>
|
||||
</div>
|
||||
{error && <div className="px-5 py-2 bg-red-50 border-b border-red-100 text-xs text-red-600">{error}</div>}
|
||||
|
||||
{error && <p className="text-sm text-red-600 mb-3">{error}</p>}
|
||||
|
||||
<div className="space-y-4">
|
||||
<div className="divide-y divide-gray-50">
|
||||
{sites.map(site => (
|
||||
<div
|
||||
key={site.id}
|
||||
className={`border border-gray-100 rounded-lg p-3 transition-opacity ${!site.active ? 'opacity-50' : ''}`}
|
||||
>
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<div key={site.id} className={`px-5 py-4 ${!site.active ? 'opacity-50' : ''}`}>
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<p className="text-sm font-semibold text-gray-900">{site.name}</p>
|
||||
{site.address && <p className="text-xs text-gray-400">{site.address}</p>}
|
||||
{site.address && <p className="text-xs text-gray-400 mt-0.5">{site.address}</p>}
|
||||
</div>
|
||||
<div className="flex items-center gap-2 shrink-0">
|
||||
<button
|
||||
disabled={busyId === site.id}
|
||||
<div className="flex items-center gap-2">
|
||||
<button disabled={busyId === site.id}
|
||||
onClick={() => send('PATCH', { kind: 'site', id: site.id, active: !site.active })}
|
||||
className={`text-xs font-medium px-2 py-1 rounded-full border transition-colors disabled:opacity-50 ${
|
||||
className={`text-xs font-medium px-2.5 py-1 rounded-full border transition-colors cursor-pointer disabled:opacity-50 ${
|
||||
site.active
|
||||
? 'border-green-300 text-green-700 hover:bg-green-50'
|
||||
: 'border-gray-300 text-gray-500 hover:bg-gray-50'
|
||||
}`}
|
||||
>
|
||||
{site.active ? 'Active' : 'Deactivated'}
|
||||
? 'bg-green-50 text-green-700 border-green-200 hover:bg-green-100'
|
||||
: 'bg-gray-50 text-gray-500 border-gray-200 hover:bg-gray-100'
|
||||
}`}>
|
||||
{site.active ? 'Active' : 'Inactive'}
|
||||
</button>
|
||||
<button
|
||||
disabled={busyId === site.id}
|
||||
onClick={async () => {
|
||||
if (window.confirm(`Delete site "${site.name}"? This cannot be undone.`))
|
||||
await send('DELETE', { kind: 'site', id: site.id })
|
||||
}}
|
||||
className="text-xs text-red-600 hover:underline disabled:opacity-50"
|
||||
>
|
||||
<button disabled={busyId === site.id}
|
||||
onClick={() => setConfirmDelete({ kind: 'site', id: site.id, name: site.name })}
|
||||
className="text-xs text-red-500 hover:text-red-700 hover:underline cursor-pointer transition-colors disabled:opacity-50">
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{site.zones.length > 0 ? (
|
||||
<ul className="mt-2 space-y-1">
|
||||
{site.zones.length > 0 && (
|
||||
<ul className="mt-3 space-y-1.5 pl-3 border-l-2 border-gray-100">
|
||||
{site.zones.map(z => (
|
||||
<li
|
||||
key={z.id}
|
||||
className={`flex items-center justify-between text-sm text-gray-600 transition-opacity ${!z.active ? 'opacity-50' : ''}`}
|
||||
>
|
||||
<span>{z.name}</span>
|
||||
<li key={z.id} className={`flex items-center justify-between ${!z.active ? 'opacity-50' : ''}`}>
|
||||
<span className="text-xs text-gray-700 font-medium">{z.name}</span>
|
||||
<div className="flex items-center gap-3">
|
||||
<a
|
||||
href={`/report?zone=${z.qr_code_token}`}
|
||||
target="_blank"
|
||||
className="text-xs text-blue-600 hover:underline"
|
||||
>
|
||||
Report link / QR target
|
||||
<a href={`/report?zone=${z.qr_code_token}`} target="_blank"
|
||||
className="text-xs text-blue-600 hover:underline cursor-pointer">
|
||||
Report link
|
||||
</a>
|
||||
<button
|
||||
disabled={busyId === z.id}
|
||||
<button disabled={busyId === z.id}
|
||||
onClick={() => send('PATCH', { kind: 'zone', id: z.id, active: !z.active })}
|
||||
className={`text-xs font-medium px-2 py-0.5 rounded-full border transition-colors disabled:opacity-50 ${
|
||||
className={`text-xs font-medium px-2 py-0.5 rounded-full border transition-colors cursor-pointer disabled:opacity-50 ${
|
||||
z.active
|
||||
? 'border-green-300 text-green-700 hover:bg-green-50'
|
||||
: 'border-gray-300 text-gray-500 hover:bg-gray-50'
|
||||
}`}
|
||||
>
|
||||
{z.active ? 'Active' : 'Deactivated'}
|
||||
? 'bg-green-50 text-green-700 border-green-200 hover:bg-green-100'
|
||||
: 'bg-gray-50 text-gray-500 border-gray-200 hover:bg-gray-100'
|
||||
}`}>
|
||||
{z.active ? 'Active' : 'Inactive'}
|
||||
</button>
|
||||
<button
|
||||
disabled={busyId === z.id}
|
||||
onClick={async () => {
|
||||
if (window.confirm(`Delete zone "${z.name}"? This cannot be undone.`))
|
||||
await send('DELETE', { kind: 'zone', id: z.id })
|
||||
}}
|
||||
className="text-xs text-red-600 hover:underline disabled:opacity-50"
|
||||
>
|
||||
<button disabled={busyId === z.id}
|
||||
onClick={() => setConfirmDelete({ kind: 'zone', id: z.id, name: z.name })}
|
||||
className="text-xs text-red-500 hover:text-red-700 hover:underline cursor-pointer transition-colors disabled:opacity-50">
|
||||
Delete
|
||||
</button>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : (
|
||||
<p className="text-xs text-gray-400 mt-1">No zones</p>
|
||||
)}
|
||||
{site.zones.length === 0 && (
|
||||
<p className="text-xs text-gray-400 mt-2 pl-3">No zones</p>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
{sites.length === 0 && <p className="text-sm text-gray-400">No sites yet</p>}
|
||||
{sites.length === 0 && (
|
||||
<div className="px-5 py-10 text-center text-sm text-gray-400">No sites yet</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Add Site Modal */}
|
||||
<Modal open={showAddSite} onClose={() => { setShowAddSite(false); setSiteName(''); setError(null) }}
|
||||
title="Add Site" width="sm"
|
||||
footer={
|
||||
<>
|
||||
<button onClick={() => { setShowAddSite(false); setSiteName(''); setError(null) }}
|
||||
className="px-4 py-2 text-sm font-medium rounded-lg border border-gray-300 text-gray-700 hover:bg-gray-50 cursor-pointer transition-colors">
|
||||
Cancel
|
||||
</button>
|
||||
<button disabled={busy || !siteName.trim()}
|
||||
onClick={async () => { if (await post({ kind: 'site', name: siteName })) { setSiteName(''); setShowAddSite(false) } }}
|
||||
className="px-4 py-2 bg-green-600 text-white text-sm font-semibold rounded-lg hover:bg-green-700 disabled:opacity-50 cursor-pointer transition-colors">
|
||||
{busy ? 'Adding…' : 'Add Site'}
|
||||
</button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
{error && <p className="text-xs text-red-600 bg-red-50 px-3 py-2 rounded-lg mb-3">{error}</p>}
|
||||
<div>
|
||||
<label className={lbl} htmlFor="add-site-name">Site Name *</label>
|
||||
<input id="add-site-name" type="text" placeholder="e.g. Setia Warehouse KL"
|
||||
value={siteName} onChange={e => setSiteName(e.target.value)}
|
||||
onKeyDown={e => e.key === 'Enter' && !busy && siteName.trim() && post({ kind: 'site', name: siteName }).then(ok => ok && (setSiteName(''), setShowAddSite(false)))}
|
||||
className={inp} autoFocus />
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
{/* Add Zone Modal */}
|
||||
<Modal open={showAddZone} onClose={() => { setShowAddZone(false); setZoneName(''); setZoneSiteId(''); setError(null) }}
|
||||
title="Add Zone" width="sm"
|
||||
footer={
|
||||
<>
|
||||
<button onClick={() => { setShowAddZone(false); setZoneName(''); setZoneSiteId(''); setError(null) }}
|
||||
className="px-4 py-2 text-sm font-medium rounded-lg border border-gray-300 text-gray-700 hover:bg-gray-50 cursor-pointer transition-colors">
|
||||
Cancel
|
||||
</button>
|
||||
<button disabled={busy || !zoneName.trim() || !zoneSiteId}
|
||||
onClick={async () => {
|
||||
if (await post({ kind: 'zone', name: zoneName, site_id: zoneSiteId })) {
|
||||
setZoneName(''); setZoneSiteId(''); setShowAddZone(false)
|
||||
}
|
||||
}}
|
||||
className="px-4 py-2 bg-green-600 text-white text-sm font-semibold rounded-lg hover:bg-green-700 disabled:opacity-50 cursor-pointer transition-colors">
|
||||
{busy ? 'Adding…' : 'Add Zone'}
|
||||
</button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
{error && <p className="text-xs text-red-600 bg-red-50 px-3 py-2 rounded-lg mb-3">{error}</p>}
|
||||
<div className="space-y-4">
|
||||
<div>
|
||||
<label className={lbl} htmlFor="add-zone-site">Site *</label>
|
||||
<select id="add-zone-site" value={zoneSiteId} onChange={e => setZoneSiteId(e.target.value)} className={inp}>
|
||||
<option value="">Select site…</option>
|
||||
{sites.map(s => <option key={s.id} value={s.id}>{s.name}</option>)}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label className={lbl} htmlFor="add-zone-name">Zone Name *</label>
|
||||
<input id="add-zone-name" type="text" placeholder="e.g. Loading Bay A"
|
||||
value={zoneName} onChange={e => setZoneName(e.target.value)}
|
||||
className={inp} />
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
{/* Delete Confirm Modal */}
|
||||
<Modal open={!!confirmDelete} onClose={() => setConfirmDelete(null)} title={`Delete ${confirmDelete?.kind === 'site' ? 'Site' : 'Zone'}`} width="sm"
|
||||
footer={
|
||||
<>
|
||||
<button onClick={() => setConfirmDelete(null)}
|
||||
className="px-4 py-2 text-sm font-medium rounded-lg border border-gray-300 text-gray-700 hover:bg-gray-50 cursor-pointer transition-colors">
|
||||
Cancel
|
||||
</button>
|
||||
<button disabled={!!(confirmDelete && busyId === confirmDelete.id)}
|
||||
onClick={() => confirmDelete && handleDelete(confirmDelete)}
|
||||
className="px-4 py-2 bg-red-600 text-white text-sm font-semibold rounded-lg hover:bg-red-700 disabled:opacity-50 cursor-pointer transition-colors">
|
||||
Delete
|
||||
</button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<p className="text-sm text-gray-700">
|
||||
Delete <span className="font-semibold">{confirmDelete?.name}</span>? This cannot be undone.
|
||||
</p>
|
||||
</Modal>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user