From eae43317b8ebdcb8ac1730d77013df75e7214b84 Mon Sep 17 00:00:00 2001 From: Hossein Rahmani Date: Tue, 23 Jan 2024 20:00:30 +0330 Subject: [PATCH] Pagination added --- components/NavbarPanel.tsx | 43 ++++++++------ components/Pagination.tsx | 67 ++++++++++++++++++++++ components/Search.tsx | 102 ++++++--------------------------- components/TabController.tsx | 83 +++++++++++++++++++++------ components/TableController.tsx | 11 +++- components/ui/skeleton.tsx | 15 +++++ components/ui/tabs.tsx | 2 +- package-lock.json | 12 ++++ package.json | 1 + utils/authenticateApi.ts | 27 +++++++-- utils/inspectorApi.ts | 8 ++- 11 files changed, 238 insertions(+), 133 deletions(-) create mode 100644 components/Pagination.tsx create mode 100644 components/ui/skeleton.tsx diff --git a/components/NavbarPanel.tsx b/components/NavbarPanel.tsx index fcefd5d..3eb2f3b 100644 --- a/components/NavbarPanel.tsx +++ b/components/NavbarPanel.tsx @@ -5,16 +5,16 @@ import { AdminMenuItems, ControllerMenuItems } from "@/constants"; import { usePathname, useRouter } from "next/navigation"; import { Squash as Hamburger } from "hamburger-react"; import { AnimatePresence, motion } from "framer-motion"; -import { getProfile } from "@/utils/authenticateApi"; -import { useQuery } from "@tanstack/react-query"; +import { getProfile, revoke } from "@/utils/authenticateApi"; +import { useMutation, useQuery } from "@tanstack/react-query"; import { DoorOpen, LogOut } from "lucide-react"; -import { deleteCookie } from "cookies-next"; +import { deleteCookie, getCookie } from "cookies-next"; export default function NavbarPanel() { const [isOpen, setOpen] = useState(false); const ref = useRef(null); const router = useRouter(); const pathname = usePathname(); - const [activeLink, setActiveLink] = useState(''); + const [activeLink, setActiveLink] = useState(""); const [role, setRole] = useState(""); useEffect(() => { @@ -27,23 +27,36 @@ export default function NavbarPanel() { setActiveLink(pathname); }, [pathname]); - const { data, isLoading, refetch } = useQuery({ queryKey: ["Profile"], queryFn: () => getProfile(), refetchOnWindowFocus: false, }); + const revokeMutation = useMutation({ + mutationFn: revoke, + onSuccess: (res) => {}, + onError: (err: any | string) => {}, + }); + const menus = { Admin: AdminMenuItems, Inspector: ControllerMenuItems, }; const selectedMenu = menus[role as keyof typeof menus]; + const logout = () => { + revokeMutation.mutate({ + refreshToken: getCookie("Sootzani_Refresh_Token"), + }); + deleteCookie("Sootzani_Token"); + localStorage.removeItem("privateKey"); + localStorage.removeItem("privatePassword"); + router.push("/"); + }; + return ( -
+
{selectedMenu.map( (item: { id: number; name: string; href: string }) => { - const isActive = activeLink === item.href; + const isActive = activeLink === item.href; return (

{item.name}

@@ -135,15 +148,7 @@ export default function NavbarPanel() { )}
- { - deleteCookie("Sootzani_Token"); - localStorage.removeItem("privateKey"); - localStorage.removeItem("privatePassword"); - router.push("/"); - }} - className="text-white cursor-pointer " - /> +
diff --git a/components/Pagination.tsx b/components/Pagination.tsx new file mode 100644 index 0000000..5c884b2 --- /dev/null +++ b/components/Pagination.tsx @@ -0,0 +1,67 @@ +"use client"; +import React, { useEffect, useState } from "react"; +import ReactPaginate from "react-paginate"; +import { MdKeyboardArrowRight, MdKeyboardArrowLeft } from "react-icons/md"; +import { twMerge } from "tailwind-merge"; +// Example items, to simulate fetching from another resources. +const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]; + +export default function Pagination({ + itemsPerPage, + Page, + countPage, + length, +}: { + itemsPerPage?: any; + Page?: any; + countPage?: any; + length?: any; +}) { + // Here we use item offsets; we could also use page offsets + // following the API or data you're working with. + const [itemOffset, setItemOffset] = useState(0); + + // Simulate fetching items from another resources. + // (This could be items from props; or items loaded in a local state + // from an API endpoint with useEffect and useState) + const endOffset = itemOffset + itemsPerPage; + // console.log(`Loading items from ${itemOffset} to ${endOffset}`); + const currentItems = items.slice(itemOffset, endOffset); + const pageCount = Math.ceil(items.length / itemsPerPage); + + // Invoke when user click to request another page. + const handlePageClick = (event: any) => { + const newOffset = (event.selected * itemsPerPage) % items.length; + console.log( + `User requested page number ${event.selected}, which is offset ${newOffset}` + ); + setItemOffset(newOffset); + // console.log(event.selected); + console.log(event); + Page(event.selected); + }; + console.log(countPage); + // console.log(pageCount, "1111"); + return ( + <> + {/* */} + console.log(e)} + nextLabel={} + onPageChange={handlePageClick} + pageRangeDisplayed={3} + pageCount={Math.ceil(countPage / 5)} + previousLabel={} + pageClassName=" w-10 h-10 grid place-content-center rounded-lg !text-blue shadow-md" + className={twMerge( + " flex flex-wrap gap-3 text-sm md:text-base w-full bottom-0 rounded-md p-1 bg-white/30 backdrop-blur-lg justify-center text-blue mt-5" + )} + activeClassName=" bg-blue text-blue shadow-md shadow-main" + activeLinkClassName=" text-white w-full p-1 h-full" + previousLinkClassName=" w-10 h-10 grid place-content-center rounded-lg text-sm shadow-md" + nextLinkClassName=" w-10 h-10 grid place-content-center rounded-lg text-sm shadow-md" + /> + + ); +} diff --git a/components/Search.tsx b/components/Search.tsx index ec688c7..b05bc45 100644 --- a/components/Search.tsx +++ b/components/Search.tsx @@ -33,100 +33,32 @@ export default function Search({ // const isMobile = useMediaQuery({ query: "(max-width: 768px)" }); // }, []); return ( -
- {mode === "controller" || mode === "admin" ? ( -
- + changeValue(e.target.value)} - placeholder="جستجو..." - required - /> - {/* */} - -
- ) : ( - <> -
- - -
-
- - - - - - - - -

- فرم جستجو -

- - - -
- - - - - - -
-
-
-
- - )} -
+ +
); } diff --git a/components/TabController.tsx b/components/TabController.tsx index 17ab257..79ca894 100644 --- a/components/TabController.tsx +++ b/components/TabController.tsx @@ -22,11 +22,17 @@ import { useRouter } from "next/navigation"; import { Decrypt } from "@/lib/utils"; import useData from "@/store/useData"; import { toast } from "sonner"; +import Pagination from "./Pagination"; +import { Skeleton } from "./ui/skeleton"; + export default function TabController() { const saveData = useData((state) => state.saveData); const [selectedFile, setSelectedFile] = useState(null); const [search, setSearch] = useState(null); + const [page, setPage] = useState(1); + const [totalCount, setTotalCount] = useState(0); const router = useRouter(); + const handleFileChange = (event: any) => { const file = event.target.files[0]; const reader = new FileReader(); @@ -38,6 +44,7 @@ export default function TabController() { reader.readAsText(file); }; + const checkKey = (item: any) => { const dcKey = Decrypt(item.cipherKeyInspector, selectedFile); if (dcKey != false && dcKey != null) { @@ -52,7 +59,7 @@ export default function TabController() { }); } }; - console.log(search); + const [psblState, setPsblState] = useState(0); const [selectedTab, setSelectedTab] = useState("0"); @@ -70,27 +77,36 @@ export default function TabController() { // setSelectedTab(initialSelectedTab); const { data, isLoading, refetch } = useQuery({ - queryKey: ["ComplaintsList", psblState, dataState], + queryKey: ["ComplaintsList", psblState, dataState, page], queryFn: () => - getComplaintsList({ states: psblState, TrackingNumber: search }), + getComplaintsList({ + states: psblState, + TrackingNumber: search, + PageNumber: page, + }), refetchOnWindowFocus: false, }); const handelSearch = (event: any) => { refetch(); }; - // useEffect(() => { - // // Handle the data after it has been updated - // }, [data]); + useEffect(() => { + if (data) { + const headersData = JSON.parse(data.headers["x-pagination"]); + setTotalCount(headersData.TotalCount); + } + }, [data]); + const isZero = totalCount === 0; + console.log(totalCount); return (
-

+

لیست شکایت ها

{ // setPsblState(value); // setSelectedTab(value); @@ -99,10 +115,10 @@ export default function TabController() { className="" dir="rtl" > -
+
{/* Tab Title */} - -

+ +

لیست شکایت ها |

@@ -111,16 +127,19 @@ export default function TabController() { return ( { setPsblState(item.value); - setSelectedTab(item.value); + setSelectedTab(String(item.value)); refetch(); + setPage(0); }} > - {item.title} +
+ {item.title} ({item.count}) +
); })} @@ -138,7 +157,37 @@ export default function TabController() { {/* Tab Content */} - checkKey(item)} data={data} /> + {data ? ( + checkKey(item)} + data={data?.data} + /> + ) : ( +
+ + + + +
+ )} + + {!isZero ? ( + setPage(num + 1)} + /> + ) : ( + <> + {" "} + {data?.data.length == 0 && ( +

+ درخواستی وجود ندارد +

+ )} + + )}
diff --git a/components/TableController.tsx b/components/TableController.tsx index 3e89625..ec656fc 100644 --- a/components/TableController.tsx +++ b/components/TableController.tsx @@ -30,6 +30,7 @@ import { toast } from "sonner"; import { ScrollArea } from "./ui/scroll-area"; import secureLocalStorage from "react-secure-storage"; import { Checkbox } from "./ui/checkbox"; +import { Skeleton } from "./ui/skeleton"; interface Complaint { id: string; trackingNumber: string; @@ -110,7 +111,7 @@ export default function TableController({ دستگاه مربوطه - + {data?.map((item: Complaint, index: number) => { return ( {/* ); })} + {data.length != 0 && + [...Array(5 - data.length)].map(() => { + return ; + })} @@ -174,7 +179,7 @@ export default function TableController({ checked={check} onCheckedChange={setCheck} id="terms" - className=" ml-2" + className="ml-2 " />