From d9ec74b14948ec79da997e6d622c08adec1ce95c Mon Sep 17 00:00:00 2001 From: Pablo Lara Date: Mon, 30 Sep 2024 06:21:56 +0200 Subject: [PATCH] chore: WIP --- .../providers/table/data-table-pagination.tsx | 5 +- .../ui/custom/custom-dropdown-filter.tsx | 112 ++++++++++++++++++ components/ui/custom/index.ts | 1 + .../users/table/DataTablePagination.tsx | 5 +- 4 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 components/ui/custom/custom-dropdown-filter.tsx diff --git a/components/providers/table/data-table-pagination.tsx b/components/providers/table/data-table-pagination.tsx index 0e195f0507..4c4278682f 100644 --- a/components/providers/table/data-table-pagination.tsx +++ b/components/providers/table/data-table-pagination.tsx @@ -9,7 +9,7 @@ import { import Link from "next/link"; import { usePathname, useSearchParams } from "next/navigation"; -import { extractPaginationInfo } from "@/lib"; +import { getPaginationInfo } from "@/lib"; import { MetaDataProps } from "@/types"; interface DataTablePaginationProps { @@ -22,8 +22,7 @@ export function DataTablePagination({ metadata }: DataTablePaginationProps) { const pathname = usePathname(); const searchParams = useSearchParams(); - const { currentPage, totalPages, totalEntries } = - extractPaginationInfo(metadata); + const { currentPage, totalPages, totalEntries } = getPaginationInfo(metadata); const createPageUrl = (pageNumber: number | string) => { const params = new URLSearchParams(searchParams); diff --git a/components/ui/custom/custom-dropdown-filter.tsx b/components/ui/custom/custom-dropdown-filter.tsx new file mode 100644 index 0000000000..e901bd2725 --- /dev/null +++ b/components/ui/custom/custom-dropdown-filter.tsx @@ -0,0 +1,112 @@ +"use client"; + +import { + Button, + Checkbox, + CheckboxGroup, + Popover, + PopoverContent, + PopoverTrigger, +} from "@nextui-org/react"; +import React, { useCallback, useState } from "react"; + +import { PlusCircleIcon } from "@/components/icons"; + +interface FilterOption { + key: string; + labelCheckboxGroup: string; + values: string[]; +} + +interface CustomDropdownFilterProps { + filter?: FilterOption; +} + +export const CustomDropdownFilter: React.FC = ({ + filter, +}) => { + // Early return if filter is undefined + if (!filter) { + return null; + } + + const [groupSelected, setGroupSelected] = useState(new Set()); + + const allFilterKeys = filter.values || []; + + const onSelectionChange = useCallback( + (keys: string[]) => { + const newSelection = new Set(keys); + + if ( + newSelection.size === allFilterKeys.length && + !newSelection.has("all") + ) { + setGroupSelected(new Set(["all", ...allFilterKeys])); + } else if (groupSelected.has("all")) { + newSelection.delete("all"); + const remainingValues = allFilterKeys.filter((key) => + newSelection.has(key), + ); + setGroupSelected(new Set(remainingValues)); + } else { + setGroupSelected(newSelection); + } + }, + [allFilterKeys, groupSelected], + ); + + const handleSelectAllClick = useCallback(() => { + if (groupSelected.has("all")) { + setGroupSelected(new Set()); + } else { + setGroupSelected(new Set(["all", ...allFilterKeys])); + } + }, [groupSelected, allFilterKeys]); + + return ( +
+ + + + + +
+ + + Select All + + {allFilterKeys.map((value) => ( + + {value} + + ))} + +
+
+
+

+ Selected:{" "} + {Array.from(groupSelected) + .filter((item) => item !== "all") + .join(", ")} +

+
+ ); +}; diff --git a/components/ui/custom/index.ts b/components/ui/custom/index.ts index 4c1021bc9e..dc543cb6ca 100644 --- a/components/ui/custom/index.ts +++ b/components/ui/custom/index.ts @@ -1,3 +1,4 @@ +export * from "./custom-dropdown-filter"; export * from "./CustomAlertModal"; export * from "./CustomBox"; export * from "./CustomButton"; diff --git a/components/users/table/DataTablePagination.tsx b/components/users/table/DataTablePagination.tsx index 0e195f0507..4c4278682f 100644 --- a/components/users/table/DataTablePagination.tsx +++ b/components/users/table/DataTablePagination.tsx @@ -9,7 +9,7 @@ import { import Link from "next/link"; import { usePathname, useSearchParams } from "next/navigation"; -import { extractPaginationInfo } from "@/lib"; +import { getPaginationInfo } from "@/lib"; import { MetaDataProps } from "@/types"; interface DataTablePaginationProps { @@ -22,8 +22,7 @@ export function DataTablePagination({ metadata }: DataTablePaginationProps) { const pathname = usePathname(); const searchParams = useSearchParams(); - const { currentPage, totalPages, totalEntries } = - extractPaginationInfo(metadata); + const { currentPage, totalPages, totalEntries } = getPaginationInfo(metadata); const createPageUrl = (pageNumber: number | string) => { const params = new URLSearchParams(searchParams);