Skip to content

Commit

Permalink
refactor: rename custom hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
alisher-epam committed Oct 28, 2024
1 parent e8abe63 commit cae8bfc
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export * from './useDefaultReceivingSearchSettings';
export * from './useEventEmitter';
export * from './useExchangeRateValue';
export * from './useFunds';
export * from './useHoldingsAndLocations';
export * from './useIdentifierTypes';
export * from './useInstanceHoldings';
export * from './useInstanceHoldingsQuery';
Expand All @@ -33,6 +32,7 @@ export * from './usePaneFocus';
export * from './useReceivingTenantIdsAndLocations';
export * from './useShowCallout';
export * from './useTags';
export * from './useTenantHoldingsAndLocations';
export * from './useToggle';
export * from './useTranslatedCategories';
export * from './useUser';
Expand Down
1 change: 0 additions & 1 deletion lib/hooks/useHoldingsAndLocations/index.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { useMemo } from 'react';

import { useCurrentUserTenants } from '../consortia';

/*
The purpose of this hook is to generate the list of unique tenantIds and locationIds
for the receiving tenant and locations when we need to fetch locations from other tenants via `useTenantHoldingsAndLocations`
*/
export const useReceivingTenantIdsAndLocations = ({
currentReceivingTenantId,
currentLocationId: locationId,
Expand Down
1 change: 1 addition & 0 deletions lib/hooks/useTenantHoldingsAndLocations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useTenantHoldingsAndLocations } from './useTenantHoldingsAndLocations';
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import {

import { LIMIT_MAX } from '../../constants';
import {
getHoldingLocations,
getHoldingLocationsByTenants,
getHoldingsAndLocations,
getHoldingsAndLocationsByTenants,
} from '../../utils';

const DEFAULT_DATA = [];

export const useHoldingsAndLocations = ({
/*
The purpose of this hook is to fetch holdings and locations for a given instanceId
and tenants when we need to fetch locations from other tenants when Central ordering is enabled.
*/
export const useTenantHoldingsAndLocations = ({
instanceId,
options = {},
tenantId,
Expand Down Expand Up @@ -63,8 +67,8 @@ export const useHoldingsAndLocations = ({
queryKey,
queryFn: ({ signal }) => {
return receivingTenantIds.length
? getHoldingLocationsByTenants({ ky, instanceId, receivingTenantIds, additionalTenantLocationIdsMap })
: getHoldingLocations({ ky, searchParams, signal, additionalLocationIds });
? getHoldingsAndLocationsByTenants({ ky, instanceId, receivingTenantIds, additionalTenantLocationIdsMap })
: getHoldingsAndLocations({ ky, searchParams, signal, additionalLocationIds });
},
enabled: enabled && Boolean(instanceId),
...queryOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useOkapiKy } from '@folio/stripes/core';

import { HOLDINGS_API } from '../../constants';
import { extendKyWithTenant } from '../../utils';
import { useHoldingsAndLocations } from './useHoldingsAndLocations';
import { useTenantHoldingsAndLocations } from './useTenantHoldingsAndLocations';

jest.mock('../../utils', () => ({
...jest.requireActual('../../utils'),
Expand Down Expand Up @@ -49,7 +49,7 @@ const getMock = jest.fn()
.mockReturnValueOnce({ json: () => Promise.resolve({ holdingsRecords }) })
.mockReturnValue({ json: () => Promise.resolve({ locations }) });

describe('useHoldingsAndLocations', () => {
describe('useTenantHoldingsAndLocations', () => {
beforeEach(() => {
useOkapiKy
.mockClear()
Expand All @@ -73,7 +73,7 @@ describe('useHoldingsAndLocations', () => {
});

it('should fetch holding locations', async () => {
const { result, waitFor } = renderHook(() => useHoldingsAndLocations({ instanceId: '1', tenantId: '2' }), { wrapper });
const { result, waitFor } = renderHook(() => useTenantHoldingsAndLocations({ instanceId: '1', tenantId: '2' }), { wrapper });

await waitFor(() => expect(result.current.isLoading).toBeFalsy());

Expand All @@ -82,7 +82,7 @@ describe('useHoldingsAndLocations', () => {

it('should fetch holding locations with different tenants', async () => {
const receivingTenantIds = ['1', '2'];
const { result, waitFor } = renderHook(() => useHoldingsAndLocations({ instanceId: '1', receivingTenantIds, tenantId: '2' }), { wrapper });
const { result, waitFor } = renderHook(() => useTenantHoldingsAndLocations({ instanceId: '1', receivingTenantIds, tenantId: '2' }), { wrapper });

await waitFor(() => expect(result.current.isLoading).toBeFalsy());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { extendKyWithTenant } from './extendKyWithTenant';

const DEFAULT_DATA = [];

export const getHoldingLocations = async ({
export const getHoldingsAndLocations = async ({
ky,
searchParams,
signal,
Expand Down Expand Up @@ -48,7 +48,7 @@ export const getHoldingLocations = async ({
};
};

export const getHoldingLocationsByTenants = async ({
export const getHoldingsAndLocationsByTenants = async ({
ky,
instanceId,
/*
Expand Down Expand Up @@ -77,7 +77,7 @@ export const getHoldingLocationsByTenants = async ({
const locationsRequest = receivingTenantIds.map(async (tenantId) => {
const tenantKy = extendKyWithTenant(ky, tenantId);

return getHoldingLocations({
return getHoldingsAndLocations({
ky: tenantKy,
searchParams,
tenantId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { HOLDINGS_API } from '../constants';
import { extendKyWithTenant } from './extendKyWithTenant';
import {
getHoldingLocations,
getHoldingLocationsByTenants,
} from './getHoldingLocations';
getHoldingsAndLocations,
getHoldingsAndLocationsByTenants,
} from './getTenantHoldingsAndLocations';

jest.mock('./extendKyWithTenant', () => ({
extendKyWithTenant: jest.fn(),
Expand Down Expand Up @@ -38,12 +38,12 @@ describe('utils', () => {
};
});

describe('getHoldingLocations', () => {
describe('getHoldingsAndLocations', () => {
it('should return holdings, locations and locationIds', async () => {
const searchParams = {};
const signal = { signal: 'signal' };

const result = await getHoldingLocations({ ky, searchParams, signal });
const result = await getHoldingsAndLocations({ ky, searchParams, signal });

expect(result).toEqual({
holdings: holdingsRecords,
Expand All @@ -57,7 +57,7 @@ describe('utils', () => {
const signal = { signal: 'signal' };
const tenantId = 'tenant-id';

const result = await getHoldingLocations({ ky, searchParams, signal, tenantId });
const result = await getHoldingsAndLocations({ ky, searchParams, signal, tenantId });

expect(result).toEqual({
holdings: holdingsRecords.map(holding => ({ ...holding, tenantId })),
Expand All @@ -67,7 +67,7 @@ describe('utils', () => {
});
});

describe('getHoldingLocationsByTenants', () => {
describe('getHoldingsAndLocationsByTenants', () => {
beforeEach(() => {
extendKyWithTenant.mockImplementation((tenantKy, tenantId) => {
return { ...tenantKy, tenantId };
Expand All @@ -78,7 +78,7 @@ describe('utils', () => {
const instanceId = 'instance-id';
const receivingTenantIds = ['tenant-id'];

const result = await getHoldingLocationsByTenants({ ky, instanceId, receivingTenantIds });
const result = await getHoldingsAndLocationsByTenants({ ky, instanceId, receivingTenantIds });

expect(result).toEqual({
holdings: holdingsRecords.map(holding => ({ ...holding, tenantId: receivingTenantIds[0] })),
Expand All @@ -90,7 +90,7 @@ describe('utils', () => {
it('should return empty array of holdings, locations and locationIds when `receivingTenantIds` and not present or empty array', async () => {
const instanceId = 'instance-id';

const result = await getHoldingLocationsByTenants({
const result = await getHoldingsAndLocationsByTenants({
ky,
instanceId,
});
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export * from './getConfigSetting';
export * from './getControlledVocabTranslations';
export * from './getErrorCodeFromResponse';
export * from './getFundsForSelect';
export * from './getHoldingLocations';
export * from './getTenantHoldingsAndLocations';
export * from './getLocationOptions';
export * from './getMoneyMultiplier';
export * from './getOrganizationsOptions';
Expand Down

0 comments on commit cae8bfc

Please sign in to comment.