All files / hooks useCustomProperties.js

10.52% Statements 2/19
0% Branches 0/17
0% Functions 0/3
10.52% Lines 2/19

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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81              27x                                                                                                                           27x                      
import PropTypes from 'prop-types';
 
import { useQuery } from 'react-query';
import { useOkapiKy } from '@folio/stripes/core';
 
import { generateKiwtQuery } from '../utils';
 
const useCustomProperties = ({
  endpoint,
  ctx,
  nsValues = {
    sort: 'id'
  },
  options = {},
  queryParams,
  returnQueryObject = false,
}) => {
  const ky = useOkapiKy();
 
  const custPropOptions = {
    searchKey: 'label,name,description',
    filterKeys: {
      ContextKey: 'ctx'
    },
    sort: [
      { path: 'weight' },
      { path: 'label' }
    ],
    filters: [],
    stats: false,
    max: 100,
    ...options
  };
 
  if (Array.isArray(ctx)) {
    // If we have an array, append a context filter for each ctx given
 
    // Special case if one is isNull
    custPropOptions.filters.push({
      values: ctx.map(c => (c === 'isNull' ? 'ctx isNull' : `ctx==${c}`))
    });
  } else if (ctx === 'isNull') { // isNull is a special case
    custPropOptions.filters.push({
      value: 'ctx isNull'
    });
  } else if (ctx) {
    custPropOptions.filters.push({
      path: 'ctx',
      value: ctx
    });
  }
 
  const query = generateKiwtQuery(custPropOptions, nsValues);
  const path = `${endpoint}${query}`;
 
  const queryObject = useQuery(
    ['stripes-kint-components', 'useCustomProperties', 'custprops', ctx, path],
    () => ky(path).json(),
    queryParams
  );
 
  if (returnQueryObject) {
    return queryObject || {};
  }
 
  const { data: custprops } = queryObject;
  return custprops || [];
};
 
useCustomProperties.propTypes = {
  endpoint: PropTypes.string,
  ctx: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.arrayOf(PropTypes.string)
  ]),
  queryParams: PropTypes.object,
  returnQueryObject: PropTypes.bool
};
 
export default useCustomProperties;