All files / CustomProperties/Config CustomPropertiesLookup.js

52.63% Statements 10/19
40% Branches 2/5
22.22% Functions 2/9
52.63% Lines 10/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 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194                      26x                   2x     2x   2x                                 2x           2x   2x   2x               2x                                                                                                                                                                                                                                 26x                                      
import { useState } from 'react';
import PropTypes from 'prop-types';
 
import { Col, MultiColumnList, Row, Spinner, Select, Button } from '@folio/stripes/components';
 
import { Form } from 'react-final-form';
import SearchField from '../../SearchField';
import { useCustProps, useKintIntl } from '../../hooks';
 
import css from '../../../../styles/CustomProperties.css';
 
const CustomPropertiesLookup = ({
  contextFilterOptions, // expects an array of the form [{value: "OpenAccess", label: "Open Access"}, {value: false, label: "None"}]
  customPropertiesEndpoint: endpoint,
  intlKey: passedIntlKey,
  intlNS: passedIntlNS,
  labelOverrides = {},
  mclProps,
  onSelectCustomProperty,
  queryParams
}) => {
  const [nsValues, setNsValues] = useState({
    sort: 'label'
  });
  const [selectedContext, setSelectedContext] = useState();
 
  const { custprops, isLoading } = useCustProps({
    endpoint,
    nsValues,
    queryParams,
    ctx: selectedContext,
    options: {
      sort: [
        {
          path: 'ctx'
        },
        {
          path: 'weight'
        }
      ]
    }
  });
 
  const handleSearch = (query) => {
    setNsValues({
      ...nsValues,
      query,
    });
  };
  const [searchTerm, setSearchTerm] = useState('');
 
  const kintIntl = useKintIntl(passedIntlKey, passedIntlNS);
 
  return (
    <>
      <Form
        enableReinitialize
        keepDirtyOnReinitialize
        onSubmit={() => handleSearch(searchTerm)}
      >
        {({ handleSubmit }) => (
          <form
            className={css.lookupSearchContainer}
            onSubmit={handleSubmit}
          >
            <SearchField
              ariaLabel={
                kintIntl.formatKintMessage({
                  id: 'customProperties.config.searchAriaLabel',
                  overrideValue: labelOverrides.searchAriaLabel,
                  fallbackMessage: 'custom-property-search-field'
                })
              }
              className={css.lookupSearch}
              marginBottom0
              onChange={e => setSearchTerm(e.target.value)}
              value={searchTerm}
            />
            <Button
              buttonClass={css.lookupSearchButton}
              buttonStyle="primary"
              marginBottom0
              type="submit"
            >
              {kintIntl.formatKintMessage({
                id: 'search',
                overrideValue: labelOverrides.search
              })}
            </Button>
          </form>
        )}
      </Form>
      <Row>
        <Col xs={6}>
          <Select
            dataOptions={contextFilterOptions}
            label={
              kintIntl.formatKintMessage({
                id: 'customProperties.ctx',
                overrideValue: labelOverrides.ctx
              })
            }
            onChange={(e) => {
              setSelectedContext(e.target.value);
            }}
            value={selectedContext}
          />
        </Col>
      </Row>
      {isLoading ?
        <Spinner /> :
        <MultiColumnList
          columnMapping={{
            'label': kintIntl.formatKintMessage({
              id: 'customProperties.label',
              overrideValue: labelOverrides.label
            }),
            'primary': kintIntl.formatKintMessage({
              id: 'customProperties.primary',
              overrideValue: labelOverrides.primary
            }),
            'ctx': kintIntl.formatKintMessage({
              id: 'customProperties.ctx',
              overrideValue: labelOverrides.ctx
            }),
            'weight': kintIntl.formatKintMessage({
              id: 'customProperties.weight',
              overrideValue: labelOverrides.weight
            }),
            'type': kintIntl.formatKintMessage({
              id: 'customProperties.type',
              overrideValue: labelOverrides.type
            }),
            'category': kintIntl.formatKintMessage({
              id: 'customProperties.category',
              overrideValue: labelOverrides.category
            }),
          }}
          contentData={custprops}
          formatter={{
            primary: data => {
              if (data?.primary) {
                return (
                  kintIntl.formatKintMessage({
                    id: 'yes',
                    overrideValue: labelOverrides.yes
                  })
                );
              } else {
                return (
                  kintIntl.formatKintMessage({
                    id: 'no',
                    overrideValue: labelOverrides.no
                  })
                );
              }
            },
            type: data => (
              kintIntl.formatKintMessage({
                id: `customProperties.type.${data?.type}`,
                overrideValue: labelOverrides?.[data?.type]
              })
            ),
            category: data => data?.category?.desc
          }}
          onRowClick={onSelectCustomProperty}
          visibleColumns={['label', 'primary', 'ctx', 'weight', 'type', 'category']}
          {...mclProps}
        />
      }
    </>
  );
};
 
CustomPropertiesLookup.propTypes = {
  contextFilterOptions: PropTypes.arrayOf(PropTypes.shape({
    value: PropTypes.string,
    label: PropTypes.oneOfType([
      PropTypes.element,
      PropTypes.string
    ])
  })),
  customPropertiesEndpoint: PropTypes.string,
  intlKey: PropTypes.string,
  intlNS: PropTypes.string,
  labelOverrides: PropTypes.object,
  mclProps: PropTypes.object,
  onSelectCustomProperty: PropTypes.func,
  queryParams: PropTypes.object,
  refdataEndpoint: PropTypes.string,
};
 
export default CustomPropertiesLookup;