Skip to content

Commit 4930950

Browse files
committed
UISAUTCOMP-130 Provide a prop to <AcqDateRangeFilter> to subscribe to search form resets. (#172)
* UISAUTCOMP-130 Provide a prop to `<AcqDateRangeFilter>` to subscribe to search form resets. * UISAUTCOMP-130 fix eslint error
1 parent 5a634e6 commit 4930950

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change history for stripes-authoriy-components
22

3+
## [5.0.1] (IN PROGRESS)
4+
5+
- [UISAUTCOMP-130](https://issues.folio.org/browse/UISAUTCOMP-130) Provide a prop to `<AcqDateRangeFilter>` to subscribe to search form resets.
6+
37
## [5.0.0] (https://github.com/folio-org/stripes-authority-components/tree/v5.0.0) (2024-10-30)
48

59
- [UISAUTCOMP-117](https://issues.folio.org/browse/UISAUTCOMP-117) Provide deprecation notice to `useUserTenantPermissions.js`.

lib/FilterNavigation/FilterNavigation.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { useContext } from 'react';
1+
import {
2+
useCallback,
3+
useContext,
4+
} from 'react';
25
import { FormattedMessage } from 'react-intl';
36

47
import {
@@ -13,8 +16,14 @@ const FilterNavigation = () => {
1316
const {
1417
navigationSegmentValue,
1518
setNavigationSegmentValue,
19+
unsubscribeFromReset,
1620
} = useContext(AuthoritiesSearchContext);
1721

22+
const handleSegmentClick = useCallback(name => {
23+
setNavigationSegmentValue(name);
24+
unsubscribeFromReset();
25+
}, [unsubscribeFromReset, setNavigationSegmentValue]);
26+
1827
return (
1928
<ButtonGroup
2029
fullWidth
@@ -29,7 +38,7 @@ const FilterNavigation = () => {
2938
role="tab"
3039
id={`segment-navigation-${name}`}
3140
data-testid={`segment-navigation-${name}`}
32-
onClick={() => setNavigationSegmentValue(name)}
41+
onClick={() => handleSegmentClick(name)}
3342
>
3443
<FormattedMessage id={`stripes-authority-components.label.${name}`} />
3544
</Button>

lib/FilterNavigation/FilterNavigation.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import Harness from '../../test/jest/helpers/harness';
1212
const mockSetNavigationSegmentValue = jest.fn();
1313
const mockSetSearchDropdownValue = jest.fn();
1414
const mockSetSearchIndex = jest.fn();
15+
const mockUnsubscribeFromReset = jest.fn();
1516

1617
const authoritiesCtxValue = {
1718
setNavigationSegmentValue: mockSetNavigationSegmentValue,
1819
setSearchDropdownValue: mockSetSearchDropdownValue,
1920
setSearchIndex: mockSetSearchIndex,
21+
unsubscribeFromReset: mockUnsubscribeFromReset,
2022
};
2123

2224
const renderFilterNavigation = () => render(

lib/SearchFilters/SearchFilters.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const SearchFilters = ({
4242
filters,
4343
setFilters,
4444
navigationSegmentValue,
45+
subscribeOnReset,
4546
} = useContext(AuthoritiesSearchContext);
4647

4748
const [filterAccordions, { handleSectionToggle }] = useSectionToggle({
@@ -138,6 +139,7 @@ const SearchFilters = ({
138139
disabled={isLoading}
139140
closedByDefault
140141
dateFormat={DATE_FORMAT}
142+
subscribeOnReset={subscribeOnReset}
141143
/>
142144
}
143145
{isVisible(FILTERS.UPDATED_DATE) &&
@@ -150,6 +152,7 @@ const SearchFilters = ({
150152
disabled={isSearching}
151153
closedByDefault
152154
dateFormat={DATE_FORMAT}
155+
subscribeOnReset={subscribeOnReset}
153156
/>
154157
}
155158
</>

lib/context/AuthoritiesSearchContext.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ const AuthoritiesSearchContextProvider = ({
4848
[navigationSegments.search]: null,
4949
[navigationSegments.browse]: null,
5050
});
51+
const subscribers = useRef([]);
52+
53+
const subscribeOnReset = useCallback(cb => {
54+
const isSubscribed = subscribers.current.some(callback => callback === cb);
55+
56+
if (!isSubscribed) {
57+
subscribers.current.push(cb);
58+
}
59+
}, []);
60+
61+
const unsubscribeFromReset = useCallback(() => {
62+
subscribers.current = [];
63+
}, []);
64+
65+
const publishOnReset = useCallback(() => {
66+
subscribers.current.forEach(cb => cb());
67+
}, []);
5168

5269
const searchParams = readParamsFromUrl
5370
? queryString.parse(location.search)
@@ -186,6 +203,7 @@ const AuthoritiesSearchContextProvider = ({
186203
setIsGoingToBaseURL(true);
187204
setAdvancedSearchDefaultSearch(null);
188205
paramsBySegment.current[navigationSegmentValue] = null;
206+
publishOnReset();
189207
};
190208

191209
const contextValue = {
@@ -218,6 +236,8 @@ const AuthoritiesSearchContextProvider = ({
218236
setIsGoingToBaseURL,
219237
advancedSearchDefaultSearch,
220238
setAdvancedSearchDefaultSearch,
239+
subscribeOnReset,
240+
unsubscribeFromReset,
221241
};
222242

223243
return (

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@folio/stripes-authority-components",
3-
"version": "5.0.0",
3+
"version": "5.0.1",
44
"description": "Component library for Stripes Authority modules",
55
"repository": "https://github.com/folio-org/stripes-authority-components",
66
"main": "index.js",

0 commit comments

Comments
 (0)