From 5306bb310da3a8c9c701c35e4da6d5452ac37515 Mon Sep 17 00:00:00 2001 From: Henry Date: Fri, 27 Sep 2024 15:42:02 +0100 Subject: [PATCH] feat: clear all filters --- src/utils/helpers.ts | 8 ++++++ src/views/board/components/Filters.tsx | 34 ++++++++++++++++++++++++-- src/views/board/index.tsx | 8 +----- 3 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 src/utils/helpers.ts diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts new file mode 100644 index 00000000..f849c4cc --- /dev/null +++ b/src/utils/helpers.ts @@ -0,0 +1,8 @@ +export const formatToArray = ( + value: string | string[] | undefined, +): string[] => { + if (Array.isArray(value)) { + return value.filter((item) => item !== undefined); + } + return value ? [value] : []; +}; diff --git a/src/views/board/components/Filters.tsx b/src/views/board/components/Filters.tsx index 5d12d4d6..4eb98691 100644 --- a/src/views/board/components/Filters.tsx +++ b/src/views/board/components/Filters.tsx @@ -1,9 +1,13 @@ import { IoFilterOutline } from "react-icons/io5"; -import { HiOutlineUserCircle, HiOutlineTag } from "react-icons/hi2"; +import { + HiOutlineUserCircle, + HiOutlineTag, + HiMiniXMark, +} from "react-icons/hi2"; import { useRouter } from "next/router"; import Button from "~/components/Button"; import CheckboxDropdown from "~/components/CheckboxDropdown"; - +import { formatToArray } from "~/utils/helpers"; import { useBoard } from "~/providers/board"; const LabelIcon = ({ colourCode }: { colourCode: string | null }) => ( @@ -32,6 +36,16 @@ const Filters = () => { const { boardData } = useBoard(); const router = useRouter(); + const clearFilters = (e: React.MouseEvent) => { + e.preventDefault(); + e.stopPropagation(); + + router.push({ + pathname: router.pathname, + query: { ...router.query, members: [], labels: [] }, + }); + }; + const formattedMembers = boardData?.workspace?.members?.map((member) => ({ key: member.publicId, @@ -87,6 +101,11 @@ const Filters = () => { } }; + const numOfFilters = [ + ...formatToArray(router.query.members), + ...formatToArray(router.query.labels), + ].length; + return (
{ > + )}
diff --git a/src/views/board/index.tsx b/src/views/board/index.tsx index af79ac10..96e685c2 100644 --- a/src/views/board/index.tsx +++ b/src/views/board/index.tsx @@ -12,6 +12,7 @@ import { import { useForm } from "react-hook-form"; import { api } from "~/utils/api"; +import { formatToArray } from "~/utils/helpers"; import { useBoard } from "~/providers/board"; import { useModal } from "~/providers/modal"; @@ -31,13 +32,6 @@ import { type UpdateBoardInput } from "~/types/router.types"; type PublicListId = string; -const formatToArray = (value: string | string[] | undefined): string[] => { - if (Array.isArray(value)) { - return value.filter((item) => item !== undefined); - } - return value ? [value] : []; -}; - export default function BoardPage() { const params = useParams(); const router = useRouter();