All files / Settings/hooks/useAppSettings useAppSettings.js

5% Statements 1/20
0% Branches 0/25
0% Functions 0/2
5% Lines 1/20

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        27x                                                                                                                                  
import { useQuery } from 'react-query';
import { useOkapiKy } from '@folio/stripes/core';
import { generateKiwtQueryParams } from '../../../utils';
 
const useAppSettings = ({
  endpoint,
  keyName,
  options,
  returnQueryObject = false,
  sectionName,
  queryParams
}) => {
  const ky = useOkapiKy();
 
  const defaultQueryParamObject = {
    perPage: 100,
    stats: false,
  };
 
  const namespaceArray = [endpoint];
 
  if (sectionName) {
    defaultQueryParamObject.filters = [
      ...(defaultQueryParamObject.filters ?? []),
      {
        path: 'section',
        value: sectionName
      }
    ];
 
    namespaceArray.push(sectionName);
  }
 
  if (keyName) {
    defaultQueryParamObject.filters = [
      ...(defaultQueryParamObject.filters ?? []),
      {
        path: 'key',
        value: keyName
      }
    ];
 
    defaultQueryParamObject.perPage = 1;
 
    namespaceArray.push(keyName);
  }
 
  const queryParamArray = generateKiwtQueryParams(options ?? defaultQueryParamObject, {});
  namespaceArray.push(queryParamArray);
 
  const queryObject = useQuery(
    namespaceArray,
    () => ky(`${endpoint}?${queryParamArray?.join('&')}`).json(),
    queryParams
  );
 
 
  if (returnQueryObject) {
    return queryObject;
  }
 
  if ((queryParams ?? defaultQueryParamObject)?.perPage && (queryParams ?? defaultQueryParamObject)?.perPage === 1) {
    return queryObject?.data?.[0] ?? {};
  }
 
  return queryObject?.data ?? [];
};
 
export default useAppSettings;