All files / CustomProperties/Edit CustomPropertiesEditCtx.js

63.63% Statements 7/11
22.22% Branches 2/9
50% Functions 1/2
63.63% Lines 7/11

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 82 83 84 85 86 87 88 89            26x                 1x     1x                                                 1x                       1x 1x                                         26x                      
import PropTypes from 'prop-types';
import { Accordion } from '@folio/stripes/components';
 
import CustomPropertiesListField from './CustomPropertiesListField';
import { useCustProps, useKintIntl } from '../../hooks';
 
const CustomPropertiesEditCtx = ({
  ctx,
  customPropertiesEndpoint,
  id,
  intlKey: passedIntlKey,
  intlNS: passedIntlNS,
  labelOverrides = {},
  nameOverride
}) => {
  const kintIntl = useKintIntl(passedIntlKey, passedIntlNS);
 
  // Deal with all the possible label override options
  const getAccordionLabel = () => {
    // Special case for null context
    if (ctx === 'isNull') {
      return (
        kintIntl.formatKintMessage({
          id: 'customProperties',
          overrideValue: labelOverrides.noContext
        })
      );
    }
 
    // Chain formatKintMessages together using provided fallbackMessage to
    // allow for "If override or translation exists, use it, else use default"
    return (
      kintIntl.formatKintMessage({
        id: `customProperties.ctx.${ctx}`,
        overrideValue: labelOverrides[ctx],
        fallbackMessage: kintIntl.formatKintMessage({
          id: 'customProperties.defaultTitle',
          overrideValue: labelOverrides.defaultTitle
        }, { ctx })
      }, { ctx })
    );
  };
 
  const { custprops, isLoading } = useCustProps({
    ctx,
    endpoint: customPropertiesEndpoint,
    options: {
      filters: [{
        path: 'retired',
        comparator: '!=',
        value: 'true'
      }]
    }
  });
 
  Eif (isLoading) {
    return null;
  }
 
  return (
    custprops.length > 0 &&
    <Accordion
      id={`${id}-accordion-${ctx}`}
      label={getAccordionLabel()}
    >
      <CustomPropertiesListField
        ctx={ctx}
        customProperties={custprops}
        intlKey={passedIntlKey}
        intlNS={passedIntlNS}
        labelOverrides={labelOverrides}
        name={nameOverride ?? 'customProperties'}
      />
    </Accordion>
  );
};
 
CustomPropertiesEditCtx.propTypes = {
  ctx: PropTypes.string,
  customPropertiesEndpoint: PropTypes.string,
  id: PropTypes.string,
  intlKey: PropTypes.string,
  intlNS: PropTypes.string,
  labelOverrides: PropTypes.object,
  nameOverride: PropTypes.string
};
 
export default CustomPropertiesEditCtx;