From a340f1f24af1e61c993edfce54858d816584e96e Mon Sep 17 00:00:00 2001 From: Tony Vi Date: Thu, 20 Apr 2023 11:27:31 +0300 Subject: [PATCH] fix: remove useState and useEffect from filters dropdowns --- graphql/schema.graphql | 1 - src/components/EstimateFilterDropdown.tsx | 16 +++++----------- src/components/PriorityFilterDropdown.tsx | 14 +++++--------- src/components/ProjectFilterDropdown.tsx | 16 +++++----------- src/components/StateFilterDropdown.tsx | 14 +++++--------- src/components/TagsFilterDropdown.tsx | 16 +++++----------- src/components/UserFilterDropdown.tsx | 12 +++++------- 7 files changed, 30 insertions(+), 59 deletions(-) diff --git a/graphql/schema.graphql b/graphql/schema.graphql index e6b508573..3cba41b91 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -324,7 +324,6 @@ type Query { projects: [Project] settings: Settings tagCompletion(query: String!, sortBy: SortOrder): [Tag] - userFilters: [Filter] userGoals(data: UserGoalsInput!): UserGoalsOutput users(sortBy: SortOrder): [User] } diff --git a/src/components/EstimateFilterDropdown.tsx b/src/components/EstimateFilterDropdown.tsx index 8a782ee82..eef70c202 100644 --- a/src/components/EstimateFilterDropdown.tsx +++ b/src/components/EstimateFilterDropdown.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useEffect, useState } from 'react'; +import React, { useCallback } from 'react'; import { Dropdown, MenuItem } from '@taskany/bricks'; import { Estimate } from '../../graphql/@generated/genql'; @@ -20,23 +20,17 @@ export const EstimateFilterDropdown: React.FC = Rea HTMLDivElement, EstimateFilterDropdownProps >(({ text, estimates, value, disabled, onChange }, ref) => { - const [selected, setSelected] = useState(new Set(value)); - - useEffect(() => { - setSelected(new Set(value)); - }, [value]); - const onEstimateClick = useCallback( (e: Estimate) => { + const selected = new Set(value); selected.has(estimateToString(e)) ? selected.delete(estimateToString(e)) : selected.add(estimateToString(e)); const newSelected = new Set(selected); - setSelected(newSelected); onChange?.(Array.from(newSelected)); }, - [onChange, selected], + [onChange, value], ); return ( @@ -50,7 +44,7 @@ export const EstimateFilterDropdown: React.FC = Rea renderTrigger={(props) => ( @@ -61,7 +55,7 @@ export const EstimateFilterDropdown: React.FC = Rea {estimateToString(props.item)} diff --git a/src/components/PriorityFilterDropdown.tsx b/src/components/PriorityFilterDropdown.tsx index 889328e0f..156ffabae 100644 --- a/src/components/PriorityFilterDropdown.tsx +++ b/src/components/PriorityFilterDropdown.tsx @@ -1,7 +1,6 @@ -import React, { useCallback, useState } from 'react'; +import React, { useCallback } from 'react'; import { Dropdown } from '@taskany/bricks'; -import { usePageContext } from '../hooks/usePageContext'; import { Priority, priorityColorsMap } from '../types/priority'; import { trPriority } from '../i18n/priority'; @@ -19,18 +18,15 @@ interface PriorityFilterDropdownProps { export const PriorityFilterDropdown = React.forwardRef( ({ text, priority, value, disabled, onChange }, ref) => { - const { themeId } = usePageContext(); - const [selected, setSelected] = useState>(new Set(value)); - const onPriorityClick = useCallback( (p: Priority) => { + const selected = new Set(value); selected.has(p) ? selected.delete(p) : selected.add(p); const newSelected = new Set(selected); - setSelected(newSelected); onChange?.(Array.from(newSelected)); }, - [onChange, selected], + [onChange, value], ); return ( @@ -44,7 +40,7 @@ export const PriorityFilterDropdown = React.forwardRef ( @@ -55,7 +51,7 @@ export const PriorityFilterDropdown = React.forwardRef {trPriority(props.item as Priority)} diff --git a/src/components/ProjectFilterDropdown.tsx b/src/components/ProjectFilterDropdown.tsx index 6c1c7477d..f024eaded 100644 --- a/src/components/ProjectFilterDropdown.tsx +++ b/src/components/ProjectFilterDropdown.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useEffect, useState } from 'react'; +import React, { useCallback } from 'react'; import { Dropdown, MenuItem } from '@taskany/bricks'; import { Project } from '../../graphql/@generated/genql'; @@ -18,21 +18,15 @@ export const ProjectFilterDropdown: React.FC = React HTMLDivElement, ProjectFilterDropdownProps >(({ text, projects, value, disabled, onChange }, ref) => { - const [selected, setSelected] = useState(new Set(value)); - - useEffect(() => { - setSelected(new Set(value)); - }, [value]); - const onProjectClick = useCallback( (p: Project) => { + const selected = new Set(value); selected.has(p.id) ? selected.delete(p.id) : selected.add(p.id); const newSelected = new Set(selected); - setSelected(newSelected); onChange?.(Array.from(newSelected)); }, - [onChange, selected], + [onChange, value], ); return ( @@ -46,7 +40,7 @@ export const ProjectFilterDropdown: React.FC = React renderTrigger={(props) => ( @@ -54,7 +48,7 @@ export const ProjectFilterDropdown: React.FC = React )} renderItem={(props) => ( - + {props.item.title} )} diff --git a/src/components/StateFilterDropdown.tsx b/src/components/StateFilterDropdown.tsx index 42bd9e522..3049958d2 100644 --- a/src/components/StateFilterDropdown.tsx +++ b/src/components/StateFilterDropdown.tsx @@ -1,8 +1,7 @@ -import React, { useCallback, useState } from 'react'; +import React, { useCallback } from 'react'; import { Dropdown } from '@taskany/bricks'; import { State } from '../../graphql/@generated/genql'; -import { usePageContext } from '../hooks/usePageContext'; import { FiltersMenuItem } from './FiltersMenuItem'; import { ColorizedMenuItem } from './ColorizedMenuItem'; @@ -18,18 +17,15 @@ interface StateFilterDropdownProps { export const StateFilterDropdown = React.forwardRef( ({ text, states, value, disabled, onChange }, ref) => { - const { themeId } = usePageContext(); - const [selected, setSelected] = useState>(new Set(value)); - const onStateClick = useCallback( (s: State) => { + const selected = new Set(value); selected.has(s.id) ? selected.delete(s.id) : selected.add(s.id); const newSelected = new Set(selected); - setSelected(newSelected); onChange?.(Array.from(newSelected)); }, - [onChange, selected], + [onChange, value], ); return ( @@ -43,7 +39,7 @@ export const StateFilterDropdown = React.forwardRef ( @@ -54,7 +50,7 @@ export const StateFilterDropdown = React.forwardRef {props.item.title} diff --git a/src/components/TagsFilterDropdown.tsx b/src/components/TagsFilterDropdown.tsx index 116727bce..3ea37bdf1 100644 --- a/src/components/TagsFilterDropdown.tsx +++ b/src/components/TagsFilterDropdown.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useEffect, useState } from 'react'; +import React, { useCallback } from 'react'; import { Dropdown, MenuItem } from '@taskany/bricks'; import { Tag as TagModel } from '../../graphql/@generated/genql'; @@ -18,21 +18,15 @@ export const TagsFilterDropdown: React.FC = React.forwa HTMLDivElement, TagsFilterDropdownProps >(({ text, tags, value, disabled, onChange }, ref) => { - const [selected, setSelected] = useState(new Set(value)); - - useEffect(() => { - setSelected(new Set(value)); - }, [value]); - const onTagClick = useCallback( (t: TagModel) => { + const selected = new Set(value); selected.has(t.id) ? selected.delete(t.id) : selected.add(t.id); const newSelected = new Set(selected); - setSelected(newSelected); onChange?.(Array.from(newSelected)); }, - [onChange, selected], + [onChange, value], ); return ( @@ -46,7 +40,7 @@ export const TagsFilterDropdown: React.FC = React.forwa renderTrigger={(props) => ( @@ -54,7 +48,7 @@ export const TagsFilterDropdown: React.FC = React.forwa )} renderItem={(props) => ( - + {props.item.title} )} diff --git a/src/components/UserFilterDropdown.tsx b/src/components/UserFilterDropdown.tsx index 858505df7..b14776a72 100644 --- a/src/components/UserFilterDropdown.tsx +++ b/src/components/UserFilterDropdown.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useState } from 'react'; +import React, { useCallback } from 'react'; import { Dropdown } from '@taskany/bricks'; import { Activity } from '../../graphql/@generated/genql'; @@ -17,17 +17,15 @@ interface UserFilterDropdownProps { export const UserFilterDropdown = React.forwardRef( ({ text, activity, value, disabled, onChange }, ref) => { - const [selected, setSelected] = useState>(new Set(value)); - const onUserClick = useCallback( (a: Activity) => { + const selected = new Set(value); selected.has(a.id) ? selected.delete(a.id) : selected.add(a.id); const newSelected = new Set(selected); - setSelected(newSelected); onChange?.(Array.from(newSelected)); }, - [onChange, selected], + [onChange, value], ); return ( @@ -41,7 +39,7 @@ export const UserFilterDropdown = React.forwardRef ( @@ -54,7 +52,7 @@ export const UserFilterDropdown = React.forwardRef )}