Skip to content

Commit af09ca0

Browse files
JakobMiesnerkpsherva
authored andcommitted
backoffice: fix: loans: ensure date filters can be changed
* previously changing the date filter just added another instance of the filter to the url
1 parent 9dd5c34 commit af09ca0

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/lib/pages/backoffice/Loan/LoanSearch/SearchDateRange.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,18 @@ export class _SearchDateRange extends Component {
1919
return [fromDate, toDate];
2020
}
2121

22-
onDateChange = (newFilter) => {
22+
onDateChange = (field, value) => {
2323
const { currentQueryState, updateQueryState } = this.props;
24-
const filters = currentQueryState.filters;
25-
filters.push(newFilter);
26-
return updateQueryState({ filters: filters });
24+
25+
const updatedFilters = currentQueryState.filters.filter(
26+
([name]) => name !== field
27+
);
28+
29+
if (value !== '') {
30+
updatedFilters.push([field, value]);
31+
}
32+
33+
return updateQueryState({ filters: updatedFilters });
2734
};
2835

2936
render() {
@@ -43,7 +50,7 @@ export class _SearchDateRange extends Component {
4350
defaultValue={fromDate}
4451
placeholder="From"
4552
handleDateChange={(value) =>
46-
this.onDateChange(['loans_from_date', value])
53+
this.onDateChange('loans_from_date', value)
4754
}
4855
/>
4956
</Card.Content>
@@ -53,7 +60,7 @@ export class _SearchDateRange extends Component {
5360
defaultValue={toDate}
5461
placeholder="To"
5562
handleDateChange={(value) =>
56-
this.onDateChange(['loans_to_date', value])
63+
this.onDateChange('loans_to_date', value)
5764
}
5865
/>
5966
</Card.Content>

src/lib/pages/backoffice/Loan/LoanSearch/SearchDateRange.test.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ jest.mock('@components/DatePicker', () => {
1010
};
1111
});
1212

13-
const newFilter = ['loans_from_date', '2020-02-02'];
13+
const newFilterField = 'loans_from_date';
14+
const newFilterValue = '2020-02-02';
1415
const mockCurrentQueryState = { filters: [] };
1516
const mockUpdateQueryState = jest.fn();
1617

@@ -39,7 +40,7 @@ describe('SearchDateRange tests', () => {
3940
/>
4041
);
4142

42-
wrapper.instance().onDateChange(newFilter);
43+
wrapper.instance().onDateChange(newFilterField, newFilterValue);
4344
const expected = { filters: [['loans_from_date', '2020-02-02']] };
4445
const mockFn = wrapper.instance().props.updateQueryState;
4546
expect(mockFn).toBeCalledWith(expected);
@@ -55,12 +56,9 @@ describe('SearchDateRange tests', () => {
5556
updateQueryState={mockUpdateQueryState}
5657
/>
5758
);
58-
wrapper.instance().onDateChange(newFilter);
59+
wrapper.instance().onDateChange(newFilterField, newFilterValue);
5960
const expected = {
60-
filters: [
61-
['loans_from_date', '2020-02-01'],
62-
['loans_from_date', '2020-02-02'],
63-
],
61+
filters: [['loans_from_date', '2020-02-02']],
6462
};
6563
const mockFn = wrapper.instance().props.updateQueryState;
6664
expect(mockFn).toBeCalledWith(expected);
@@ -75,9 +73,12 @@ describe('SearchDateRange tests', () => {
7573
updateQueryState={mockUpdateQueryState}
7674
/>
7775
);
78-
wrapper.instance().onDateChange(newFilter);
76+
wrapper.instance().onDateChange(newFilterField, newFilterValue);
7977
const expected = {
80-
filters: mockCurrentQueryState.filters,
78+
filters: [
79+
['loans_to_date', '2020-03-03'],
80+
['loans_from_date', '2020-02-02'],
81+
],
8182
};
8283
const mockFn = wrapper.instance().props.updateQueryState;
8384
expect(mockFn).toBeCalledWith(expected);

0 commit comments

Comments
 (0)