Skip to content

Commit f58bed1

Browse files
glo80771devpatil7glo82145
authored
PWA-3162-V3::Price filter change in sidebar (#4164)
* PWA-3162-V3::Price filter change in sidebar * PWA-3162-V3::testing the library issue * PWA-3162-V3::Testing with the library * PWA-3162-V3::Resolved filter dropdown * test * test * test --------- Co-authored-by: Devagouda Patil <[email protected]> Co-authored-by: Aanchal Pawar <[email protected]>
1 parent d0eb0a4 commit f58bed1

File tree

14 files changed

+380
-57
lines changed

14 files changed

+380
-57
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"watch:venia": "yarn venia run watch"
4747
},
4848
"dependencies": {
49+
"@material-ui/core": "~4.12.4",
4950
"caniuse-lite": "~1.0.30001335"
5051
},
5152
"devDependencies": {
@@ -115,4 +116,4 @@
115116
"maxSize": "100 kB"
116117
}
117118
]
118-
}
119+
}

packages/peregrine/lib/talons/FilterModal/__tests__/useFilterBlock.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { useFilterBlock } from '../useFilterBlock';
66

77
const log = jest.fn();
88

9+
jest.mock('react-router-dom', () => ({
10+
useLocation: jest.fn(() => ({ search: '?a=b&c=d' }))
11+
}));
12+
913
let handleClickProp = null;
1014
let inputValues = {};
1115

packages/peregrine/lib/talons/FilterModal/useFilterBlock.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { useCallback, useState, useEffect, useMemo } from 'react';
2+
import { useLocation } from 'react-router-dom';
23

34
export const useFilterBlock = props => {
4-
const { filterState, items, initialOpen } = props;
5+
const { filterState, items, initialOpen, group } = props;
6+
const location = useLocation();
57

68
const hasSelected = useMemo(() => {
9+
const params = new URLSearchParams(location.search);
10+
//expansion of price filter dropdown
11+
if (group == 'price') {
12+
return params.get('price[filter]') ? true : false;
13+
}
714
return items.some(item => {
815
return filterState && filterState.has(item);
916
});
10-
}, [filterState, items]);
17+
}, [filterState, items, group, location.search]);
1118

1219
const [isExpanded, setExpanded] = useState(hasSelected || initialOpen);
1320

packages/peregrine/lib/talons/FilterSidebar/__tests__/useFilterSidebar.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ jest.mock('react-router-dom', () => ({
6868
useHistory: jest.fn(() => ({ push: jest.fn() })),
6969
useLocation: jest.fn(() => ({ pathname: '', search: '' }))
7070
}));
71+
7172
const mockPush = jest.fn();
7273
useHistory.mockImplementation(() => ({ push: mockPush }));
7374

packages/peregrine/lib/talons/FilterSidebar/useFilterSidebar.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,10 @@ export const useFilterSidebar = props => {
182182
}, [handleClose]);
183183

184184
const handleReset = useCallback(() => {
185-
filterApi.clear();
186-
setIsApplying(true);
187-
}, [filterApi, setIsApplying]);
185+
// filterApi.clear();
186+
// setIsApplying(true);
187+
history.replace({ search: 'page=1' });
188+
}, [history]);
188189

