From d8ef40ea73d731e91febee9512352721b1a484d2 Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 30 Aug 2023 15:29:17 +0300 Subject: [PATCH] fix(FiltersPanel): estimate as JSON at search URL --- src/components/EstimateFilter.tsx | 13 ++++++------ .../FiltersPanelApplied.tsx | 9 +++++++- src/utils/estimateToString.ts | 21 +++++++++++++++++-- trpc/queries/goals.ts | 12 +++++------ 4 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/components/EstimateFilter.tsx b/src/components/EstimateFilter.tsx index 9670ce66b..9375ca800 100644 --- a/src/components/EstimateFilter.tsx +++ b/src/components/EstimateFilter.tsx @@ -1,12 +1,7 @@ import { FiltersDropdown } from '@taskany/bricks'; import { FC, useMemo } from 'react'; -import { estimateToString } from '../utils/estimateToString'; - -type Estimate = { - q: string | null; - y: string; -}; +import { estimateToString, Estimate, encodeEstimateFilterValue } from '../utils/estimateToString'; export const EstimateFilter: FC<{ text: string; @@ -15,7 +10,11 @@ export const EstimateFilter: FC<{ onChange: (value: string[]) => void; }> = ({ text, value, estimates, onChange }) => { const items = useMemo( - () => estimates.map((estimate) => ({ id: estimateToString(estimate), data: estimateToString(estimate) })), + () => + estimates.map((estimate) => ({ + id: encodeEstimateFilterValue(estimate), + data: estimateToString(estimate), + })), [estimates], ); diff --git a/src/components/FiltersPanelApplied/FiltersPanelApplied.tsx b/src/components/FiltersPanelApplied/FiltersPanelApplied.tsx index f469ca7ff..2f07473b6 100644 --- a/src/components/FiltersPanelApplied/FiltersPanelApplied.tsx +++ b/src/components/FiltersPanelApplied/FiltersPanelApplied.tsx @@ -10,6 +10,7 @@ import { ProjectFilter } from '../ProjectFilter'; import { TagFilter } from '../TagFilter'; import { EstimateFilter } from '../EstimateFilter'; import { SortableProps, sortFilterTr } from '../SortFilter/SortFilter'; +import { decodeEstimateFilterValue, estimateToString } from '../../utils/estimateToString'; import { tr } from './FiltersPanelApplied.i18n'; @@ -98,7 +99,13 @@ export const FiltersPanelApplied: React.FC = ({ } if (queryState.estimate.length && estimates?.length) { - appliedMap[tr('Estimate')] = queryState.estimate.filter(Boolean); + appliedMap[tr('Estimate')] = queryState.estimate + .map((e) => { + const estimate = decodeEstimateFilterValue(e); + + return estimate ? estimateToString(estimate) : null; + }) + .filter(Boolean); } Object.entries(appliedMap).forEach(([k, v]) => { diff --git a/src/utils/estimateToString.ts b/src/utils/estimateToString.ts index 3e9e3eb9f..d1c1e40b5 100644 --- a/src/utils/estimateToString.ts +++ b/src/utils/estimateToString.ts @@ -1,8 +1,25 @@ -import { Estimate } from '@prisma/client'; +import { Estimate as EstimateType } from '@prisma/client'; -export const estimateToString = (estimate: { q: Estimate['q']; y: Estimate['y'] }) => { +export type Estimate = { q: EstimateType['q']; y: EstimateType['y'] }; + +export const estimateToString = (estimate: Estimate) => { if (!estimate.q) { return estimate.y; } return `${estimate.q}/${estimate.y}`; }; + +export const encodeEstimateFilterValue = ({ y, q }: Estimate) => encodeURIComponent(JSON.stringify({ q, y })); +export const decodeEstimateFilterValue = (data: string): null | Estimate => { + try { + const { q = null, y } = JSON.parse(decodeURIComponent(data)); + + if (y) { + return { q, y }; + } + + return null; + } catch (e) { + return null; + } +}; diff --git a/trpc/queries/goals.ts b/trpc/queries/goals.ts index 53eca7834..d415a69f9 100644 --- a/trpc/queries/goals.ts +++ b/trpc/queries/goals.ts @@ -1,7 +1,7 @@ import { Estimate, EstimateToGoal, Goal, GoalAchieveCriteria, Prisma, Role, State, StateType } from '@prisma/client'; import { QueryWithFilters } from '../../src/schema/common'; -import { quarters } from '../../src/utils/dateTime'; +import { decodeEstimateFilterValue } from '../../src/utils/estimateToString'; const defaultOrderBy = { updatedAt: 'desc', @@ -44,13 +44,11 @@ const getEstimateFilter = (data: QueryWithFilters): Prisma.GoalFindManyArgs['whe estimate: { some: { estimate: { - OR: data.estimate.reduce((acum, e) => { - const match = e.match( - new RegExp(`((?${Object.keys(quarters).join('|')})/)?(?[0-9]+$)`), - ); + OR: data.estimate.reduce<{ q?: string | null; y: string }[]>((acum, e) => { + const match = decodeEstimateFilterValue(e); if (match) { - const { groups: { q, y } = {} } = match; + const { q, y } = match; if (q) { acum.push({ @@ -70,7 +68,7 @@ const getEstimateFilter = (data: QueryWithFilters): Prisma.GoalFindManyArgs['whe } return acum; - }, [] as { q?: string | null; y: string }[]), + }, []), }, }, },