All files / hooks useRefdata.js

11.76% Statements 2/17
0% Branches 0/12
0% Functions 0/3
12.5% Lines 2/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61              27x                                                                                   27x                      
import PropTypes from 'prop-types';
 
import { useQuery } from 'react-query';
import { useOkapiKy } from '@folio/stripes/core';
 
import { generateKiwtQuery, refdataOptions, refdataQueryKey } from '../utils';
 
const useRefdata = ({
  endpoint,
  desc,
  options = refdataOptions,
  queryParams,
  returnQueryObject = false,
}) => {
  const ky = useOkapiKy();
 
  const nsValues = {};
 
  /* Desc will tend to contain a '.', which will throw off generateKiwtQuery.
   * To combat this we can insert another Desc. at the beginning to act as the filtername, so
   * Funder.Name => Desc.desc=Funder.Name. This way "Desc" acts as the filterName, and without a filterKey
   * "desc=Funder.Name" is passed wholesale to the query.
   */
 
  if (Array.isArray(desc)) {
    // If we have an array, append a desc filter for each desc given
    nsValues.filters = desc.map(d => `DescKey.${d}`).join(',');
  } else if (desc) {
    // If we just have a string, append a single desc filter
    nsValues.filters = `DescKey.${desc}`;
  }
 
  const query = generateKiwtQuery(options, nsValues);
  const path = `${endpoint}${query}`;
 
  const queryObject = useQuery(
    refdataQueryKey(desc),
    () => ky(path).json(),
    queryParams
  );
 
  if (returnQueryObject) {
    return queryObject || {};
  }
 
  const { data: refdata } = queryObject;
  return refdata || [];
};
 
useRefdata.propTypes = {
  endpoint: PropTypes.string,
  desc: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.arrayOf(PropTypes.string)
  ]),
  queryParams: PropTypes.object,
  returnQueryObject: PropTypes.bool
};
 
export default useRefdata;