Skip to content

Commit

Permalink
UIIN-3137: Add the isSearchToggleHitInBrowse flag to the history st…
Browse files Browse the repository at this point in the history
…ate to use in the condition that specifies whether to use the default sort on mount. (#2682)
  • Loading branch information
Dmytro-Melnyshyn authored Dec 2, 2024
1 parent 034df62 commit a83e729
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Display informative error message when editing same instance, holdings, item in two tabs. Fixes UIIN-3127.
* Display user's name instead of "Unknown user" in "Last updated" field in "Settings" for member tenant. Fixes UIIN-3144.
* Update Linked data API URL to use the new API path. Fixes UIIN-3146.
* Add the `isSearchToggleHitInBrowse` flag to the history state to use in the condition that specifies whether to use the default sort on mount. Fixes UIIN-3137.

## [12.0.3](https://github.com/folio-org/ui-inventory/tree/v12.0.3) (2024-11-27)
[Full Changelog](https://github.com/folio-org/ui-inventory/compare/v12.0.2...v12.0.3)
Expand Down
10 changes: 9 additions & 1 deletion src/components/InstancesList/InstancesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ class InstancesList extends React.Component {
data,
} = this.props;

const { isSearchToggleHitInBrowse } = _location.state || {};
const params = getParams();
const defaultSort = data.displaySettings.defaultSort;

Expand All @@ -251,9 +252,16 @@ class InstancesList extends React.Component {
const searchParams = new URLSearchParams(_location.search);

const isStaffSuppressFilterChanged = this.applyDefaultStaffSuppressFilter(searchParams);
let isSortingChanged = false;

if (params.sort !== defaultSort || isStaffSuppressFilterChanged) {
// don't set the default sort when redirecting from Browse search by hitting the Search toggle,
// the previously selected sort option should be used (it will be taken from session storage).
if (params.sort !== defaultSort && !isSearchToggleHitInBrowse) {
isSortingChanged = true;
searchParams.set('sort', defaultSort);
}

if (isSortingChanged || isStaffSuppressFilterChanged) {
this.redirectToSearchParams(searchParams);
}
}
Expand Down
47 changes: 42 additions & 5 deletions src/components/InstancesList/InstancesList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,13 @@ describe('InstancesList', () => {
it('should call history.replace with sort parameter from Settings', () => {
jest.spyOn(history, 'replace');

history.push('/inventory?filters=staffSuppress.false&sort=title');
history.push({
pathname: '/inventory',
search: '?filters=staffSuppress.false&sort=title',
state: {
isSearchToggleHitInBrowse: false,
},
});

renderInstancesList({
segment: 'instances',
Expand All @@ -329,10 +335,41 @@ describe('InstancesList', () => {
},
});

expect(history.replace).toHaveBeenLastCalledWith({
pathname: '/inventory',
search: 'filters=staffSuppress.false&sort=relevance',
state: undefined,
expect(history.replace).toHaveBeenLastCalledWith(expect.objectContaining({
search: expect.stringContaining('sort=relevance'),
}));
});

describe('and redirection from Browse search by pressing Search toggle', () => {
it('should not call history.replace with sort parameter from Settings', () => {
jest.spyOn(history, 'replace');

history.push({
pathname: '/inventory',
search: '?filters=staffSuppress.false&sort=title',
state: {
isSearchToggleHitInBrowse: true,
},
});

const defaultSort = SORT_OPTIONS.RELEVANCE;

renderInstancesList({
segment: segments.instances,
data: {
...data,
query: {
query: '',
},
displaySettings: {
defaultSort,
},
},
});

expect(history.replace).not.toHaveBeenLastCalledWith(expect.objectContaining({
search: expect.stringContaining(`sort=${defaultSort}`),
}));
});
});
});
Expand Down
3 changes: 3 additions & 0 deletions src/views/BrowseInventory/BrowseInventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ const BrowseInventory = () => {
>
<SearchModeNavigation
search={getLastSearch()}
state={{
isSearchToggleHitInBrowse: true,
}}
/>

<SingleSearchForm
Expand Down
8 changes: 7 additions & 1 deletion src/views/BrowseInventory/BrowseInventory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,17 @@ describe('BrowseInventory', () => {

it('should have search prop in SearchModeNavigation component', () => {
const search = '?qindex=title&query=book&sort=title';
const state = {
isSearchToggleHitInBrowse: true,
};

mockGetLastSearch.mockClear().mockImplementation(() => search);
renderBrowseInventory();

expect(SearchModeNavigation).toHaveBeenCalledWith({ search }, {});
expect(SearchModeNavigation).toHaveBeenCalledWith({
search,
state,
}, {});
mockGetLastSearch.mockRestore();
});

Expand Down

0 comments on commit a83e729

Please sign in to comment.