From c54b8b9a1589b3eff459a9ad18b586dec1cae4b5 Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal <65252264+aaryan610@users.noreply.github.com> Date: Thu, 27 Jul 2023 00:57:12 +0530 Subject: [PATCH] refactor: filters list component (#1687) * refactor: filters list component * fix: build error --- .../components/core/filters/filters-list.tsx | 275 +++++++----------- .../app/components/core/views/issues-view.tsx | 2 +- .../issues/my-issues/my-issues-view.tsx | 16 +- apps/app/contexts/issue-view.context.tsx | 1 + .../hooks/my-issues/use-my-issues-filter.tsx | 23 +- apps/app/types/issues.d.ts | 2 + apps/app/types/projects.d.ts | 4 +- apps/app/types/state.d.ts | 6 +- apps/app/types/workspace.d.ts | 6 +- 9 files changed, 139 insertions(+), 196 deletions(-) diff --git a/apps/app/components/core/filters/filters-list.tsx b/apps/app/components/core/filters/filters-list.tsx index cf9c9dc4b..12ae9f4c9 100644 --- a/apps/app/components/core/filters/filters-list.tsx +++ b/apps/app/components/core/filters/filters-list.tsx @@ -1,7 +1,5 @@ import React from "react"; -import { useRouter } from "next/router"; - // icons import { XMarkIcon } from "@heroicons/react/24/outline"; import { getPriorityIcon, getStateGroupIcon } from "components/icons"; @@ -12,11 +10,13 @@ import { replaceUnderscoreIfSnakeCase } from "helpers/string.helper"; // helpers import { renderShortDateWithYearFormat } from "helpers/date-time.helper"; // types -import { IIssueFilterOptions, IIssueLabels, IState, IUserLite } from "types"; +import { IIssueFilterOptions, IIssueLabels, IState, IUserLite, TStateGroups } from "types"; +// constants +import { STATE_GROUP_COLORS } from "constants/state"; type Props = { - filters: any; - setFilters: any; + filters: Partial; + setFilters: (updatedFilter: Partial) => void; clearAllFilters: (...args: any) => void; labels: IIssueLabels[] | undefined; members: IUserLite[] | undefined; @@ -31,9 +31,6 @@ export const FiltersList: React.FC = ({ members, states, }) => { - const router = useRouter(); - const { viewId } = router.query; - if (!filters) return <>; const nullFilters = Object.keys(filters).filter( @@ -42,24 +39,26 @@ export const FiltersList: React.FC = ({ return (
- {Object.keys(filters).map((key) => { - if (filters[key as keyof typeof filters] !== null) - return ( -
- - {key === "target_date" ? "Due Date" : replaceUnderscoreIfSnakeCase(key)}: - - {filters[key as keyof IIssueFilterOptions] === null || - (filters[key as keyof IIssueFilterOptions]?.length ?? 0) <= 0 ? ( - None - ) : Array.isArray(filters[key as keyof IIssueFilterOptions]) ? ( -
- {key === "state" ? ( -
- {filters.state?.map((stateId: any) => { + {Object.keys(filters).map((filterKey) => { + const key = filterKey as keyof typeof filters; + + if (filters[key] === null) return null; + + return ( +
+ + {key === "target_date" ? "Due Date" : replaceUnderscoreIfSnakeCase(key)}: + + {filters[key] === null || (filters[key]?.length ?? 0) <= 0 ? ( + None + ) : Array.isArray(filters[key]) ? ( +
+
+ {key === "state" + ? filters.state?.map((stateId: string) => { const state = states?.find((s) => s.id === stateId); return ( @@ -83,33 +82,46 @@ export const FiltersList: React.FC = ({ - setFilters( - { - state: filters.state?.filter((s: any) => s !== stateId), - }, - !Boolean(viewId) - ) + setFilters({ + state: filters.state?.filter((s: any) => s !== stateId), + }) } >

); - })} - -
- ) : key === "priority" ? ( -
- {filters.priority?.map((priority: any) => ( + }) + : key === "state_group" + ? filters.state_group?.map((stateGroup) => { + const group = stateGroup as TStateGroups; + + return ( +

+ {getStateGroupIcon(group, "16", "16")} + {group} + + setFilters({ + state_group: filters.state_group?.filter((g) => g !== group), + }) + } + > + + +

+ ); + }) + : key === "priority" + ? filters.priority?.map((priority: any) => (

= ({ - setFilters( - { - priority: filters.priority?.filter((p: any) => p !== priority), - }, - !Boolean(viewId) - ) + setFilters({ + priority: filters.priority?.filter((p: any) => p !== priority), + }) } >

- ))} - -
- ) : key === "assignees" ? ( -
- {filters.assignees?.map((memberId: string) => { + )) + : key === "assignees" + ? filters.assignees?.map((memberId: string) => { const member = members?.find((m) => m.id === memberId); return ( @@ -167,35 +164,18 @@ export const FiltersList: React.FC = ({ - setFilters( - { - assignees: filters.assignees?.filter( - (p: any) => p !== memberId - ), - }, - !Boolean(viewId) - ) + setFilters({ + assignees: filters.assignees?.filter((p: any) => p !== memberId), + }) } >
); - })} - -
- ) : key === "created_by" ? ( -
- {filters.created_by?.map((memberId: string) => { + }) + : key === "created_by" + ? filters.created_by?.map((memberId: string) => { const member = members?.find((m) => m.id === memberId); return ( @@ -208,35 +188,20 @@ export const FiltersList: React.FC = ({ - setFilters( - { - created_by: filters.created_by?.filter( - (p: any) => p !== memberId - ), - }, - !Boolean(viewId) - ) + setFilters({ + created_by: filters.created_by?.filter( + (p: any) => p !== memberId + ), + }) } >
); - })} - -
- ) : key === "labels" ? ( -
- {filters.labels?.map((labelId: string) => { + }) + : key === "labels" + ? filters.labels?.map((labelId: string) => { const label = labels?.find((l) => l.id === labelId); if (!label) return null; @@ -260,12 +225,9 @@ export const FiltersList: React.FC = ({ - setFilters( - { - labels: filters.labels?.filter((l: any) => l !== labelId), - }, - !Boolean(viewId) - ) + setFilters({ + labels: filters.labels?.filter((l: any) => l !== labelId), + }) } > = ({
); - })} - -
- ) : key === "target_date" ? ( -
- {filters.target_date?.map((date: string) => { - if (filters.target_date.length <= 0) return null; + }) + : key === "target_date" + ? filters.target_date?.map((date: string) => { + if (filters.target_date && filters.target_date.length <= 0) return null; const splitDate = date.split(";"); @@ -308,39 +258,17 @@ export const FiltersList: React.FC = ({ - setFilters( - { - target_date: filters.target_date?.filter( - (d: any) => d !== date - ), - }, - !Boolean(viewId) - ) + setFilters({ + target_date: filters.target_date?.filter((d: any) => d !== date), + }) } >
); - })} - -
- ) : ( - (filters[key as keyof IIssueFilterOptions] as any)?.join(", ") - )} -
- ) : ( -
- {filters[key as keyof typeof filters]} + }) + : (filters[key] as any)?.join(", ")}
- )} -
- ); + + ) : ( +
+ {filters[key as keyof typeof filters]} + +
+ )} + + ); })} {Object.keys(filters).length > 0 && nullFilters.length !== Object.keys(filters).length && (