All files / CustomProperties/View CustomPropertiesViewCtx.js

47.36% Statements 9/19
12.5% Branches 2/16
33.33% Functions 2/6
50% Lines 9/18

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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120              26x                 1x     1x   1x 1x                                           1x                         1x 1x                                                                                                   26x                      
import PropTypes from 'prop-types';
import { Accordion, Badge, Spinner } from '@folio/stripes/components';
 
import { useCustProps, useKintIntl } from '../../hooks';
 
import CustomPropertyCard from './CustomPropertyCard';
 
const CustomPropertiesViewCtx = ({
  ctx,
  customProperties = [],
  customPropertiesEndpoint,
  id,
  intlKey: passedIntlKey,
  intlNS: passedIntlNS,
  labelOverrides = {}
}) => {
  const kintIntl = useKintIntl(passedIntlKey, passedIntlNS);
 
  // Deal with all the possible label override options
  const getAccordionLabel = () => {
    // Special case for null context
    Eif (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: {
      sort: [
        { path: 'retired' }, // Place retired custprops at the end
        { path: 'primary', direction: 'desc' }, // Primary properties should display before optional
        { path: 'weight' }, // Within those groups, sort by weight
        { path: 'label' } // For those with the same weight, sort by label
      ]
    }
  });
 
  Eif (isLoading) {
    return (
      <Accordion
        closedByDefault
        displayWhenClosed={<Spinner />}
        displayWhenOpen={<Spinner />}
        id={`${id}-accordion-${ctx}`}
        label={getAccordionLabel()}
      >
        <Spinner />
      </Accordion>
    );
  }
 
  const countSetProperties = (primary) => (
    Object.entries(customProperties)?.filter(
      ([_key, cp]) => (
        (cp[0]?.type?.ctx === ctx || (ctx === 'isNull' && !cp[0]?.type?.ctx)) && // Either the ctx string matches, or isNull matches blank
        cp[0]?.type?.primary === primary // Only count non-primary set custprops
      )
    )?.length ?? 0
  );
 
  // We need to display any set properties, along with any non-set primary properties
  const primaryCount = custprops?.filter(cp => cp.primary === true)?.length ?? 0;
  const optionalCount = countSetProperties(false);
  const setPrimaryCount = countSetProperties(true);
 
  return (
    (primaryCount + optionalCount) > 0 &&
    <Accordion
      closedByDefault
      displayWhenClosed={<Badge>{setPrimaryCount + optionalCount}</Badge>}
      displayWhenOpen={<Badge>{setPrimaryCount + optionalCount}</Badge>}
      id={`${id}-accordion-${ctx}`}
      label={getAccordionLabel()}
    >
      {custprops.map((cp, index) => (
        <CustomPropertyCard
          key={`custom-property-card-${ctx}[${index}]`}
          ctx={ctx}
          customProperty={customProperties?.[cp.name]?.[0]}
          customPropertyDefinition={cp}
          index={index}
          labelOverrides={labelOverrides}
        />
      ))}
    </Accordion>
  );
};
 
CustomPropertiesViewCtx.propTypes = {
  ctx: PropTypes.string,
  customProperties: PropTypes.object,
  customPropertiesEndpoint: PropTypes.string,
  id: PropTypes.string,
  intlKey: PropTypes.string,
  intlNS: PropTypes.string,
  labelOverrides: PropTypes.object,
};
 
export default CustomPropertiesViewCtx;