Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro-Melnyshyn committed May 7, 2024
2 parents efa8139 + bf8021e commit e3267b5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Jest/RTL: Cover InstancePlugin component with unit tests. Refs UIIN-2668.
* Jest/RTL: Cover ImportRecord component with unit test. Refs UIIN-2667.
* Jest/RTL: Cover MoveHoldingContext component with unit tests. Refs UIIN-2664.
* Use consolidated locations endpoint to fetch all locations when in central tenant context. Refs UIIN-2811.
* Change label of eye-readable call number search option in holdings/items. Refs UIIN-2797.

## [11.0.4](https://github.com/folio-org/ui-inventory/tree/v11.0.4) (2024-04-30)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,8 @@
"browse.subjects.instances.collection.get",
"browse.classification-numbers.instances.collection.get",
"perms.users.get",
"inventory-storage.bound-with-parts.collection.get"
"inventory-storage.bound-with-parts.collection.get",
"consortium-search.locations.collection.get"
],
"visible": true
},
Expand Down
22 changes: 18 additions & 4 deletions src/hooks/useLocationsForTenants/useLocationsForTenants.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { useQueries } from 'react-query';
import {
useQueries,
useQuery,
} from 'react-query';

import {
useNamespace,
useOkapiKy,
useStripes,
checkIfUserInCentralTenant,
} from '@folio/stripes/core';

import { isUserInConsortiumMode } from '../../utils';
Expand All @@ -18,9 +22,11 @@ const useLocationsForTenants = ({ tenantIds = [] }) => {
const stripes = useStripes();
const ky = useOkapiKy();

const isUserInCentralTenant = checkIfUserInCentralTenant(stripes);

const queries = useQueries(tenantIds.map(tenantId => {
return {
enabled: Boolean(tenantIds.length && isUserInConsortiumMode(stripes)),
enabled: Boolean(tenantIds.length && isUserInConsortiumMode(stripes) && !isUserInCentralTenant),
queryKey: [namespace, 'locations', tenantId],
queryFn: () => ky.get('locations', {
searchParams: {
Expand All @@ -38,10 +44,18 @@ const useLocationsForTenants = ({ tenantIds = [] }) => {
};
}));

const locationsFromAllTenants = queries.map(({ data }) => data?.locations).filter(Boolean).flat();
const { data: consolidatedLocations } = useQuery({
queryKey: [namespace, 'consolidatedLocations'],
queryFn: () => ky.get('search/consortium/locations').json(),
enabled: Boolean(isUserInConsortiumMode(stripes) && isUserInCentralTenant),
});

const locationsFromAllTenants = isUserInCentralTenant
? consolidatedLocations?.locations
: queries.map(({ data }) => data?.locations).filter(Boolean).flat();

return {
data: locationsFromAllTenants,
data: locationsFromAllTenants || [],
isLoading: queries.some(({ isFetching }) => isFetching),
};
};
Expand Down
65 changes: 53 additions & 12 deletions src/hooks/useLocationsForTenants/useLocationsForTenants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {
QueryClientProvider,
} from 'react-query';

import { useOkapiKy } from '@folio/stripes/core';
import {
useOkapiKy,
checkIfUserInCentralTenant,
} from '@folio/stripes/core';

import {
renderHook,
Expand Down Expand Up @@ -37,22 +40,60 @@ describe('useLocationsForTenants', () => {

const tenantIds = ['cs00000int', 'cs00000int_0002'];

it('should fetch locations of all tenants', async () => {
const { result } = renderHook(() => useLocationsForTenants({ tenantIds }), { wrapper });
describe('when user is in a member tenant', () => {
it('should fetch locations of all tenants via multiple requests', async () => {
const { result } = renderHook(() => useLocationsForTenants({ tenantIds }), { wrapper });

await act(() => !result.current.isLoading);

expect(mockGet.mock.calls[0][0]).toBe('locations');
expect(mockGet.mock.calls[1][0]).toBe('locations');
expect(result.current.data).toEqual([
{ id: 'location-id' },
{ id: 'location-id' },
]);
});

it('should not call consolidated locations endpoint', async () => {
const { result } = renderHook(() => useLocationsForTenants({ tenantIds }), { wrapper });

await act(() => !result.current.isLoading);
await act(() => !result.current.isLoading);

expect(result.current.data).toEqual([
{ id: 'location-id' },
{ id: 'location-id' },
]);
expect(mockGet).not.toHaveBeenCalledWith('search/consortium/locations');
});

describe('when tenantIds is empty', () => {
it('should not make a request', () => {
renderHook(() => useLocationsForTenants({ tenantIds: [] }), { wrapper });

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

describe('when tenantIds is empty', () => {
it('should not make a request', () => {
renderHook(() => useLocationsForTenants({ tenantIds: [] }), { wrapper });
describe('when user is in a central tenant', () => {
beforeEach(() => {
checkIfUserInCentralTenant.mockClear().mockReturnValue(true);
});

it('should fetch locations of all tenants via consolidated endpoint', async () => {
const { result } = renderHook(() => useLocationsForTenants({ tenantIds }), { wrapper });

await act(() => !result.current.isLoading);

expect(mockGet).toHaveBeenCalledWith('search/consortium/locations');

expect(result.current.data).toEqual([
{ id: 'location-id' },
]);
});

it('should not call multiple locations endpoints', async () => {
const { result } = renderHook(() => useLocationsForTenants({ tenantIds }), { wrapper });

await act(() => !result.current.isLoading);

expect(mockGet).not.toHaveBeenCalled();
expect(mockGet).not.toHaveBeenCalledWith('locations');
});
});
});
4 changes: 4 additions & 0 deletions src/providers/DataProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const DataProvider = ({
const { data: locationsOfAllTenants } = useLocationsForTenants({ tenantIds });

useEffect(() => {
if (isUserInConsortiumMode(stripes)) {
return;
}

mutator.locations.GET({ tenant: stripes.okapi.tenant });
}, [stripes.okapi.tenant]);

Expand Down

0 comments on commit e3267b5

Please sign in to comment.