174 lines
8.1 KiB
TypeScript
174 lines
8.1 KiB
TypeScript
'use client';
|
|
|
|
import { useState, useEffect } from 'react';
|
|
import { Check, X, RefreshCw, Search, Loader2 } from 'lucide-react';
|
|
import { apiFetch } from '@/services/api/client';
|
|
import { toast } from 'sonner';
|
|
import { TablePagination, useClientPagination } from '@/components/ui/TablePagination';
|
|
import { formatCurrency } from '@/lib/utils';
|
|
|
|
function statusClass(status?: string) {
|
|
const value = (status || '').toUpperCase();
|
|
if (value === 'APPROVED') return 'bg-success/15 text-success';
|
|
if (value === 'PENDING') return 'bg-warning/15 text-warning';
|
|
if (value === 'REJECTED') return 'bg-destructive/10 text-destructive';
|
|
return 'bg-muted text-muted-foreground';
|
|
}
|
|
|
|
export default function RefundsPage() {
|
|
const [refunds, setRefunds] = useState<any[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [searchQuery, setSearchQuery] = useState('');
|
|
|
|
const loadRefunds = async () => {
|
|
setLoading(true);
|
|
try {
|
|
const data = await apiFetch<any[]>('/api/v1/refunds');
|
|
setRefunds(Array.isArray(data) ? data : []);
|
|
} catch (err: any) {
|
|
toast.error(err.message || 'Failed to load refunds');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
loadRefunds();
|
|
}, []);
|
|
|
|
const handleAction = async (refundId: string, action: 'APPROVE' | 'REJECT') => {
|
|
try {
|
|
const payload = action === 'APPROVE'
|
|
? { admin_notes: "Approved from UI" }
|
|
: { rejection_reason: "Rejected from UI" };
|
|
|
|
await apiFetch(`/api/v1/refunds/${refundId}/${action.toLowerCase()}`, {
|
|
method: 'POST',
|
|
body: JSON.stringify(payload)
|
|
});
|
|
toast.success(`Refund ${action === 'APPROVE' ? 'Approved' : 'Rejected'}!`);
|
|
loadRefunds();
|
|
} catch (err: any) {
|
|
toast.error(err.message || `Failed to ${action.toLowerCase()} refund`);
|
|
}
|
|
};
|
|
|
|
const filteredRefunds = refunds.filter((r) =>
|
|
JSON.stringify(r).toLowerCase().includes(searchQuery.toLowerCase())
|
|
);
|
|
|
|
const pager = useClientPagination(filteredRefunds, 10);
|
|
|
|
const dataCardShell =
|
|
'crm-radius-section border border-border bg-card shadow-[0_1px_3px_rgba(16,24,40,0.06)] min-w-0';
|
|
|
|
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">Refunds</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">
|
|
{filteredRefunds.length}
|
|
</span>
|
|
</div>
|
|
<button
|
|
type="button"
|
|
onClick={loadRefunds}
|
|
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 ${loading ? 'animate-spin' : ''}`} />
|
|
</button>
|
|
</div>
|
|
|
|
<div className={dataCardShell}>
|
|
<div className="p-4">
|
|
<div className="relative w-full sm:w-[240px]">
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 w-3.5 h-3.5 text-muted-foreground pointer-events-none" />
|
|
<input
|
|
type="text"
|
|
placeholder="Search"
|
|
value={searchQuery}
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
|
className="w-full h-9 pl-8 pr-3 crm-radius-control border border-border bg-card text-[13px] text-foreground placeholder:text-muted-foreground focus:outline-none focus:border-primary"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="py-16 flex justify-center border-t border-border">
|
|
<Loader2 className="w-6 h-6 text-primary animate-spin" />
|
|
</div>
|
|
) : (
|
|
<div className="overflow-x-auto border-t border-border">
|
|
<table className="crm-data-table min-w-[860px]">
|
|
<thead>
|
|
<tr className="border-b border-border">
|
|
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Request ID</th>
|
|
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Entity</th>
|
|
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Part No. Verif.</th>
|
|
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Amount</th>
|
|
<th className="px-4 py-2.5 text-left font-medium text-muted-foreground">Reason</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-center font-medium text-muted-foreground">Action</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{pager.items.length === 0 ? (
|
|
<tr>
|
|
<td colSpan={7} className="px-4 py-16 text-center text-[13px] text-muted-foreground whitespace-normal">
|
|
{searchQuery ? 'No refunds match your search' : 'No refunds yet'}
|
|
</td>
|
|
</tr>
|
|
) : pager.items.map((refund) => (
|
|
<tr key={refund.refund_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">
|
|
{String(refund.refund_id || '').slice(-8).toUpperCase()}
|
|
</td>
|
|
<td className="px-4 py-2.5">
|
|
<div className="text-[13px] font-semibold text-foreground">
|
|
{refund.entity_type === 'ECOM_ORDER' ? 'Order' : 'Service'}
|
|
</div>
|
|
<div className="text-[11px] text-muted-foreground">{refund.entity_id}</div>
|
|
</td>
|
|
<td className="px-4 py-2.5 text-[13px] text-foreground">{refund.part_number_verification || '—'}</td>
|
|
<td className="px-4 py-2.5 text-left text-[13px] font-semibold text-foreground">{formatCurrency(refund.amount)}</td>
|
|
<td className="px-4 py-2.5 text-[13px] text-muted-foreground max-w-[220px] truncate">{refund.reason || '—'}</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 ${statusClass(refund.status)}`}>
|
|
{refund.status}
|
|
</span>
|
|
</td>
|
|
<td className="px-4 py-2.5 text-center">
|
|
{refund.status === 'PENDING' && (
|
|
<div className="inline-flex items-center justify-center gap-1.5">
|
|
<button
|
|
type="button"
|
|
onClick={() => handleAction(refund.refund_id, 'APPROVE')}
|
|
className="w-8 h-8 crm-radius-control flex items-center justify-center text-success bg-success/10 hover:bg-success/20 cursor-pointer"
|
|
title="Approve"
|
|
>
|
|
<Check className="w-3.5 h-3.5" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => handleAction(refund.refund_id, 'REJECT')}
|
|
className="w-8 h-8 crm-radius-control flex items-center justify-center text-destructive bg-destructive/10 hover:bg-destructive/20 cursor-pointer"
|
|
title="Reject"
|
|
>
|
|
<X className="w-3.5 h-3.5" />
|
|
</button>
|
|
</div>
|
|
)}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
{!loading && pager.items.length > 0 && <TablePagination {...pager} />}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|