Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UISAUTCOMP-135: useAutoOpenDetailView - block auto-open on mount if record id exists in URL. #178

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
- [UISAUTCOMP-136](https://folio-org.atlassian.net/browse/UISAUTCOMP-136) Spitfire - migrate to shared CI workflows.
- [UISAUTCOMP-118](https://folio-org.atlassian.net/browse/UISAUTCOMP-118) React v19: refactor away from default props for functional components.

## [5.0.3] (IN PROGRESS)

- [UISAUTCOMP-135](https://issues.folio.org/browse/UISAUTCOMP-135) `useAutoOpenDetailView` - block auto-open on mount if record id exists in URL.

## [5.0.2] (https://github.com/folio-org/stripes-authority-components/tree/v5.0.2) (2024-12-10)

- [UISAUTCOMP-133](https://issues.folio.org/browse/UISAUTCOMP-133) Handle uncaught error when a search request fails.
Expand Down
21 changes: 13 additions & 8 deletions lib/hooks/useAutoOpenDetailView/useAutoOpenDetailView.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ import { AuthoritiesSearchContext } from '../../context';
const useAutoOpenDetailView = (authorities, onOpenDetailView, isLoading = false, urlAuthorityId = '') => {
const { navigationSegmentValue } = useContext(AuthoritiesSearchContext);
const prevOpenedSingleAuthority = useRef(null);
const isBlockOnMount = useRef(false);

useEffect(() => {
// block auto-opening on page reload and redirection from quick marc.
if (urlAuthorityId) {
isBlockOnMount.current = true;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

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

Expand All @@ -53,7 +58,7 @@ const useAutoOpenDetailView = (authorities, onOpenDetailView, isLoading = false,
prevOpenedSingleAuthority.current = authority;
onOpenDetailView(authority);
}
}, [authorities, navigationSegmentValue, onOpenDetailView, urlAuthorityId, isLoading]);
}, [authorities, navigationSegmentValue, onOpenDetailView, isLoading]);
};

export default useAutoOpenDetailView;
60 changes: 60 additions & 0 deletions lib/hooks/useAutoOpenDetailView/useAutoOpenDetailView.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,66 @@ describe('useAutoOpenDetailView hook', () => {
});

describe('when there is a record ID in URL', () => {
describe('and it is mount (page reload or redirection from quick marc)', () => {
it('should not open record`s detail view', () => {
const isLoading = true;
const urlAuthorityId = 'fake-id';

const initialProps = [[], openDetailView, isLoading, urlAuthorityId];

const { rerender } = renderHook(_initialProps => useAutoOpenDetailView(..._initialProps), {
wrapper: Wrapper,
initialProps,
});

rerender([
[authorities[0]],
openDetailView,
false,
urlAuthorityId,
]);

expect(openDetailView).not.toHaveBeenCalled();
});
});

describe('and closing the opened single record', () => {
it('should not reopen record`s detail view', () => {
const isLoading = true;
const urlAuthorityId = 'fake-id';

const initialProps = [
[],
openDetailView,
isLoading,
urlAuthorityId,
];

const { rerender } = renderHook(_initialProps => useAutoOpenDetailView(..._initialProps), {
initialProps,
wrapper: Wrapper,
});

rerender([
[authorities[0]],
openDetailView,
false,
urlAuthorityId,
]);

const _urlAuthorityId = null;

rerender([
[authorities[0]],
openDetailView,
false,
_urlAuthorityId,
]);

expect(openDetailView).not.toHaveBeenCalled();
});
});

it('should not open record`s detail view', () => {
const isLoading = false;
const urlAuthorityId = 'fake-id';
Expand Down
Loading