Skip to content

Commit

Permalink
fix(FiltersPanel): estimate as JSON at search URL
Browse files Browse the repository at this point in the history
  • Loading branch information
asabotovich committed Aug 30, 2023
1 parent 5355b76 commit d8ef40e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
13 changes: 6 additions & 7 deletions src/components/EstimateFilter.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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],
);

Expand Down
9 changes: 8 additions & 1 deletion src/components/FiltersPanelApplied/FiltersPanelApplied.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -98,7 +99,13 @@ export const FiltersPanelApplied: React.FC<FiltersPanelAppliedProps> = ({
}

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]) => {
Expand Down
21 changes: 19 additions & 2 deletions src/utils/estimateToString.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};
12 changes: 5 additions & 7 deletions trpc/queries/goals.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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(`((?<q>${Object.keys(quarters).join('|')})/)?(?<y>[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({
Expand All @@ -70,7 +68,7 @@ const getEstimateFilter = (data: QueryWithFilters): Prisma.GoalFindManyArgs['whe
}

return acum;
}, [] as { q?: string | null; y: string }[]),
}, []),
},
},
},
Expand Down

0 comments on commit d8ef40e

Please sign in to comment.