Skip to content

Commit 99b2ef5

Browse files
committed
feat: add clear button to search page
1 parent 96e691a commit 99b2ef5

File tree

7 files changed

+75
-32
lines changed

7 files changed

+75
-32
lines changed

src/components/ProductsFilters/ProductsFilters.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import { GetAllCategoriesQuery } from '@/graphql/types/graphql';
55
import useSearchPageParams from '@/hooks/useSearchPageParams';
66
import { SearchPageParamsKeys } from '@/utils/params';
77
import { useQuery } from '@apollo/client';
8-
import { Divider, List, ListItemText, Switch } from '@mui/material';
8+
import { List, ListItemText, Switch } from '@mui/material';
99
import { useTranslations } from 'next-intl';
1010
import { FC } from 'react';
1111
import Categories from './components/Categories';
1212
import { ListItem } from './components/ListItem';
13-
import { Title } from './components/Title';
1413

1514
export interface ProductsFiltersProps {}
1615

@@ -29,19 +28,25 @@ const ProductsFilters: FC<ProductsFiltersProps> = () => {
2928
};
3029

3130
return (
32-
<List>
31+
<List dense disablePadding>
3332
<ListItem disableGutters disableRipple>
3433
<ListItemText
35-
primary={<Title>{t('products.filters.categories')}</Title>}
34+
primary={t('products.filters.categories')}
35+
primaryTypographyProps={{
36+
variant: 'body1',
37+
fontWeight: 600,
38+
}}
3639
/>
3740
</ListItem>
3841
{!!categories?.length && <Categories options={categories} />}
3942

40-
<Divider />
41-
4243
<ListItem disableGutters disableRipple onClick={handleClickOnInStock}>
4344
<ListItemText
44-
primary={<Title>{t('products.filters.justInStock')}</Title>}
45+
primary={t('products.filters.justInStock')}
46+
primaryTypographyProps={{
47+
variant: 'body1',
48+
fontWeight: 600,
49+
}}
4550
/>
4651
<Switch checked={inStock} />
4752
</ListItem>

src/components/ProductsFilters/components/Categories.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Collapse, IconButton, List, ListItemText } from '@mui/material';
55
import { FC, MouseEventHandler, useMemo, useState } from 'react';
66
import { ProductCategoryOptions } from '../types';
77
import { ListItem } from './ListItem';
8-
import { Title } from './Title';
98

109
export interface CategoriesProps {
1110
options: ProductCategoryOptions;
@@ -80,24 +79,25 @@ const Categories: FC<CategoriesProps> = ({ options, parentId = null }) => {
8079
>
8180
<ListItem dense disableRipple>
8281
<ListItemText
83-
primary={<Title>{option.name}</Title>}
84-
onClick={handleClickOnItem(option.id)}
85-
sx={{
86-
color: (theme) =>
87-
isActive ? theme.palette.primary.main : undefined,
82+
primary={option.name}
83+
primaryTypographyProps={{
84+
variant: 'body2',
85+
...(isActive
86+
? {
87+
fontWeight: 700,
88+
color: 'primary',
89+
}
90+
: {}),
8891
}}
92+
onClick={handleClickOnItem(option.id)}
8993
/>
9094
{hasChildren && (
9195
<IconButton size="small" onClick={handleClickOnIcon(option.id)}>
9296
{open[option.id] ? <ExpandLess /> : <ExpandMore />}
9397
</IconButton>
9498
)}
9599
</ListItem>
96-
<Collapse
97-
timeout="auto"
98-
in={(isActive || open[option.id]) ?? false}
99-
unmountOnExit
100-
>
100+
<Collapse timeout="auto" in={open[option.id] ?? false} unmountOnExit>
101101
<Categories
102102
options={options}
103103
parentId={option.id > 0 ? option.id : null}

src/components/ProductsFilters/components/FiltersCard.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
import { Card, CardContent } from '@mui/material';
1+
'use client';
2+
3+
import useSearchPageParams from '@/hooks/useSearchPageParams';
4+
import { Button, Card, CardContent, CardHeader } from '@mui/material';
5+
import { useTranslations } from 'next-intl';
26
import React, { FC, ReactNode } from 'react';
37

48
export interface FiltersCardProps {
59
children: ReactNode;
610
}
711
const FiltersCard: FC<FiltersCardProps> = ({ children }) => {
12+
const t = useTranslations();
13+
14+
const { clear, count } = useSearchPageParams();
15+
816
return (
917
<Card
1018
variant="outlined"
@@ -13,6 +21,23 @@ const FiltersCard: FC<FiltersCardProps> = ({ children }) => {
1321
top: 130,
1422
}}
1523
>
24+
<CardHeader
25+
titleTypographyProps={{
26+
variant: 'h6',
27+
}}
28+
title={t('products.filters.title')}
29+
sx={{
30+
borderBottom: '1px solid',
31+
borderColor: 'divider',
32+
}}
33+
action={
34+
!!count && (
35+
<Button color="error" onClick={clear}>
36+
{t('products.filters.buttons.removeFilters')}
37+
</Button>
38+
)
39+
}
40+
/>
1641
<CardContent>{children}</CardContent>
1742
</Card>
1843
);

src/components/ProductsFilters/components/Title.tsx

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/data/i18n/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@
118118
"filters": {
119119
"title": "Filters",
120120
"categories": "Categories",
121-
"justInStock": "Just in stock"
121+
"justInStock": "Just in stock",
122+
"buttons": {
123+
"removeFilters": "Clear Filters"
124+
}
122125
},
123126
"sort": {
124127
"title": "Sort",

src/data/i18n/fa.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@
118118
"filters": {
119119
"title": "فیلترها",
120120
"categories": "دسته بندی ها",
121-
"justInStock": "فقط کالاهای موجود"
121+
"justInStock": "فقط کالاهای موجود",
122+
"buttons": {
123+
"removeFilters": "حذف فیلترها"
124+
}
122125
},
123126
"sort": {
124127
"title": "مرتب‌سازی",

src/hooks/useSearchPageParams.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,24 @@ export interface IUseSearchPageParams {
1414
key: SearchPageParamsKeys,
1515
value: string | number | boolean | null,
1616
) => void;
17+
clear: () => void;
18+
count: number;
1719
} & SearchPagesParams;
1820
}
1921

2022
const useSearchPageParams: IUseSearchPageParams = () => {
2123
const params = useSearchParams();
2224
const router = useRouter();
2325
const pathname = usePathname();
26+
const count = params.size;
27+
28+
const _redirect = (target: string) => {
29+
if (pathname.includes('/search')) {
30+
window.history.pushState(null, '', target);
31+
} else {
32+
router.push(target);
33+
}
34+
};
2435

2536
const navigate: ReturnType<IUseSearchPageParams>['navigate'] = (
2637
key,
@@ -34,16 +45,18 @@ const useSearchPageParams: IUseSearchPageParams = () => {
3445
}
3546

3647
const target = `/search?${newParams}`;
48+
_redirect(target);
49+
};
3750

38-
if (pathname.includes('/search')) {
39-
window.history.pushState(null, '', target);
40-
} else {
41-
router.push(target);
42-
}
51+
const clear = () => {
52+
const target = `/search`;
53+
_redirect(target);
4354
};
4455

4556
return {
4657
navigate,
58+
clear,
59+
count,
4760
...getSearchPageParams(params),
4861
};
4962
};

0 commit comments

Comments
 (0)