From d18b8d807b03cb0236d8d8fa15fdb032943c06ca Mon Sep 17 00:00:00 2001 From: farhan Date: Wed, 4 Sep 2024 22:28:05 +0530 Subject: [PATCH] added pagination to marketplace table --- components/Marketplace/MarketplaceList.tsx | 52 ++++++++++++++++------ 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/components/Marketplace/MarketplaceList.tsx b/components/Marketplace/MarketplaceList.tsx index c3f5872..2f868b1 100644 --- a/components/Marketplace/MarketplaceList.tsx +++ b/components/Marketplace/MarketplaceList.tsx @@ -1,9 +1,9 @@ -import React, {useState, useEffect} from "react"; +import React, { useState } from "react"; import SortbyComponent from "../reusable/SortbyComponent"; -import {getKeyValue} from "@nextui-org/react"; -import {cardsSortData} from "@/constants/MarketplaceList"; -import {ArrowDownUpIcon, ArrowUpIcon} from "lucide-react"; -import {Dialog, DialogContent} from "@/components/ui/dialog"; +import { getKeyValue, Pagination } from "@nextui-org/react"; +import { cardsSortData } from "@/constants/MarketplaceList"; +import { ArrowDownUpIcon, ArrowUpIcon } from "lucide-react"; +import { Dialog, DialogContent } from "@/components/ui/dialog"; import NoteDetails from "./NoteDetails"; // Import the new component interface MarketplaceListProps { @@ -18,9 +18,11 @@ const MarketplaceList: React.FC = ({ const [sortConfig, setSortConfig] = useState<{ key: string; direction: string; - }>({key: "rank", direction: "descending"}); + }>({ key: "rank", direction: "descending" }); const [isModalOpen, setIsModalOpen] = useState(false); // new state const [selectedRow, setSelectedRow] = useState(null); // new state + const pageSize = 10; // max rows per page + const [currentPage, setCurrentPage] = useState(1); const parseValue = (value: string) => { if (value === undefined || value === "") return 0; @@ -65,16 +67,22 @@ const MarketplaceList: React.FC = ({ return 0; }); - return sortableRows; + const result: string[][] = []; + const pages = Math.ceil(sortableRows?.length / pageSize); + for (let i = 0; i < pages; i++) { + result.push(sortableRows?.slice(i * pageSize, (i + 1) * pageSize)); + } + return result; }, [cardsData, sortConfig]); const requestSort = (key: string) => { setSortConfig((oldValue) => { - return {key, direction: oldValue.direction}; + return { key, direction: oldValue.direction }; }); }; - const handleRowClick = (data: any) => { // new function + const handleRowClick = (data: any) => { + // new function setSelectedRow(data); setIsModalOpen(true); }; @@ -92,8 +100,8 @@ const MarketplaceList: React.FC = ({
setSortConfig((prevState) => ({ @@ -126,7 +134,7 @@ const MarketplaceList: React.FC = ({
Market
- {sortedRows.map((data: any) => ( + {sortedRows[currentPage - 1].map((data: any) => (
handleRowClick(data)} // updated to handle click @@ -219,10 +227,28 @@ const MarketplaceList: React.FC = ({
))}
+
+ +
{selectedRow && ( - {/* Use the new component */} + {" "} + {/* Use the new component */} )}