Skip to content

Commit 7a24b8b

Browse files
UISAUTCOMP-135: useAutoOpenDetailView - block auto-open on mount if record id exists in URL.
1 parent f3e78d6 commit 7a24b8b

File tree

4 files changed

+78
-9
lines changed

4 files changed

+78
-9
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.3] (IN PROGRESS)
4+
5+
- [UISAUTCOMP-135](https://issues.folio.org/browse/UISAUTCOMP-135) `useAutoOpenDetailView` - block auto-open on mount if record id exists in URL.
6+
37
## [5.0.2] (https://github.com/folio-org/stripes-authority-components/tree/v5.0.2) (2024-12-10)
48

59
- [UISAUTCOMP-133](https://issues.folio.org/browse/UISAUTCOMP-133) Handle uncaught error when a search request fails.

lib/hooks/useAutoOpenDetailView/useAutoOpenDetailView.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ import { AuthoritiesSearchContext } from '../../context';
1717
const useAutoOpenDetailView = (authorities, onOpenDetailView, isLoading = false, urlAuthorityId = '') => {
1818
const { navigationSegmentValue } = useContext(AuthoritiesSearchContext);
1919
const prevOpenedSingleAuthority = useRef(null);
20+
const isBlockOnMount = useRef(false);
21+
22+
useEffect(() => {
23+
// block auto-opening on page reload and redirection from quick marc.
24+
if (urlAuthorityId) {
25+
isBlockOnMount.current = true;
26+
}
27+
// eslint-disable-next-line react-hooks/exhaustive-deps
28+
}, []);
2029

2130
useEffect(() => {
2231
// do nothing during loading, to be able to correctly compare the previously opened record with the current one.
@@ -32,14 +41,10 @@ const useAutoOpenDetailView = (authorities, onOpenDetailView, isLoading = false,
3241
if (navigationSegmentValue === navigationSegments.browse) {
3342
isDetailViewNeedsToBeOpen = authority?.isAnchor && authority?.isExactMatch;
3443
} else {
35-
// Check the record id so that when the third pane has a single record open and the user creates a record
36-
// on a different route and then is redirected back to the third pane, then the third pane should contain
37-
// the newly created record instead of the previously opened one.
38-
if (urlAuthorityId) {
39-
// A single record can be opened in two ways: when reloading the page with opened single record and when all
40-
// the conditions for auto-opening a single record are met. So let's save the record for both cases. This will
41-
// be used to compare the previously opened record with the current one.
44+
if (isBlockOnMount.current) {
45+
// record should be stored to avoid auto-opening after closing the third pane.
4246
prevOpenedSingleAuthority.current = authority;
47+
isBlockOnMount.current = false;
4348
return;
4449
}
4550

@@ -53,7 +58,7 @@ const useAutoOpenDetailView = (authorities, onOpenDetailView, isLoading = false,
5358
prevOpenedSingleAuthority.current = authority;
5459
onOpenDetailView(authority);
5560
}
56-
}, [authorities, navigationSegmentValue, onOpenDetailView, urlAuthorityId, isLoading]);
61+
}, [authorities, navigationSegmentValue, onOpenDetailView, isLoading]);
5762
};
5863

5964
export default useAutoOpenDetailView;

lib/hooks/useAutoOpenDetailView/useAutoOpenDetailView.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,66 @@ describe('useAutoOpenDetailView hook', () => {
4949
});
5050

5151
describe('when there is a record ID in URL', () => {
52+
describe('and it is mount (page reload or redirection from quick marc)', () => {
53+
it('should not open record`s detail view', async () => {
54+
const isLoading = true;
55+
const urlAuthorityId = 'fake-id';
56+
57+
const initialProps = [[], openDetailView, isLoading, urlAuthorityId];
58+
59+
const { rerender } = renderHook(_initialProps => useAutoOpenDetailView(..._initialProps), {
60+
wrapper: Wrapper,
61+
initialProps,
62+
});
63+
64+
rerender([
65+
[authorities[0]],
66+
openDetailView,
67+
false,
68+
urlAuthorityId,
69+
]);
70+
71+
expect(openDetailView).not.toHaveBeenCalled();
72+
});
73+
});
74+
75+
describe('and closing the opened single record', () => {
76+
it('should not reopen record`s detail view', () => {
77+
const isLoading = true;
78+
const urlAuthorityId = 'fake-id';
79+
80+
const initialProps = [
81+
[],
82+
openDetailView,
83+
isLoading,
84+
urlAuthorityId,
85+
];
86+
87+
const { rerender } = renderHook(_initialProps => useAutoOpenDetailView(..._initialProps), {
88+
initialProps,
89+
wrapper: Wrapper,
90+
});
91+
92+
rerender([
93+
[authorities[0]],
94+
openDetailView,
95+
false,
96+
urlAuthorityId,
97+
]);
98+
99+
const _urlAuthorityId = null;
100+
101+
rerender([
102+
[authorities[0]],
103+
openDetailView,
104+
false,
105+
_urlAuthorityId,
106+
]);
107+
108+
expect(openDetailView).not.toHaveBeenCalled();
109+
});
110+
});
111+
52112
it('should not open record`s detail view', () => {
53113
const isLoading = false;
54114
const urlAuthorityId = 'fake-id';

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.2",
3+
"version": "5.0.3",
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)