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/ProfileDropDown.tsx b/components/ProfileDropDown.tsx index a7daae3..db91834 100644 --- a/components/ProfileDropDown.tsx +++ b/components/ProfileDropDown.tsx @@ -48,7 +48,6 @@ export default function ProfileDropDown() { } = useQuery({ queryKey: ["Profile"], queryFn: () => getProfile(), - refetchOnWindowFocus: false, }); const revokeMutation = useMutation({ diff --git a/components/Search.tsx b/components/Search.tsx index 7a80c7b..53887eb 100644 --- a/components/Search.tsx +++ b/components/Search.tsx @@ -18,14 +18,24 @@ import { } from "@/components/ui/alert-dialog"; import { IoMdClose } from "react-icons/io"; import { useMediaQuery } from "react-responsive"; -export default function Search({ mode }: { mode: string }) { +export default function Search({ + mode, + value, + action, + changeValue, +}: { + mode?: string; + value?: any; + action?: (any: any) => any; + changeValue?: (any: any) => any; +}) { // useEffect(() => { // const isMobile = useMediaQuery({ query: "(max-width: 768px)" }); // }, []); return (
{mode === "controller" || mode === "admin" ? ( -
+
{ + if (changeValue) { + changeValue(e.target.value); + } + }} placeholder="جستجو..." required /> @@ -46,7 +62,7 @@ export default function Search({ mode }: { mode: string }) { > */} -
diff --git a/components/TabController.tsx b/components/TabController.tsx index bc6e9d5..ce08ffe 100644 --- a/components/TabController.tsx +++ b/components/TabController.tsx @@ -1,5 +1,5 @@ "use client"; -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import TableController from "./TableController"; import Search from "./Search"; @@ -22,167 +22,50 @@ import { Decrypt } from "@/lib/utils"; import useData from "@/store/useData"; import { toast } from "sonner"; import { getComplaints } from "@/utils/citizenApi"; +import Pagination from "./Pagination"; export default function TabController() { const saveData = useData((state) => state.saveData); const [selectedFile, setSelectedFile] = useState(null); + const [page, setPage] = useState(1); + const [totalCount, setTotalCount] = useState(0); const router = useRouter(); - const { data, isLoading } = useQuery({ - queryKey: ["CitizenList"], - queryFn: () => getComplaints(), + const [search, setSearch] = useState(null); + + const { data, isLoading, refetch } = useQuery({ + queryKey: ["CitizenList", page], + queryFn: () => + getComplaints({ + TrackingNumber: search, + PageNumber: page, + }), refetchOnWindowFocus: false, }); - const checkKey = (item: any) => { - const dcKey = Decrypt(item.cipherKeyInspector, selectedFile); - if (dcKey != false && dcKey != null) { - saveData({ - trackingNumber: item.trackingNumber, - password: Decrypt(item.cipherKeyInspector, selectedFile), - }); - router.push(`/UserPanel/ControllerDashboard/Complaints/${item.id}`); - } else { - toast("کلید شما معتبر نمی باشد.", { - className: "!bg-red-500 !text-white", - }); + console.log(data); + useEffect(() => { + if (data) { + const headersData = JSON.parse(data.headers["x-pagination"]); + setTotalCount(headersData.TotalCount); } + }, [data]); + + const handelSearch = (event: any) => { + refetch(); }; - console.log(data); - const newComplaints = data?.filter((item: any) => item.status == 0); - const inProgressComplaints = data?.filter((item: any) => item.status == 1); - const waitingForCitizenResponseComplaints = data?.filter( - (item: any) => item.status == 2 - ); - const citizenRepliedComplaints = data?.filter( - (item: any) => item.status == 3 - ); - const finishedComplaints = data?.filter((item: any) => item.status == 4); - console.log(newComplaints); return (
- - {/* - - */} - -
- {/* Tab Title */} - -

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

- - - ثبت شده - - - در انتظار پاسخ - - - در حال بررسی - - - پاسخ داده شده - - - پایان یافته - -
- - {/* Search Box */} - -
- - {/* Tab Content */} - - checkKey(item)} - data={newComplaints} - /> - - - - checkKey(item)} - data={waitingForCitizenResponseComplaints} - /> - - - - checkKey(item)} - data={inProgressComplaints} - /> - - - - checkKey(item)} - data={citizenRepliedComplaints} - /> - - - - checkKey(item)} - data={finishedComplaints} - /> - -
- - - بارگذاری کلید خصوصی - - لطفا کلید خصوصی خود را آپلود نمایید. - - -
- {/* */} - {/* */} -
- - - - - -
-
+ setSearch(data)} + mode="controller" + action={handelSearch} + /> + + setPage(num + 1)} + />
); } diff --git a/components/TableController.tsx b/components/TableController.tsx index a96b524..630efd9 100644 --- a/components/TableController.tsx +++ b/components/TableController.tsx @@ -41,19 +41,17 @@ interface Complaint { title: string; description: string; }; + statusWithDescription: { + value: number; + description: string; + }; status: number; registeredAt: string; lastChanged: string; lastActor: number; cipherKeyInspector: string; } -export default function TableController({ - data, - checkKey, -}: { - data: Complaint[]; - checkKey: (item: any) => any; -}) { +export default function TableController({ data }: { data: Complaint[] }) { console.log(data); const saveData = useData((state) => state.saveData); const [selectedFile, setSelectedFile] = useState(null); @@ -71,17 +69,7 @@ export default function TableController({ reader.readAsText(file); }; - // const checkKey = (item: any) => { - // const dcKey = Decrypt(item.cipherKeyInspector, selectedFile); - // if (dcKey != false && dcKey != null) { - // saveData({ - // trackingNumber: item.trackingNumber, - // password: Decrypt(item.cipherKeyInspector, selectedFile), - // }); - // router.push(`/UserPanel/ControllerDashboard/Complaints/${item.id}`); - // } else { - // } - // }; + return (
@@ -92,6 +80,7 @@ export default function TableController({ تاریخکد پیگیریموضوع + وضعیتدستگاه مربوطه @@ -123,12 +112,18 @@ export default function TableController({ {item?.title ? item.title : "-"} - + + {item?.statusWithDescription + ? item.statusWithDescription.description + : "-"} + + {item?.complaintOrganization ? item?.complaintOrganization.title : "-"} {/* */} + ); })} diff --git a/package-lock.json b/package-lock.json index b6b95f6..037a371 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,6 +50,7 @@ "react-leaflet": "^4.2.1", "react-loader-spinner": "^6.1.6", "react-otp-input": "^3.1.1", + "react-paginate": "^8.2.0", "react-responsive": "^9.0.2", "react-spinners": "^0.13.8", "react-to-print": "^2.14.15", @@ -8159,6 +8160,17 @@ "react-dom": ">=16.8.6 || ^17.0.0 || ^18.0.0" } }, + "node_modules/react-paginate": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/react-paginate/-/react-paginate-8.2.0.tgz", + "integrity": "sha512-sJCz1PW+9PNIjUSn919nlcRVuleN2YPoFBOvL+6TPgrH/3lwphqiSOgdrLafLdyLDxsgK+oSgviqacF4hxsDIw==", + "dependencies": { + "prop-types": "^15" + }, + "peerDependencies": { + "react": "^16 || ^17 || ^18" + } + }, "node_modules/react-remove-scroll": { "version": "2.5.5", "resolved": "https://registry.npmjs.org/react-remove-scroll/-/react-remove-scroll-2.5.5.tgz", diff --git a/package.json b/package.json index 1d74faf..cf6e7cc 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "react-leaflet": "^4.2.1", "react-loader-spinner": "^6.1.6", "react-otp-input": "^3.1.1", + "react-paginate": "^8.2.0", "react-responsive": "^9.0.2", "react-spinners": "^0.13.8", "react-to-print": "^2.14.15", diff --git a/tailwind.config.js b/tailwind.config.js index dbc4629..853dfed 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -39,7 +39,6 @@ module.exports = { blue50: "#335F9380", yellow: "#F7A600", brown: "#D28249", - gray: "#BFBFBF", border: "hsl(var(--border))", input: "hsl(var(--input))", ring: "hsl(var(--ring))", diff --git a/utils/citizenApi.ts b/utils/citizenApi.ts index 29c288a..2786237 100644 --- a/utils/citizenApi.ts +++ b/utils/citizenApi.ts @@ -44,7 +44,19 @@ export async function postComplaintsDetails(payload: FormData) { return data.data; } -export async function getComplaints() { - const data = await axios.get("api/Citizen/List"); - return data.data; +export async function getComplaints({ + states = 0, + TrackingNumber, + PageNumber = 1, + PageSize = 5, +}: { + states?: number; + TrackingNumber?: string; + PageNumber?: number; + PageSize?: number; +}) { + const data = await axios.get("api/Citizen/List", { + params: { TrackingNumber: TrackingNumber, PageNumber, PageSize }, + }); + return data; }