Skip to content

Commit

Permalink
UISACQCOMP-228 Add more reusable hooks and utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
usavkov-epam committed Nov 7, 2024
1 parent a05ef6c commit f97d602
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion lib/AcqList/utils/queryUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
4 changes: 3 additions & 1 deletion lib/CustomFieldsFilters/CustomFieldsFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -85,6 +86,7 @@ CustomFieldsFilter.propTypes = {
customField: PropTypes.object,
disabled: PropTypes.bool,
onChange: PropTypes.func.isRequired,
name: PropTypes.string,
};

CustomFieldsFilter.defaultProps = {
Expand Down
5 changes: 4 additions & 1 deletion lib/CustomFieldsFilters/CustomFieldsFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ export const CustomFieldsFilters = ({
activeFilters,
customFields,
onChange,
name,
...props
}) => {
if (!customFields) return null;

return customFields.map((customField) => (
<CustomFieldsFilter
activeFilters={activeFilters[`${CUSTOM_FIELDS_FILTER}.${customField.refId}`]}
name={name}
activeFilters={activeFilters[`${name || CUSTOM_FIELDS_FILTER}.${customField.refId}`]}
customField={customField}
key={`custom-field-${customField.id}`}
onChange={onChange}
Expand All @@ -28,4 +30,5 @@ CustomFieldsFilters.propTypes = {
customFields: PropTypes.arrayOf(PropTypes.object),
disabled: PropTypes.bool,
onChange: PropTypes.func.isRequired,
name: PropTypes.string,
};
1 change: 1 addition & 0 deletions lib/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './useAccordionToggle';
export * from './useAcqMethodsOptions';
export * from './useAcquisitionMethods';
export * from './useAcquisitionUnits';
export * from './useAddresses';
export * from './useAsyncDebounce';
export * from './useCampuses';
export * from './useCampusesQuery';
Expand Down
1 change: 1 addition & 0 deletions lib/hooks/useAddresses/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useAddresses } from './useAddresses';
57 changes: 57 additions & 0 deletions lib/hooks/useAddresses/useAddresses.js
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,
});
};
8 changes: 8 additions & 0 deletions lib/utils/getAddressOptions.js
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',
);
1 change: 1 addition & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit f97d602

Please sign in to comment.