All files / Settings/hooks/staticSettingsPages useStaticSettingsSection.js

3.03% Statements 1/33
0% Branches 0/11
0% Functions 0/10
3.12% Lines 1/32

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                    27x                                                                                                                                                                                    
import { useCallback } from 'react';
 
import useSettingSection from '../useSettingSection';
import useSettingsSectionInitalValues from './useSettingsSectionInitalValues';
import useSettingCallout from './useSettingCallout';
 
/**
 * A helper hook which collates several hooks useful in the setup and operation of static setting pages, in which
 * an entire section of AppSetting objects needs to be submitted at once
 */
const useStaticSettingsSection = ({
  intlKey: passedIntlKey,
  intlNS: passedIntlNS,
  onSubmitComplete = () => null, // Callback which runs after all promises have completed
  onSettingsSuccess = () => null, // Specifically logic to handle a list of successful settings calls
  onSettingsFailure = () => null, // Specifically logic to handle a list of failed settings calls
  sectionName,
  settingEndpoint,
  getQueryNamespaceGenerator
}) => {
  // Following example in agreements for useAgreementsDisplaySettings
  const settingsSection = useSettingSection({
    sectionName,
    settingEndpoint,
    getQueryNamespaceGenerator
  });
 
  const { handleSubmit, settings: rawSettings } = settingsSection;
 
  // We should ONLY change initial values when autoResponderValues updates
  // initialValues should take a KIWT AppSetting object and turn it into key/value for the Form
  const initialValues = useSettingsSectionInitalValues({ settings: rawSettings });
  const sendSettingsCallout = useSettingCallout({
    intlKey: passedIntlKey,
    intlNS: passedIntlNS,
  }); // Translations will need to be set up here for this to work
 
  const onSubmit = useCallback((values) => {
    const updates = [];
 
    rawSettings.forEach(setting => {
      const originalValue = initialValues[setting.key];
      const formValue = values[setting.key];
      if (formValue !== originalValue) {
        updates.push({
          ...setting,
          value: formValue.toString()
        });
      }
    });
 
    if (updates.length > 0) {
      const successful = [];
      const failed = [];
 
      // type MUST be 'success' or 'error'
 
      // We catch any failed requests and stick them in an array for us to deal with later
      const updatePromises = updates.map(update => handleSubmit(update)
        .then(() => {
          // Ensure same shape for these objects in failure AND success
          successful.push({ setting: update });
        })
        .catch((error) => {
          failed.push({ setting: update, error });
        }));
 
      return Promise.all(updatePromises)
        .then(() => {
          if (successful.length > 0) {
            sendSettingsCallout({ type: 'success', settings: successful });
            onSettingsSuccess({ settings: successful });
          }
 
          if (failed.length > 0) {
            sendSettingsCallout({ type: 'error', settings: failed });
            onSettingsFailure({ settings: failed });
          }
 
          onSubmitComplete();
          return true;
        });
    }
    // In case we somehow called this with no updates
    // (Shouldn't be possible because of the disabled save button),
    // return an empty promise
    return Promise.resolve(true);
  }, [rawSettings, initialValues, handleSubmit, onSubmitComplete, sendSettingsCallout, onSettingsSuccess, onSettingsFailure]);
 
 
  return ({
    rawSettings,
    initialValues,
    onSubmit,
    sendSettingsCallout,
    settingsSection
  });
};
 
export default useStaticSettingsSection;