All files / Settings/SettingsFormContainer SettingsFormContainer.js

11.76% Statements 2/17
0% Branches 0/5
0% Functions 0/4
11.76% Lines 2/17

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                      27x                                                                                                                                                                             27x                                
import PropTypes from 'prop-types';
 
import { useCallout } from '@folio/stripes/core';
import { useKintIntl, useModConfigEntries, useMutateModConfigEntry } from '../../hooks';
 
/*
 * API is similar to ConfigManager
 *
 * DEPRECATED -- As far as I can see this is now unused anyway
 * KInt modules are swapping to AppSettings instead of mod-settings, so no centralised components should be needed
 */
const SettingsFormContainer = ({
  ConfigFormComponent,
  configName,
  getInitialValues: passedGetInitialValues,
  intlKey: passedIntlKey,
  intlNS: passedIntlNS,
  labelOverrides = {},
  moduleName,
  scope, // We don't use this currently but it's here for future reference
  ...rest
}) => {
  // eslint-disable-next-line no-console
  console.warn('SettingsFormContainer is deprecated, as it utilises mod-config');
 
  const callout = useCallout();
  const kintIntl = useKintIntl(passedIntlKey, passedIntlNS);
 
/*
  * It is important to pass a namespaceAppend because the same hook could be rendered
  * on the main App component. This means that in settings
  * (Which is rendered inside App itself, stripes behaviour)
  * the same query namespace would apply to the query in the config form, and also to
  * the App level hook call, resulting in an infinite loop.
  *
  * We avoid this by ensuring all calls from this component have (at least) an extra namespace key item
  */
  const {
    parsedSettings,
    settings
  } = useModConfigEntries({
    moduleName,
    configName,
    namespaceAppend: ['SettingsFormContainer']
  });
 
  const { mutateAsync: editSettings } = useMutateModConfigEntry({
    configName,
    moduleName,
    id: settings?.id
  });
 
  const onSubmit = (values) => {
    const setting = {
      ...settings,
      value: JSON.stringify(values),
      ...(moduleName ?
        { module: moduleName, configName } :
        { scope, key: configName })
    };
 
    editSettings(setting).then(() => {
      callout.sendCallout({
        message: kintIntl.formatKintMessage({
          fallbackMessage: 'stripes-smart-components.cm.success',
          id: 'settingSaveSuccess',
          intlKey: passedIntlKey,
          intlNS: passedIntlNS,
          overrideValue: labelOverrides?.settingSaveSuccess
        }),
        type: 'success',
      });
    });
 
    // At some point we might want to add a catch callout.
    // The current ConfigManager doesn't have one, so leaving for now
  };
 
  const getInitialValues = () => {
    let initialValues = parsedSettings;
    if (passedGetInitialValues) {
      initialValues = passedGetInitialValues(parsedSettings, settings);
    }
 
    return initialValues;
  };
 
  return (
    <div style={{ width: '100%' }}>
      <ConfigFormComponent
        initialValues={getInitialValues()}
        onSubmit={onSubmit}
        {...rest}
      />
    </div>
  );
};
 
SettingsFormContainer.propTypes = {
  ConfigFormComponent: PropTypes.oneOfType([
    PropTypes.arrayOf(PropTypes.node),
    PropTypes.node,
    PropTypes.func,
  ]),
  configName: PropTypes.string.isRequired,
  getInitialValues: PropTypes.func,
  intlKey: PropTypes.string,
  intlNS: PropTypes.string,
  labelOverrides: PropTypes.object,
  moduleName: PropTypes.string.isRequired,
  scope: PropTypes.string,
};
 
export default SettingsFormContainer;