From f97d6026213678f2c2785600c859b24c66578176 Mon Sep 17 00:00:00 2001 From: Yury Saukou Date: Thu, 7 Nov 2024 15:26:25 +0400 Subject: [PATCH] UISACQCOMP-228 Add more reusable hooks and utilities --- CHANGELOG.md | 2 + lib/AcqList/utils/queryUtils.js | 6 +- lib/CustomFieldsFilters/CustomFieldsFilter.js | 4 +- .../CustomFieldsFilters.js | 5 +- lib/hooks/index.js | 1 + lib/hooks/useAddresses/index.js | 1 + lib/hooks/useAddresses/useAddresses.js | 57 +++++++++++++++++++ lib/utils/getAddressOptions.js | 8 +++ lib/utils/index.js | 1 + 9 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 lib/hooks/useAddresses/index.js create mode 100644 lib/hooks/useAddresses/useAddresses.js create mode 100644 lib/utils/getAddressOptions.js diff --git a/CHANGELOG.md b/CHANGELOG.md index c7033e01..7062b458 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## (6.1.0 IN PROGRESS) +* Add more reusable hooks and utilities. Refs UISACQCOMP-228. + ## [6.0.0](https://github.com/folio-org/stripes-acq-components/tree/v6.0.0) (2024-10-29) [Full Changelog](https://github.com/folio-org/stripes-acq-components/compare/v5.1.2...v6.0.0) diff --git a/lib/AcqList/utils/queryUtils.js b/lib/AcqList/utils/queryUtils.js index 9118c14f..add04626 100644 --- a/lib/AcqList/utils/queryUtils.js +++ b/lib/AcqList/utils/queryUtils.js @@ -60,7 +60,11 @@ export const getFilterParams = (queryParams) => omit( ); export const getFiltersCount = (filters) => { - return filters && Object.keys(getFilterParams(filters)).filter(k => filters[k] !== undefined).length; + return filters && Object.keys(getFilterParams(filters)).filter((k) => { + return k === SEARCH_PARAMETER + ? Boolean(filters[k]) + : filters[k] !== undefined; + }).length; }; export const buildFilterQuery = ( diff --git a/lib/CustomFieldsFilters/CustomFieldsFilter.js b/lib/CustomFieldsFilters/CustomFieldsFilter.js index 21634dc4..84658689 100644 --- a/lib/CustomFieldsFilters/CustomFieldsFilter.js +++ b/lib/CustomFieldsFilters/CustomFieldsFilter.js @@ -26,11 +26,12 @@ const CustomFieldsFilter = ({ customField, disabled, onChange, + name: filterNameProp, }) => { const FilterComponent = customFieldTypeToFilterMap[customField.type]; const { refId, name, selectField } = customField; const values = selectField?.options?.values ?? [{ id: 'true', value: name }]; - const filterName = `${CUSTOM_FIELDS_FILTER}.${refId}`; + const filterName = `${filterNameProp || CUSTOM_FIELDS_FILTER}.${refId}`; const dataOptions = values.map(({ id: value, value: label }) => ({ label, value, @@ -85,6 +86,7 @@ CustomFieldsFilter.propTypes = { customField: PropTypes.object, disabled: PropTypes.bool, onChange: PropTypes.func.isRequired, + name: PropTypes.string, }; CustomFieldsFilter.defaultProps = { diff --git a/lib/CustomFieldsFilters/CustomFieldsFilters.js b/lib/CustomFieldsFilters/CustomFieldsFilters.js index 72ddb8bb..c6779613 100644 --- a/lib/CustomFieldsFilters/CustomFieldsFilters.js +++ b/lib/CustomFieldsFilters/CustomFieldsFilters.js @@ -7,13 +7,15 @@ export const CustomFieldsFilters = ({ activeFilters, customFields, onChange, + name, ...props }) => { if (!customFields) return null; return customFields.map((customField) => ( { + 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, + }); +}; diff --git a/lib/utils/getAddressOptions.js b/lib/utils/getAddressOptions.js new file mode 100644 index 00000000..6abba119 --- /dev/null +++ b/lib/utils/getAddressOptions.js @@ -0,0 +1,8 @@ +import sortBy from 'lodash/sortBy'; + +export const getAddressOptions = (addresses = []) => sortBy( + addresses.map(address => ({ + value: address.id, + label: address.name, + })), 'label', +); diff --git a/lib/utils/index.js b/lib/utils/index.js index efd2c9a3..2ba33652 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -16,6 +16,7 @@ export * from './formatDateTime'; export * from './generateQueryTemplate'; export * from './getAcqUnitsOptions'; export * from './getAddresses'; +export * from './getAddressOptions'; export * from './getAmountWithCurrency'; export * from './getConfigSetting'; export * from './getControlledVocabTranslations';