ifixkart-admin/app/(admin)/warehouses/page.tsx

195 lines
8.4 KiB
TypeScript

'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<Warehouse[]>([]);
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 (
<div className="flex flex-col gap-5 min-w-0">
<div className="flex flex-wrap items-center justify-between gap-3 min-w-0">
<div className="flex items-center gap-2 min-w-0">
<h1 className="text-lg font-semibold text-foreground">Warehouses</h1>
<span className="inline-flex items-center justify-center min-w-5 h-5 px-1.5 crm-radius-toggle bg-primary/10 text-primary text-[11px] font-semibold leading-none">
{warehouses.length}
</span>
</div>
<button type="button" onClick={() => setIsModalOpen(true)} className={primaryButton}>
<Plus className="w-3.5 h-3.5" />
Add warehouse
</button>
</div>
<div className={dataCardShell}>
{loading ? (
<div className="py-16 flex justify-center">
<Loader2 className="w-6 h-6 animate-spin text-primary" />
</div>
) : (
<div className="overflow-x-auto">
<table className="crm-data-table min-w-[720px]">
<thead>
<tr className="border-b border-border">
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Warehouse</th>
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Code</th>
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Address</th>
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Status</th>
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Created</th>
</tr>
</thead>
<tbody>
{warehouses.length === 0 ? (
<tr>
<td colSpan={5} className="px-4 py-16 text-center text-[13px] text-muted-foreground whitespace-normal">No warehouses yet</td>
</tr>
) : warehouses.map((warehouse) => (
<tr key={warehouse.warehouse_id} className="border-b border-border last:border-b-0 hover:bg-muted/10">
<td className="px-4 py-2.5 text-[13px] font-semibold text-foreground">{warehouse.name}</td>
<td className="px-4 py-2.5">
<span className="inline-flex items-center px-2.5 py-1 crm-radius-badge bg-primary/10 text-primary text-[11px] font-semibold">
{warehouse.code}
</span>
</td>
<td className="px-4 py-2.5 text-[13px] text-muted-foreground">
<span className="inline-flex items-center gap-1.5">
<MapPin className="w-3.5 h-3.5 shrink-0" />
{warehouse.location_address || '—'}
</span>
</td>
<td className="px-4 py-2.5">
<span className={`inline-flex items-center px-2.5 py-1 crm-radius-badge text-[11px] font-semibold ${
warehouse.is_active ? 'bg-success/15 text-success' : 'bg-muted text-muted-foreground'
}`}>
{warehouse.is_active ? 'Active' : 'Inactive'}
</span>
</td>
<td className="px-4 py-2.5 text-[13px] text-muted-foreground">
{(() => {
const created = warehouse.created_at ? new Date(warehouse.created_at) : null;
return created && !Number.isNaN(created.getTime()) ? format(created, 'dd-MMM-yyyy') : '—';
})()}
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
{isModalOpen && (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
<div className={`${dataCardShell} w-full max-w-md p-5`}>
<div className="flex items-center justify-between gap-3 mb-4">
<h2 className="text-[15px] font-semibold text-foreground">New warehouse</h2>
<button
type="button"
onClick={() => setIsModalOpen(false)}
className="w-8 h-8 crm-radius-control hover:bg-muted text-muted-foreground cursor-pointer flex items-center justify-center"
aria-label="Close"
>
<X className="w-4 h-4" />
</button>
</div>
<form onSubmit={handleCreate} className="space-y-3">
<div>
<label className={labelClass}>Name</label>
<input
type="text"
required
value={name}
onChange={(e) => setName(e.target.value)}
className={fieldClass}
/>
</div>
<div>
<label className={labelClass}>Code</label>
<input
type="text"
required
value={code}
onChange={(e) => setCode(e.target.value)}
className={fieldClass}
/>
</div>
<div>
<label className={labelClass}>Address</label>
<input
type="text"
value={location}
onChange={(e) => setLocation(e.target.value)}
className={fieldClass}
/>
</div>
<div className="flex justify-end gap-2 pt-2">
<button type="button" onClick={() => setIsModalOpen(false)} className={secondaryButton}>
Cancel
</button>
<button type="submit" disabled={saving} className={primaryButton}>
{saving ? 'Saving' : 'Save'}
</button>
</div>
</form>
</div>
</div>
)}
</div>
);
}