Update ChatTable.tsx

This commit is contained in:
NotII
2025-03-24 17:24:39 +00:00
parent 06984b53ef
commit 9f4e873d4a

View File

@@ -24,6 +24,13 @@ import {
ChevronLeft, ChevronLeft,
ChevronRight ChevronRight
} from "lucide-react"; } from "lucide-react";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { clientFetch } from "@/lib/client-utils"; import { clientFetch } from "@/lib/client-utils";
import { toast } from "sonner"; import { toast } from "sonner";
import { getCookie } from "@/lib/client-utils"; import { getCookie } from "@/lib/client-utils";
@@ -59,7 +66,7 @@ export default function ChatTable() {
const [currentPage, setCurrentPage] = useState(1); const [currentPage, setCurrentPage] = useState(1);
const [totalPages, setTotalPages] = useState(1); const [totalPages, setTotalPages] = useState(1);
const [totalChats, setTotalChats] = useState(0); const [totalChats, setTotalChats] = useState(0);
const ITEMS_PER_PAGE = 10; const [itemsPerPage, setItemsPerPage] = useState<number>(10);
// Initialize audio element for notifications // Initialize audio element for notifications
useEffect(() => { useEffect(() => {
@@ -103,7 +110,7 @@ export default function ChatTable() {
return { vendorId, authToken }; return { vendorId, authToken };
}; };
// Fetch chats when component mounts or page changes // Fetch chats when component mounts or page/limit changes
useEffect(() => { useEffect(() => {
fetchChats(); fetchChats();
@@ -113,7 +120,7 @@ export default function ChatTable() {
}, 30000); // Check every 30 seconds }, 30000); // Check every 30 seconds
return () => clearInterval(interval); return () => clearInterval(interval);
}, [currentPage]); }, [currentPage, itemsPerPage]);
// Fetch unread counts // Fetch unread counts
const fetchUnreadCounts = async () => { const fetchUnreadCounts = async () => {
@@ -146,7 +153,7 @@ export default function ChatTable() {
const { vendorId } = getVendorIdFromToken(); const { vendorId } = getVendorIdFromToken();
// Now fetch chats for this vendor using clientFetch with pagination // Now fetch chats for this vendor using clientFetch with pagination
const response = await clientFetch(`/chats/vendor/${vendorId}?page=${currentPage}&limit=${ITEMS_PER_PAGE}`); const response = await clientFetch(`/chats/vendor/${vendorId}?page=${currentPage}&limit=${itemsPerPage}`);
// Check if the response is the old format (array) or new paginated format // Check if the response is the old format (array) or new paginated format
if (Array.isArray(response)) { if (Array.isArray(response)) {
@@ -195,6 +202,13 @@ export default function ChatTable() {
} }
}; };
// Handle items per page change
const handleItemsPerPageChange = (value: string) => {
const newLimit = parseInt(value);
setItemsPerPage(newLimit);
setCurrentPage(1); // Reset to first page when changing limit
};
return ( return (
<div className="space-y-4"> <div className="space-y-4">
<div className="flex justify-between items-center"> <div className="flex justify-between items-center">
@@ -315,6 +329,24 @@ export default function ChatTable() {
<div className="text-sm text-muted-foreground"> <div className="text-sm text-muted-foreground">
Showing {chats.length} of {totalChats} chats Showing {chats.length} of {totalChats} chats
</div> </div>
<div className="flex items-center space-x-4">
<div className="flex items-center space-x-2">
<span className="text-sm text-muted-foreground">Rows per page:</span>
<Select
value={itemsPerPage.toString()}
onValueChange={handleItemsPerPageChange}
>
<SelectTrigger className="h-8 w-[70px]">
<SelectValue placeholder={itemsPerPage.toString()} />
</SelectTrigger>
<SelectContent>
<SelectItem value="5">5</SelectItem>
<SelectItem value="10">10</SelectItem>
<SelectItem value="20">20</SelectItem>
<SelectItem value="50">50</SelectItem>
</SelectContent>
</Select>
</div>
<div className="flex items-center space-x-2"> <div className="flex items-center space-x-2">
<Button <Button
variant="outline" variant="outline"
@@ -337,6 +369,7 @@ export default function ChatTable() {
</Button> </Button>
</div> </div>
</div> </div>
</div>
)} )}
</div> </div>
); );