- Backend: FastAPI app with all models, services, schemas, routes - Admin Panel: Next.js dashboard with all modules (CRM, POS, repairs, warranties, billing, notifications, etc.) - Ecommerce: storefront (submodule) + product catalog - AI Agents: .agents/ skill library for Antigravity IDE - Docs: architecture, billing, notification gateway markdown - Submodules: Backend/OpenWA, Ecommerce/File 2/storefront
403 lines
17 KiB
TypeScript
403 lines
17 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
import {
|
|
RefreshCw,
|
|
Search,
|
|
Shield,
|
|
ShieldAlert,
|
|
ShieldCheck,
|
|
Ban,
|
|
Clock
|
|
} from 'lucide-react';
|
|
import { toast } from 'sonner';
|
|
import { format } from 'date-fns';
|
|
import { apiFetch } from '@/services/api/client';
|
|
import { TablePagination, useClientPagination } from '@/components/ui/TablePagination';
|
|
import { SlideOver } from '@/components/ui/SlideOver';
|
|
|
|
interface WarrantySchema {
|
|
warranty_id: string;
|
|
tenant_id: string;
|
|
customer_id: string;
|
|
reference_type: string;
|
|
reference_id: string;
|
|
product_id?: string;
|
|
order_item_id?: string;
|
|
service_job_id?: string;
|
|
serial_no_imei?: string;
|
|
start_date: string;
|
|
end_date: string;
|
|
status: string;
|
|
warranty_type: string;
|
|
terms?: string;
|
|
void_reason?: string;
|
|
created_at: string;
|
|
}
|
|
|
|
type StatusFilter = 'all' | 'active' | 'expired' | 'void';
|
|
|
|
function formatDisplayDate(iso: string) {
|
|
if (!iso) return '-';
|
|
const normalized = iso.endsWith('Z') || iso.includes('+') ? iso : `${iso}Z`;
|
|
const date = new Date(normalized);
|
|
if (Number.isNaN(date.getTime())) return iso;
|
|
return format(date, 'dd-MMM-yyyy HH:mm');
|
|
}
|
|
|
|
function statusBadgeClass(status?: string) {
|
|
const s = (status || '').toUpperCase();
|
|
if (s === 'ACTIVE') return 'bg-success text-white';
|
|
if (s === 'EXPIRED') return 'bg-warning text-white';
|
|
if (s === 'VOID') return 'bg-destructive text-white';
|
|
return 'bg-muted text-muted-foreground';
|
|
}
|
|
|
|
export default function WarrantiesPage() {
|
|
const [warranties, setWarranties] = useState<WarrantySchema[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [searchQuery, setSearchQuery] = useState('');
|
|
const [statusFilter, setStatusFilter] = useState<StatusFilter>('all');
|
|
|
|
const [selectedWarranty, setSelectedWarranty] = useState<WarrantySchema | null>(null);
|
|
const [showVoidForm, setShowVoidForm] = useState(false);
|
|
const [voidReason, setVoidReason] = useState('');
|
|
const [voiding, setVoiding] = useState(false);
|
|
|
|
const fetchWarranties = async () => {
|
|
setLoading(true);
|
|
try {
|
|
const data = await apiFetch<WarrantySchema[]>('/api/v1/warranties');
|
|
setWarranties(data || []);
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to fetch warranties');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
fetchWarranties();
|
|
}, []);
|
|
|
|
const handleVoidWarranty = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (!selectedWarranty) return;
|
|
if (!voidReason.trim()) {
|
|
toast.error('Void reason is required');
|
|
return;
|
|
}
|
|
|
|
setVoiding(true);
|
|
try {
|
|
await apiFetch(`/api/v1/warranties/${selectedWarranty.warranty_id}/void`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ void_reason: voidReason }),
|
|
});
|
|
toast.success('Warranty has been VOIDED successfully');
|
|
setShowVoidForm(false);
|
|
setSelectedWarranty(null);
|
|
setVoidReason('');
|
|
fetchWarranties();
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to void warranty');
|
|
} finally {
|
|
setVoiding(false);
|
|
}
|
|
};
|
|
|
|
const activeCount = warranties.filter((w) => w.status === 'ACTIVE').length;
|
|
const expiredCount = warranties.filter((w) => w.status === 'EXPIRED').length;
|
|
const voidCount = warranties.filter((w) => w.status === 'VOID').length;
|
|
|
|
const filteredData = useMemo(() => {
|
|
const q = searchQuery.trim().toLowerCase();
|
|
return warranties.filter((w) => {
|
|
const statusMatch =
|
|
statusFilter === 'all' ||
|
|
(statusFilter === 'active' && w.status === 'ACTIVE') ||
|
|
(statusFilter === 'expired' && w.status === 'EXPIRED') ||
|
|
(statusFilter === 'void' && w.status === 'VOID');
|
|
|
|
const searchMatch =
|
|
!q ||
|
|
w.warranty_id.toLowerCase().includes(q) ||
|
|
w.reference_id.toLowerCase().includes(q) ||
|
|
(w.serial_no_imei && w.serial_no_imei.toLowerCase().includes(q));
|
|
return statusMatch && searchMatch;
|
|
});
|
|
}, [warranties, searchQuery, statusFilter]);
|
|
|
|
const pager = useClientPagination(filteredData);
|
|
|
|
const dataCardShell =
|
|
'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] overflow-hidden min-w-0';
|
|
|
|
return (
|
|
<div className="flex flex-col gap-5 min-w-0">
|
|
<div className="flex flex-wrap items-start justify-between gap-3 min-w-0">
|
|
<div className="min-w-0">
|
|
<div className="flex items-center gap-2">
|
|
<h1 className="text-lg font-semibold text-foreground">Warranty Management</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">
|
|
{warranties.length}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-2 shrink-0">
|
|
<button
|
|
type="button"
|
|
onClick={fetchWarranties}
|
|
className="w-8 h-8 crm-radius-control border border-border bg-card text-muted-foreground hover:text-foreground hover:bg-muted cursor-pointer transition-colors flex items-center justify-center"
|
|
title="Reload"
|
|
>
|
|
<RefreshCw className="w-3.5 h-3.5" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className={dataCardShell}>
|
|
<div className="px-5 pt-5">
|
|
<div className="-mx-5 px-5 flex flex-wrap items-center justify-between gap-3 min-w-0 pb-4 border-b border-border">
|
|
<div className="relative flex-1 min-w-[180px] max-w-[480px]">
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground pointer-events-none" />
|
|
<input
|
|
type="text"
|
|
placeholder="Search Warranty ID, Reference ID, Serial No..."
|
|
value={searchQuery}
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
|
className="w-full h-9 pl-9 pr-3 crm-radius-control border border-border bg-card text-[13px] text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-primary transition-colors"
|
|
/>
|
|
</div>
|
|
<div className="crm-radius-toggle inline-flex items-center gap-1 border border-border bg-card p-1 shrink-0 overflow-x-auto max-w-full">
|
|
{(
|
|
[
|
|
{ id: 'all', label: `All (${warranties.length})` },
|
|
{ id: 'active', label: `Active (${activeCount})` },
|
|
{ id: 'expired', label: `Expired (${expiredCount})` },
|
|
{ id: 'void', label: `Void (${voidCount})` },
|
|
] as { id: StatusFilter; label: string }[]
|
|
).map((tab) => (
|
|
<button
|
|
key={tab.id}
|
|
type="button"
|
|
onClick={() => setStatusFilter(tab.id)}
|
|
className={`h-8 px-3 crm-radius-toggle text-[13px] font-medium cursor-pointer transition-colors whitespace-nowrap ${
|
|
statusFilter === tab.id ? 'bg-primary text-white' : 'text-foreground hover:bg-muted'
|
|
}`}
|
|
>
|
|
{tab.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="flex flex-col items-center justify-center py-16 gap-2.5">
|
|
<RefreshCw className="w-6 h-6 text-primary animate-spin" />
|
|
<span className="text-[13px] text-muted-foreground">Loading warranties...</span>
|
|
</div>
|
|
) : (
|
|
<div className="overflow-x-auto">
|
|
<table className="crm-data-table min-w-[860px]">
|
|
<thead>
|
|
<tr>
|
|
<th>Warranty Details</th>
|
|
<th>Reference</th>
|
|
<th>Dates</th>
|
|
<th>Serial/IMEI</th>
|
|
<th>Status</th>
|
|
<th className="text-right">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{pager.items.length === 0 ? (
|
|
<tr>
|
|
<td colSpan={6} className="text-center py-10">
|
|
<div className="flex flex-col items-center justify-center gap-2">
|
|
<Shield className="w-8 h-8 text-muted-foreground" />
|
|
<p className="text-[13px] text-muted-foreground font-medium">No warranties found.</p>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
) : (
|
|
pager.items.map((w) => (
|
|
<tr key={w.warranty_id} className="hover:bg-muted/30 transition-colors">
|
|
<td>
|
|
<div className="flex flex-col gap-0.5">
|
|
<span className="font-semibold text-foreground">{w.warranty_id}</span>
|
|
<span className="text-muted-foreground text-[11px]">{w.warranty_type}</span>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<div className="flex flex-col gap-0.5">
|
|
<span className="font-medium text-foreground">{w.reference_id}</span>
|
|
<span className="text-muted-foreground text-[11px]">{w.reference_type}</span>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<div className="flex flex-col gap-0.5">
|
|
<span className="text-[12px]"><span className="text-muted-foreground">Start:</span> {formatDisplayDate(w.start_date)}</span>
|
|
<span className="text-[12px]"><span className="text-muted-foreground">End:</span> {formatDisplayDate(w.end_date)}</span>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<span className="text-[12px] font-medium text-foreground">{w.serial_no_imei || '-'}</span>
|
|
</td>
|
|
<td>
|
|
<span className={`inline-flex items-center px-2 py-0.5 text-[10px] font-bold uppercase rounded ${statusBadgeClass(w.status)}`}>
|
|
{w.status}
|
|
</span>
|
|
</td>
|
|
<td className="text-right">
|
|
<button
|
|
type="button"
|
|
onClick={() => setSelectedWarranty(w)}
|
|
className="text-[13px] font-semibold text-primary hover:text-primary/80"
|
|
>
|
|
Details
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
))
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
|
|
{!loading && pager.items.length > 0 && (
|
|
<TablePagination
|
|
page={pager.page}
|
|
totalPages={pager.totalPages}
|
|
total={pager.total}
|
|
pageSize={pager.pageSize}
|
|
onPageChange={pager.onPageChange}
|
|
/>
|
|
)}
|
|
</div>
|
|
|
|
{/* SlideOver for Warranty Details */}
|
|
<SlideOver
|
|
open={!!selectedWarranty}
|
|
onClose={() => {
|
|
setSelectedWarranty(null);
|
|
setShowVoidForm(false);
|
|
setVoidReason('');
|
|
}}
|
|
title="Warranty Details"
|
|
size="form"
|
|
>
|
|
{selectedWarranty && (
|
|
<div className="flex flex-col h-full">
|
|
<div className="flex-1 overflow-y-auto p-5">
|
|
<div className="flex items-start justify-between gap-4 mb-6">
|
|
<div>
|
|
<h3 className="text-base font-bold text-foreground">
|
|
{selectedWarranty.warranty_id}
|
|
</h3>
|
|
<p className="text-[13px] text-muted-foreground mt-0.5">
|
|
{selectedWarranty.warranty_type} Warranty
|
|
</p>
|
|
</div>
|
|
<span className={`inline-flex items-center px-2.5 py-1 rounded text-[11px] font-bold uppercase ${statusBadgeClass(selectedWarranty.status)}`}>
|
|
{selectedWarranty.status}
|
|
</span>
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-4 mb-6 bg-muted/40 p-4 rounded-md border border-border/50">
|
|
<div>
|
|
<span className="block text-[11px] font-medium text-muted-foreground uppercase mb-1">Reference ID</span>
|
|
<span className="text-[13px] font-semibold text-foreground">{selectedWarranty.reference_id}</span>
|
|
</div>
|
|
<div>
|
|
<span className="block text-[11px] font-medium text-muted-foreground uppercase mb-1">Type</span>
|
|
<span className="text-[13px] font-semibold text-foreground">{selectedWarranty.reference_type}</span>
|
|
</div>
|
|
<div>
|
|
<span className="block text-[11px] font-medium text-muted-foreground uppercase mb-1">Start Date</span>
|
|
<span className="text-[13px] font-medium text-foreground">{formatDisplayDate(selectedWarranty.start_date)}</span>
|
|
</div>
|
|
<div>
|
|
<span className="block text-[11px] font-medium text-muted-foreground uppercase mb-1">End Date</span>
|
|
<span className="text-[13px] font-medium text-foreground">{formatDisplayDate(selectedWarranty.end_date)}</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="mb-6 space-y-4">
|
|
<div>
|
|
<span className="block text-[12px] font-semibold text-foreground mb-1">Terms</span>
|
|
<div className="text-[13px] text-muted-foreground bg-card border border-border p-3 rounded-md whitespace-pre-wrap">
|
|
{selectedWarranty.terms || 'No specific terms provided.'}
|
|
</div>
|
|
</div>
|
|
|
|
{selectedWarranty.status === 'VOID' && selectedWarranty.void_reason && (
|
|
<div className="bg-destructive/10 border border-destructive/20 p-3 rounded-md">
|
|
<span className="block text-[12px] font-bold text-destructive mb-1 flex items-center gap-1.5">
|
|
<Ban className="w-3.5 h-3.5" />
|
|
Void Reason
|
|
</span>
|
|
<p className="text-[13px] text-destructive/90">{selectedWarranty.void_reason}</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{selectedWarranty.status === 'ACTIVE' && (
|
|
<div className="border-t border-border pt-6 mt-2">
|
|
{!showVoidForm ? (
|
|
<button
|
|
type="button"
|
|
onClick={() => setShowVoidForm(true)}
|
|
className="inline-flex items-center justify-center h-9 px-4 rounded-md bg-destructive/10 text-destructive hover:bg-destructive hover:text-white text-[13px] font-semibold transition-colors"
|
|
>
|
|
<Ban className="w-4 h-4 mr-2" />
|
|
Void Warranty
|
|
</button>
|
|
) : (
|
|
<form onSubmit={handleVoidWarranty} className="space-y-4 bg-muted/30 p-4 rounded-md border border-border">
|
|
<div>
|
|
<label className="block text-[12px] font-semibold text-foreground mb-1.5">
|
|
Reason for Voiding
|
|
</label>
|
|
<textarea
|
|
value={voidReason}
|
|
onChange={(e) => setVoidReason(e.target.value)}
|
|
placeholder="e.g. Physical damage, water damage, fraudulent claim..."
|
|
required
|
|
className="w-full p-3 bg-card border border-border rounded-md text-[13px] text-foreground focus:outline-none focus:border-destructive min-h-[100px] resize-none"
|
|
/>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<button
|
|
type="submit"
|
|
disabled={voiding || !voidReason.trim()}
|
|
className="inline-flex items-center justify-center h-9 px-4 rounded-md bg-destructive text-white hover:bg-destructive/90 text-[13px] font-semibold disabled:opacity-50"
|
|
>
|
|
{voiding ? 'Voiding...' : 'Confirm Void'}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => {
|
|
setShowVoidForm(false);
|
|
setVoidReason('');
|
|
}}
|
|
disabled={voiding}
|
|
className="inline-flex items-center justify-center h-9 px-4 rounded-md border border-border bg-card text-foreground hover:bg-muted text-[13px] font-medium disabled:opacity-50"
|
|
>
|
|
Cancel
|
|
</button>
|
|
</div>
|
|
</form>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</SlideOver>
|
|
</div>
|
|
);
|
|
}
|