189190
const handleKeyDownActions = useCallback(
190191
event => {

packages/venia-ui/lib/components/FilterModal/CurrentFilters/__tests__/currentFilter.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ const mockOnRemove = jest.fn();
1616

1717
jest.mock('../../../Trigger', () => props => <mock-Trigger {...props} />);
1818

19+
jest.mock('react-router-dom', () => ({
20+
useLocation: jest.fn(() => ({ search: '?a=b&c=d' })),
21+
useHistory: jest.fn()
22+
}));
23+
1924
let inputProps = {};
2025

2126
const Component = () => {

packages/venia-ui/lib/components/FilterModal/CurrentFilters/currentFilter.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,28 @@ import { useStyle } from '../../../classify';
77
import Icon from '../../Icon';
88
import Trigger from '../../Trigger';
99
import defaultClasses from './currentFilter.module.css';
10+
import { useHistory, useLocation } from 'react-router-dom';
1011

1112
const CurrentFilter = props => {
1213
const { group, item, removeItem, onRemove } = props;
1314
const classes = useStyle(defaultClasses, props.classes);
1415
const { formatMessage } = useIntl();
16+
const location = useLocation();
17+
const history = useHistory();
1518

1619
const handleClick = useCallback(() => {
1720
removeItem({ group, item });
1821
if (typeof onRemove === 'function') {
1922
onRemove(group, item);
2023
}
21-
}, [group, item, removeItem, onRemove]);
24+
25+
if (group == 'price') {
26+
// preserve all existing params
27+
const params = new URLSearchParams(location.search);
28+
params.delete('price[filter]');
29+
history.replace({ search: params.toString() });
30+
}
31+
}, [group, item, removeItem, onRemove, history, location.search]);
2232

2333
const ariaLabel = formatMessage(
2434
{

packages/venia-ui/lib/components/FilterModal/FilterList/__tests__/filterList.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ jest.mock('@magento/peregrine/lib/talons/FilterModal', () => ({
3333
})
3434
}));
3535

36+
jest.mock('react-router-dom', () => ({
37+
useHistory: jest.fn(() => ({ push: jest.fn() })),
38+
useLocation: jest.fn(() => ({ pathname: '', search: '' }))
39+
}));
40+
3641
let inputProps = {};
3742

3843
const Component = () => {

packages/venia-ui/lib/components/FilterModal/FilterList/filterList.js

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { useStyle } from '../../../classify';
88
import FilterItem from './filterItem';
99
import defaultClasses from './filterList.module.css';
1010
import FilterItemRadioGroup from './filterItemRadioGroup';
11+
import Slider from '@material-ui/core/Slider';
12+
import { useHistory, useLocation } from 'react-router-dom';
1113

1214
const labels = new WeakMap();
1315

@@ -22,11 +24,22 @@ const FilterList = props => {
2224
items,
2325
onApply
2426
} = props;
27+
const { pathname, search } = useLocation();
28+
const history = useHistory();
2529
const classes = useStyle(defaultClasses, props.classes);
2630
const talonProps = useFilterList({ filterState, items, itemCountToShow });
2731
const { isListExpanded, handleListToggle } = talonProps;
2832
const { formatMessage } = useIntl();
2933

34+
if (name == 'Price') {
35+
var minRange = Number(items[0].value.split('_')[0]);
36+
var maxRange = Number(items[items.length - 1].value.split('_')[1]);
37+
}
38+
const [value, setValue] = React.useState([
39+
minRange ? minRange : null,
40+
maxRange ? maxRange : null
41+
]);
42+
3043
// memoize item creation
3144
// search value is not referenced, so this array is stable
3245
const itemElements = useMemo(() => {
@@ -51,36 +64,75 @@ const FilterList = props => {
5164
);
5265
}
5366

54-
return items.map((item, index) => {
55-
const { title, value } = item;
56-
const key = `item-${group}-${value}`;
67+
const handleChange = (event, newValue) => {
68+
//removing the previous price filter from the url
69+
const test = String(search).split('&');
70+
const filters = test.filter(element => {
71+
return !element.includes('price');
72+
});
73+
const newSearch = filters.join('&');
74+
const nextParams = new URLSearchParams(newSearch);
5775

58-
if (!isListExpanded && index >= itemCountToShow) {
59-
return null;
60-
}
76+
//appending the new price filter range in the url
77+
const DELIMITER = ',';
78+
const title = String(newValue[0]) + '-' + String(newValue[1]);
79+
const value = String(newValue[0]) + '_' + String(newValue[1]);
80+
nextParams.append(
81+
`${group}[filter]`,
82+
`${title}${DELIMITER}${value}`
83+
);
6184

62-
// create an element for each item
63-
const element = (
64-
<li
65-
key={key}
66-
className={classes.item}
67-
data-cy="FilterList-item"
68-
>
69-
<FilterItem
70-
filterApi={filterApi}
71-
filterState={filterState}
72-
group={group}
73-
item={item}
74-
onApply={onApply}
85+
// write price filter state to history
86+
history.push({ pathname, search: String(nextParams) });
87+
88+
//setting new value to the slider on change
89+
setValue(newValue);
90+
};
91+
92+
if (name == 'Price') {
93+
return (
94+
<div className={classes.root}>
95+
<Slider
96+
value={value}
97+
onChange={handleChange}
98+
valueLabelDisplay="auto"
99+
aria-labelledby="range-slider"
100+
min={minRange}
101+
max={maxRange}
75102
/>
76-
</li>
103+
</div>
77104
);
105+
} else {
106+
return items.map((item, index) => {
107+
const { title, value } = item;
108+
const key = `item-${group}-${value}`;
109+
110+
if (!isListExpanded && index >= itemCountToShow) {
111+
return null;
112+
}
78113

79-
// associate each element with its normalized title
80-
// titles are not unique, so use the element as the key
81-
labels.set(element, title.toUpperCase());
82-
return element;
83-
});
114+
// create an element for each item
115+
const element = (
116+
<li
117+
key={key}
118+
className={classes.item}
119+
data-cy="FilterList-item"
120+
>
121+
<FilterItem
122+
filterApi={filterApi}
123+
filterState={filterState}
124+
group={group}
125+
item={item}
126+
onApply={onApply}
127+
/>
128+
</li>
129+
);
130+
// associate each element with its normalized title
131+
// titles are not unique, so use the element as the key
132+
labels.set(element, title.toUpperCase());
133+
return element;
134+
});
135+
}
84136
}, [
85137
classes,
86138
filterApi,
@@ -91,7 +143,13 @@ const FilterList = props => {
91143
items,
92144
isListExpanded,
93145
itemCountToShow,
94-
onApply
146+
onApply,
147+
history,
148+
minRange,
149+
maxRange,
150+
pathname,
151+
search,
152+
value
95153
]);
96154

97155
const showMoreLessItem = useMemo(() => {

packages/venia-ui/lib/components/FilterModal/filterBlock.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const FilterBlock = props => {
2828
const talonProps = useFilterBlock({
2929
filterState,
3030
items,
31-
initialOpen
31+
initialOpen,
32+
group
3233
});
3334
const { handleClick, isExpanded } = talonProps;
3435
const iconSrc = isExpanded ? ArrowUp : ArrowDown;

0 commit comments

Comments
 (0)