Files
gg-backend/frontend/src/components/PackageInquiriesTab/PackageInquiriesTab.tsx
T

310 lines
12 KiB
TypeScript
Raw Normal View History

2026-05-26 15:48:01 +05:30
import { useState, useEffect, useCallback } from 'react';
import { getAllInquiriesApi, HealthInquiry } from '@/api/healthCheck';
2026-05-15 17:58:25 +05:30
2026-05-26 15:48:01 +05:30
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Badge } from '@/components/ui/badge';
2026-07-02 16:48:25 +05:30
import { Loader2, RefreshCw, ChevronLeft, ChevronRight, CalendarDays } from 'lucide-react';
2026-05-15 17:58:25 +05:30
export default function PackageInquiriesTab() {
const [inquiries, setInquiries] = useState<HealthInquiry[]>([]);
const [loading, setLoading] = useState(true);
2026-07-02 16:48:25 +05:30
const [searchText, setSearchText] = useState('');
const [dateMode, setDateMode] = useState<'preferred' | 'booked'>('preferred');
2026-05-26 15:48:01 +05:30
const [filterDate, setFilterDate] = useState('');
const [startDate, setStartDate] = useState('');
const [endDate, setEndDate] = useState('');
2026-05-15 17:58:25 +05:30
2026-07-02 16:48:25 +05:30
const [createdDate, setCreatedDate] = useState('');
const [createdStartDate, setCreatedStartDate] = useState('');
const [createdEndDate, setCreatedEndDate] = useState('');
2026-05-15 17:58:25 +05:30
const [currentPage, setCurrentPage] = useState(1);
const [itemsPerPage, setItemsPerPage] = useState(10);
const [totalItems, setTotalItems] = useState(0);
const [totalPages, setTotalPages] = useState(1);
const fetchInquiries = useCallback(async () => {
setLoading(true);
try {
2026-07-02 16:48:25 +05:30
const res = await getAllInquiriesApi(
currentPage,
itemsPerPage,
filterDate,
startDate,
endDate,
createdDate,
createdStartDate,
createdEndDate,
searchText
);
2026-05-15 17:58:25 +05:30
setInquiries(res.data || []);
setTotalItems(res.pagination?.total || 0);
setTotalPages(res.pagination?.totalPages || 1);
} catch (err) {
2026-05-26 15:48:01 +05:30
console.error('Failed to fetch inquiries', err);
2026-05-15 17:58:25 +05:30
} finally {
setLoading(false);
}
2026-07-02 16:48:25 +05:30
}, [
currentPage,
itemsPerPage,
filterDate,
startDate,
endDate,
createdDate,
createdStartDate,
createdEndDate,
searchText,
]);
2026-05-15 17:58:25 +05:30
useEffect(() => {
fetchInquiries();
}, [fetchInquiries]);
2026-07-02 16:48:25 +05:30
const handleModeChange = (newMode: 'preferred' | 'booked') => {
setDateMode(newMode);
setFilterDate('');
setStartDate('');
setEndDate('');
setCreatedDate('');
setCreatedStartDate('');
setCreatedEndDate('');
2026-05-15 17:58:25 +05:30
setCurrentPage(1);
};
2026-07-02 16:48:25 +05:30
useEffect(() => {
setCurrentPage(1);
}, [searchText, filterDate, startDate, endDate, createdDate, createdStartDate, createdEndDate, itemsPerPage]);
2026-05-15 17:58:25 +05:30
const indexOfFirstItem = (currentPage - 1) * itemsPerPage;
const indexOfLastItem = Math.min(currentPage * itemsPerPage, totalItems);
return (
2026-07-02 16:48:25 +05:30
<Card className="shadow-sm">
<CardHeader className="flex flex-col gap-4 border-b pb-4 w-full">
<div className="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-2 w-full">
<CardTitle className="text-xl flex items-center gap-2">
<span>Package Inquiries</span>
</CardTitle>
<Button variant="outline" size="sm" onClick={fetchInquiries} disabled={loading} className="text-sm">
<RefreshCw className={`mr-2 h-4 w-4 ${loading ? 'animate-spin' : ''}`} />
Refresh
</Button>
</div>
2026-05-15 17:58:25 +05:30
2026-07-02 16:48:25 +05:30
<div className="grid grid-cols-1 xl:grid-cols-4 gap-4 bg-muted/20 p-4 rounded-lg border w-full">
<div className="flex flex-col gap-1.5 w-full">
<label className="text-xs font-bold text-muted-foreground uppercase tracking-wider">Search Inquiry</label>
2026-05-15 17:58:25 +05:30
<Input
2026-07-02 16:48:25 +05:30
placeholder="Search name, phone, email, or package name..."
value={searchText}
onChange={(e) => setSearchText(e.target.value)}
className="text-base h-10 bg-background w-full"
2026-05-15 17:58:25 +05:30
/>
</div>
2026-07-02 16:48:25 +05:30
<div className="flex flex-col gap-1.5 w-full">
<label className="text-xs font-bold text-muted-foreground uppercase tracking-wider flex items-center gap-1">
<CalendarDays className="h-3.5 w-3.5 text-primary" /> Filter Date By
</label>
<select
value={dateMode}
onChange={(e) => handleModeChange(e.target.value as 'preferred' | 'booked')}
className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm font-medium focus:ring-2 focus:ring-primary focus:outline-none"
>
<option value="preferred">🗓 Preferred Date</option>
<option value="booked"> Booked Date (Created)</option>
</select>
2026-05-15 17:58:25 +05:30
</div>
2026-07-02 16:48:25 +05:30
<div className="flex items-end gap-2 p-1.5 bg-background rounded-md border border-dashed flex-wrap sm:flex-nowrap w-full">
<div className="flex flex-col gap-1 flex-1 min-w-[80px]">
<span className="text-[10px] font-bold text-muted-foreground uppercase">Specific Day</span>
<Input
type="date"
value={dateMode === 'preferred' ? filterDate : createdDate}
onChange={(e) =>
dateMode === 'preferred' ? setFilterDate(e.target.value) : setCreatedDate(e.target.value)
}
className="text-sm h-8"
disabled={dateMode === 'preferred' ? !!startDate || !!endDate : !!createdStartDate || !!createdEndDate}
/>
</div>
<div className="flex flex-col gap-1 flex-1 min-w-[80px]">
<span className="text-[10px] font-bold text-muted-foreground uppercase">From Range</span>
<Input
type="date"
value={dateMode === 'preferred' ? startDate : createdStartDate}
onChange={(e) =>
dateMode === 'preferred' ? setStartDate(e.target.value) : setCreatedStartDate(e.target.value)
}
className="text-sm h-8"
disabled={dateMode === 'preferred' ? !!filterDate : !!createdDate}
/>
</div>
<div className="flex flex-col gap-1 flex-1 min-w-[80px]">
<span className="text-[10px] font-bold text-muted-foreground uppercase">To Range</span>
<Input
type="date"
value={dateMode === 'preferred' ? endDate : createdEndDate}
onChange={(e) =>
dateMode === 'preferred' ? setEndDate(e.target.value) : setCreatedEndDate(e.target.value)
}
className="text-sm h-8"
disabled={dateMode === 'preferred' ? !!filterDate : !!createdDate}
/>
</div>
2026-05-15 17:58:25 +05:30
</div>
2026-07-02 16:48:25 +05:30
<div className="flex flex-col gap-1.5 w-full">
<label className="text-xs font-bold text-muted-foreground uppercase tracking-wider">Page Sizing</label>
2026-05-15 17:58:25 +05:30
<select
value={itemsPerPage}
2026-07-02 16:48:25 +05:30
onChange={(e) => setItemsPerPage(Number(e.target.value))}
className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm focus:ring-2 focus:ring-primary focus:outline-none"
2026-05-26 15:48:01 +05:30
>
2026-07-02 16:48:25 +05:30
<option value={5}>5 Rows</option>
<option value={10}>10 Rows</option>
<option value={20}>20 Rows</option>
<option value={50}>50 Rows</option>
2026-05-15 17:58:25 +05:30
</select>
</div>
</div>
</CardHeader>
2026-07-02 16:48:25 +05:30
<CardContent className="p-0 sm:p-6 sm:pt-4">
2026-05-15 17:58:25 +05:30
<div className="rounded-md border overflow-x-auto overflow-y-auto max-h-[650px] relative">
2026-07-02 16:48:25 +05:30
<Table className="w-full min-w-[1100px] table-fixed border-separate border-spacing-0">
2026-05-15 17:58:25 +05:30
<TableHeader className="sticky top-0 z-20 bg-background shadow-sm">
<TableRow>
2026-07-02 16:48:25 +05:30
<TableHead className="w-[145px] font-bold bg-background text-sm">Preferred Date</TableHead>
<TableHead className="w-[145px] font-bold bg-background text-sm">Booked Date</TableHead>
<TableHead className="w-[220px] font-bold bg-background text-sm">Patient Details</TableHead>
<TableHead className="w-[240px] font-bold bg-background text-sm">Requested Package</TableHead>
<TableHead className="w-[120px] font-bold bg-background text-sm">Demographics</TableHead>
<TableHead className="w-[230px] font-bold bg-background text-sm">Message</TableHead>
2026-05-15 17:58:25 +05:30
</TableRow>
</TableHeader>
<TableBody>
{loading ? (
<TableRow>
2026-07-02 16:48:25 +05:30
<TableCell colSpan={6} className="text-center py-10">
<Loader2 className="h-8 w-8 animate-spin mx-auto text-primary" />
2026-05-15 17:58:25 +05:30
</TableCell>
</TableRow>
) : inquiries.length === 0 ? (
<TableRow>
2026-07-02 16:48:25 +05:30
<TableCell colSpan={6} className="text-center text-muted-foreground py-10 text-base">
No package inquiries matched your active search filters.
2026-05-15 17:58:25 +05:30
</TableCell>
</TableRow>
) : (
inquiries.map((inq) => (
2026-07-02 16:48:25 +05:30
<TableRow key={inq.id} className="hover:bg-muted/50 transition-colors">
2026-05-15 17:58:25 +05:30
<TableCell>
2026-07-02 16:48:25 +05:30
{inq.preferredDate ? (
<div
className={`text-sm font-semibold rounded px-1.5 py-0.5 inline-block ${dateMode === 'preferred' ? 'bg-primary/10 text-primary' : 'bg-muted'}`}
>
{new Date(inq.preferredDate).toLocaleDateString('en-GB')}
</div>
) : (
<span className="text-xs text-muted-foreground italic">Not specified</span>
)}
</TableCell>
<TableCell>
<div
className={`text-sm rounded px-1.5 py-0.5 inline-block ${dateMode === 'booked' ? 'bg-sky-500/10 text-sky-600 font-semibold' : 'text-muted-foreground'}`}
>
{new Date(inq.createdAt).toLocaleDateString('en-GB')}
2026-05-15 17:58:25 +05:30
</div>
</TableCell>
<TableCell>
2026-07-02 16:48:25 +05:30
<div className="font-semibold text-base text-gray-900 truncate">{inq.fullName}</div>
<div className="text-sm text-gray-700 mt-0.5">{inq.mobileNumber}</div>
{inq.email && <div className="text-xs text-muted-foreground truncate">{inq.email}</div>}
2026-05-15 17:58:25 +05:30
</TableCell>
<TableCell>
2026-07-02 16:48:25 +05:30
<div className="font-semibold text-sm line-clamp-2 text-gray-800" title={inq.healthPackage?.name}>
{inq.healthPackage?.name || 'N/A'}
</div>
{inq.healthPackage?.category?.name && (
<Badge variant="secondary" className="text-[10px] font-normal px-2 py-0 mt-1">
{inq.healthPackage.category.name}
</Badge>
)}
2026-05-15 17:58:25 +05:30
</TableCell>
<TableCell>
2026-07-02 16:48:25 +05:30
<div className="font-medium text-sm text-gray-800">{inq.age ? `${inq.age} yrs` : '—'}</div>
<div className="text-xs text-muted-foreground capitalize mt-0.5">
{inq.gender || 'unspecified'}
2026-05-15 17:58:25 +05:30
</div>
</TableCell>
2026-07-02 16:48:25 +05:30
<TableCell>
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<div className="text-sm italic line-clamp-3 text-muted-foreground whitespace-pre-wrap cursor-pointer leading-relaxed hover:text-foreground transition-colors">
{inq.message || 'No message provided.'}
</div>
</TooltipTrigger>
<TooltipContent className="max-w-md whitespace-pre-wrap p-3 bg-popover text-popover-foreground border shadow-md rounded-md">
2026-05-26 15:48:01 +05:30
{inq.message || 'No message provided.'}
2026-07-02 16:48:25 +05:30
</TooltipContent>
</Tooltip>
</TooltipProvider>
</TableCell>
2026-05-15 17:58:25 +05:30
</TableRow>
))
)}
</TableBody>
</Table>
</div>
{!loading && totalItems > 0 && (
<div className="flex flex-col sm:flex-row items-center justify-between px-2 py-4 border-t gap-4 mt-2">
<div className="text-sm text-muted-foreground">
2026-05-26 15:48:01 +05:30
Showing <span className="font-semibold">{indexOfFirstItem + 1}</span> to{' '}
<span className="font-semibold">{indexOfLastItem}</span> of{' '}
2026-05-15 17:58:25 +05:30
<span className="font-semibold">{totalItems}</span> inquiries
</div>
<div className="flex items-center gap-6">
2026-07-02 16:48:25 +05:30
<div className="text-sm font-medium text-gray-700">
2026-05-15 17:58:25 +05:30
Page {currentPage} of {totalPages || 1}
</div>
2026-07-02 16:48:25 +05:30
<div className="flex gap-1">
2026-05-15 17:58:25 +05:30
<Button
variant="outline"
size="icon"
className="h-9 w-9"
2026-05-26 15:48:01 +05:30
onClick={() => setCurrentPage((prev) => Math.max(prev - 1, 1))}
disabled={currentPage === 1}
>
2026-05-15 17:58:25 +05:30
<ChevronLeft className="h-4 w-4" />
</Button>
<Button
variant="outline"
size="icon"
className="h-9 w-9"
2026-05-26 15:48:01 +05:30
onClick={() => setCurrentPage((prev) => Math.min(prev + 1, totalPages))}
disabled={currentPage === totalPages || totalPages === 0}
>
2026-05-15 17:58:25 +05:30
<ChevronRight className="h-4 w-4" />
</Button>
</div>
</div>
</div>
)}
</CardContent>
</Card>
);
}