'use client'; import { useEffect, useState } from 'react'; import { inventoryPosService, Warehouse } from '@/services/api/inventoryPosService'; import { Plus, MapPin, Loader2, X } from 'lucide-react'; import { toast } from 'sonner'; import { format } from 'date-fns'; export default function WarehousesPage() { const [warehouses, setWarehouses] = useState([]); const [loading, setLoading] = useState(true); const [isModalOpen, setIsModalOpen] = useState(false); const [saving, setSaving] = useState(false); const [name, setName] = useState(''); const [code, setCode] = useState(''); const [location, setLocation] = useState(''); useEffect(() => { loadWarehouses(); }, []); const loadWarehouses = async () => { try { setLoading(true); const data = await inventoryPosService.getWarehouses(); setWarehouses(Array.isArray(data) ? data : []); } catch (err: unknown) { setWarehouses([]); const message = err instanceof Error ? err.message : 'Failed to load warehouses'; toast.error(message); } finally { setLoading(false); } }; const handleCreate = async (e: React.FormEvent) => { e.preventDefault(); try { setSaving(true); await inventoryPosService.createWarehouse({ name, code, location_address: location }); setIsModalOpen(false); setName(''); setCode(''); setLocation(''); toast.success('Warehouse created'); loadWarehouses(); } catch (err: unknown) { const message = err instanceof Error ? err.message : 'Failed to create warehouse'; toast.error(message); } finally { setSaving(false); } }; const dataCardShell = 'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] min-w-0'; const fieldClass = 'w-full h-9 px-3 crm-radius-control border border-border bg-card text-[13px] text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-primary'; const labelClass = 'block text-[11px] font-semibold text-muted-foreground uppercase mb-1.5'; const primaryButton = 'inline-flex items-center justify-center gap-2 h-9 px-4 crm-radius-control bg-primary hover:bg-primary/90 text-white text-[13px] font-semibold cursor-pointer disabled:opacity-50'; const secondaryButton = 'inline-flex items-center justify-center gap-2 h-9 px-4 crm-radius-control border border-border bg-card text-foreground text-[13px] font-medium hover:bg-muted cursor-pointer disabled:opacity-50'; return (

Warehouses

{warehouses.length}
{loading ? (
) : (
{warehouses.length === 0 ? ( ) : warehouses.map((warehouse) => ( ))}
Warehouse Code Address Status Created
No warehouses yet
{warehouse.name} {warehouse.code} {warehouse.location_address || '—'} {warehouse.is_active ? 'Active' : 'Inactive'} {(() => { const created = warehouse.created_at ? new Date(warehouse.created_at) : null; return created && !Number.isNaN(created.getTime()) ? format(created, 'dd-MMM-yyyy') : '—'; })()}
)}
{isModalOpen && (

New warehouse

setName(e.target.value)} className={fieldClass} />
setCode(e.target.value)} className={fieldClass} />
setLocation(e.target.value)} className={fieldClass} />
)}
); }