-
Notifications
You must be signed in to change notification settings - Fork 3
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
UISACQCOMP-223: ECS - Add reusable custom hooks to fix invalid reference issues related to holding names and locations #824
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
29f1b36
UISACQCOMP-223: ECS - Add reusable custom hooks to fix invalid refere…
alisher-epam aa686c7
Merge branch 'master' into UISACQCOMP-223
alisher-epam fe3f9c4
test: fix failing tests
alisher-epam fc8fe8b
test: add test coverage
alisher-epam e8abe63
fix hook key
alisher-epam cae8bfc
refactor: rename custom hooks
alisher-epam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useHoldingsAndLocations } from './useHoldingsAndLocations'; |
80 changes: 80 additions & 0 deletions
80
lib/hooks/useHoldingsAndLocations/useHoldingsAndLocations.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { useQuery } from 'react-query'; | ||
|
||
import { | ||
useNamespace, | ||
useOkapiKy, | ||
} from '@folio/stripes/core'; | ||
|
||
import { LIMIT_MAX } from '../../constants'; | ||
import { | ||
getHoldingLocations, | ||
getHoldingLocationsByTenants, | ||
} from '../../utils'; | ||
|
||
const DEFAULT_DATA = []; | ||
|
||
export const useHoldingsAndLocations = ({ | ||
instanceId, | ||
options = {}, | ||
tenantId, | ||
/* | ||
`receivingTenantIds` is a unique list of tenantIds from the pieces list. | ||
The purpose is that we need to be able to fetch locations from other | ||
tenants so that we can display all the locations on the full-screen page | ||
*/ | ||
receivingTenantIds = DEFAULT_DATA, | ||
/* | ||
ECS mode: | ||
`additionalTenantLocationIdsMap` is a map of tenantId to locationIds for ECS mode. | ||
The format can be: { tenantId: [locationId1, locationId2] } | ||
The purpose is that we need to fetch newly added locations when we select a location | ||
from "Create new holdings for location" modal so that the value is displayed in the selection | ||
*/ | ||
additionalTenantLocationIdsMap = {}, | ||
/* | ||
Non-ECS mode: | ||
`additionalLocationIds` is a list of locationIds for the case when we need to fetch additional | ||
locations for the selected location in the form so that the value is displayed in the selection. | ||
*/ | ||
additionalLocationIds = [], | ||
}) => { | ||
const { enabled = true, ...queryOptions } = options; | ||
|
||
const ky = useOkapiKy({ tenant: tenantId }); | ||
const [namespace] = useNamespace({ key: 'holdings-and-location' }); | ||
const queryKey = [ | ||
namespace, | ||
tenantId, | ||
instanceId, | ||
...receivingTenantIds, | ||
...additionalLocationIds, | ||
...Object.values(additionalTenantLocationIdsMap), | ||
]; | ||
const searchParams = { | ||
query: `instanceId==${instanceId}`, | ||
limit: LIMIT_MAX, | ||
}; | ||
|
||
const { | ||
data, | ||
isFetching, | ||
isLoading, | ||
} = useQuery({ | ||
queryKey, | ||
queryFn: ({ signal }) => { | ||
return receivingTenantIds.length | ||
? getHoldingLocationsByTenants({ ky, instanceId, receivingTenantIds, additionalTenantLocationIdsMap }) | ||
: getHoldingLocations({ ky, searchParams, signal, additionalLocationIds }); | ||
}, | ||
enabled: enabled && Boolean(instanceId), | ||
...queryOptions, | ||
}); | ||
|
||
return ({ | ||
holdings: data?.holdings || DEFAULT_DATA, | ||
locations: data?.locations || DEFAULT_DATA, | ||
locationIds: data?.locationIds || DEFAULT_DATA, | ||
isFetching, | ||
isLoading, | ||
}); | ||
}; |
91 changes: 91 additions & 0 deletions
91
lib/hooks/useHoldingsAndLocations/useHoldingsAndLocations.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { | ||
QueryClient, | ||
QueryClientProvider, | ||
} from 'react-query'; | ||
|
||
import { | ||
renderHook, | ||
} from '@testing-library/react-hooks'; | ||
import { useOkapiKy } from '@folio/stripes/core'; | ||
|
||
import { HOLDINGS_API } from '../../constants'; | ||
import { extendKyWithTenant } from '../../utils'; | ||
import { useHoldingsAndLocations } from './useHoldingsAndLocations'; | ||
|
||
jest.mock('../../utils', () => ({ | ||
...jest.requireActual('../../utils'), | ||
extendKyWithTenant: jest.fn().mockReturnValue({ extend: jest.fn() }), | ||
})); | ||
|
||
const queryClient = new QueryClient(); | ||
const wrapper = ({ children }) => ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
); | ||
|
||
const holdingsRecords = [ | ||
{ | ||
'id': 'd7e99892-6d7d-46eb-8a4c-3aa471785819', | ||
'instanceId': '8804aeeb-db18-4c42-b0e9-28d63c7855e6', | ||
'permanentLocationId': '9d1b77e8-f02e-4b7f-b296-3f2042ddac54', | ||
}, | ||
{ | ||
'id': '5636949f-8f97-4b25-a0fc-90fdb050ffb0', | ||
'instanceId': '8804aeeb-db18-4c42-b0e9-28d63c7855e6', | ||
'permanentLocationId': '9d1b77e8-f02e-4b7f-b296-3f2042ddac54', | ||
}, | ||
]; | ||
|
||
const locations = [ | ||
{ | ||
'id': '9d1b77e8-f02e-4b7f-b296-3f2042ddac54', | ||
'name': 'DCB', | ||
'code': '000', | ||
}, | ||
]; | ||
|
||
const getMock = jest.fn() | ||
.mockReturnValueOnce({ json: () => Promise.resolve({ holdingsRecords }) }) | ||
.mockReturnValue({ json: () => Promise.resolve({ locations }) }); | ||
|
||
describe('useHoldingsAndLocations', () => { | ||
beforeEach(() => { | ||
useOkapiKy | ||
.mockClear() | ||
.mockReturnValue({ | ||
get: getMock, | ||
extend: jest.fn(() => ({ | ||
get: jest.fn((path) => { | ||
if (path === HOLDINGS_API) { | ||
return { | ||
json: jest.fn().mockResolvedValue({ holdingsRecords }), | ||
}; | ||
} | ||
|
||
return ({ | ||
json: jest.fn().mockResolvedValue({ locations }), | ||
}); | ||
}), | ||
})), | ||
}); | ||
extendKyWithTenant.mockClear().mockReturnValue({ extend: jest.fn() }); | ||
}); | ||
|
||
it('should fetch holding locations', async () => { | ||
const { result, waitFor } = renderHook(() => useHoldingsAndLocations({ instanceId: '1', tenantId: '2' }), { wrapper }); | ||
|
||
await waitFor(() => expect(result.current.isLoading).toBeFalsy()); | ||
|
||
expect(result.current.locations).toEqual(locations); | ||
}); | ||
|
||
it('should fetch holding locations with different tenants', async () => { | ||
const receivingTenantIds = ['1', '2']; | ||
const { result, waitFor } = renderHook(() => useHoldingsAndLocations({ instanceId: '1', receivingTenantIds, tenantId: '2' }), { wrapper }); | ||
|
||
await waitFor(() => expect(result.current.isLoading).toBeFalsy()); | ||
|
||
expect(result.current.locations).toHaveLength(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './useReceivingTenantIdsAndLocations'; |
43 changes: 43 additions & 0 deletions
43
lib/hooks/useReceivingTenantIdsAndLocations/useReceivingTenantIdsAndLocations.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import uniq from 'lodash/uniq'; | ||
import { useMemo } from 'react'; | ||
|
||
import { useCurrentUserTenants } from '../consortia'; | ||
|
||
export const useReceivingTenantIdsAndLocations = ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please leave the purpose of this hook in the comments? |
||
currentReceivingTenantId, | ||
currentLocationId: locationId, | ||
receivingTenantIds = [], | ||
}) => { | ||
const currentUserTenants = useCurrentUserTenants(); | ||
|
||
const receivingTenants = useMemo(() => { | ||
if (receivingTenantIds.length) { | ||
const currentUserTenantIds = currentUserTenants?.map(({ id: tenantId }) => tenantId); | ||
|
||
// should get unique tenantIds from poLine locations and filter out tenantIds where the current user has assigned | ||
return uniq([ | ||
...receivingTenantIds, | ||
currentReceivingTenantId, | ||
].filter((tenantId) => currentUserTenantIds?.includes(tenantId)) | ||
.filter(Boolean)); | ||
} | ||
|
||
return []; | ||
}, [receivingTenantIds, currentUserTenants, currentReceivingTenantId]); | ||
|
||
const additionalLocations = useMemo(() => { | ||
const locationIds = locationId ? [locationId] : []; | ||
const tenantLocationIdsMap = currentReceivingTenantId ? { [currentReceivingTenantId]: locationIds } : {}; | ||
|
||
return { | ||
additionalLocationIds: locationIds, | ||
additionalTenantLocationIdsMap: tenantLocationIdsMap, | ||
}; | ||
}, [locationId, currentReceivingTenantId]); | ||
|
||
return { | ||
receivingTenantIds: receivingTenants, | ||
tenantId: currentReceivingTenantId, | ||
...additionalLocations, | ||
}; | ||
}; |
33 changes: 33 additions & 0 deletions
33
lib/hooks/useReceivingTenantIdsAndLocations/useReceivingTenantIdsAndLocations.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
|
||
import { useReceivingTenantIdsAndLocations } from './useReceivingTenantIdsAndLocations'; | ||
|
||
jest.mock('../consortia', () => ({ | ||
useCurrentUserTenants: jest.fn(() => [{ id: 'central' }, { id: 'college' }]), | ||
})); | ||
|
||
describe('useReceivingTenantIdsAndLocations', () => { | ||
it('should return receivingTenantIds', () => { | ||
const tenants = ['central', 'college']; | ||
const { result } = renderHook(() => useReceivingTenantIdsAndLocations({ | ||
receivingTenantIds: tenants, | ||
currentReceivingTenantId: 'central', | ||
})); | ||
|
||
expect(result.current.receivingTenantIds).toEqual(tenants); | ||
}); | ||
|
||
it('should return tenantId', () => { | ||
const currentReceivingTenantId = 'central'; | ||
|
||
const { result } = renderHook(() => useReceivingTenantIdsAndLocations({ currentReceivingTenantId })); | ||
|
||
expect(result.current.tenantId).toBe(currentReceivingTenantId); | ||
}); | ||
|
||
it('should return additionalLocationIds', () => { | ||
const { result } = renderHook(() => useReceivingTenantIdsAndLocations({})); | ||
|
||
expect(result.current.additionalLocationIds).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { OKAPI_TENANT_HEADER } from '../constants'; | ||
|
||
export const extendKyWithTenant = (ky, tenantId) => { | ||
return ky.extend({ | ||
hooks: { | ||
beforeRequest: [ | ||
request => { | ||
request.headers.set(OKAPI_TENANT_HEADER, tenantId); | ||
}, | ||
], | ||
}, | ||
}); | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hook's name is too general given that it requires many specific arguments.