-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UISACQCOMP-228 Add more reusable hooks and utilities
- Loading branch information
1 parent
a05ef6c
commit f97d602
Showing
9 changed files
with
82 additions
and
3 deletions.
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
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 { useAddresses } from './useAddresses'; |
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,57 @@ | ||
import { useQuery } from 'react-query'; | ||
|
||
import { | ||
useNamespace, | ||
useOkapiKy, | ||
} from '@folio/stripes/core'; | ||
|
||
import { | ||
CONFIG_API, | ||
LIMIT_MAX, | ||
} from '../../constants'; | ||
import { getAddresses } from '../../utils'; | ||
|
||
const MODULE_TENANT = 'TENANT'; | ||
const CONFIG_ADDRESSES = 'tenant.addresses'; | ||
const DEFAULT_DATA = []; | ||
|
||
export const useAddresses = (options = {}) => { | ||
const { | ||
enabled = true, | ||
tenantId, | ||
...queryOptions | ||
} = options; | ||
|
||
const [namespace] = useNamespace({ key: 'acquisitions-units' }); | ||
const ky = useOkapiKy({ tenant: tenantId }); | ||
|
||
const searchParams = { | ||
query: `(module=${MODULE_TENANT} and configName=${CONFIG_ADDRESSES})`, | ||
limit: LIMIT_MAX, | ||
}; | ||
|
||
const { | ||
data, | ||
isFetching, | ||
isLoading, | ||
} = useQuery({ | ||
queryKey: [namespace, tenantId], | ||
queryFn: ({ signal }) => { | ||
return ky.get(CONFIG_API, { searchParams, signal }) | ||
.json() | ||
.then(({ configs, totalRecords }) => ({ | ||
addresses: getAddresses(configs), | ||
totalRecords, | ||
})); | ||
}, | ||
enabled, | ||
...queryOptions, | ||
}); | ||
|
||
return ({ | ||
addresses: data?.addresses || DEFAULT_DATA, | ||
totalRecords: data?.totalRecords || 0, | ||
isFetching, | ||
isLoading, | ||
}); | ||
}; |
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,8 @@ | ||
import sortBy from 'lodash/sortBy'; | ||
|
||
export const getAddressOptions = (addresses = []) => sortBy( | ||
addresses.map(address => ({ | ||
value: address.id, | ||
label: address.name, | ||
})), 'label', | ||
); |
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