0

How to simplify the code, I tried to generate the object and call for each change method and depends on arguments(newValues) I change some field, seems I have duplicate code, how to avoid it this case? Should I use some more difficult method to avoid with spread syntax

const formName = {
    fieldRange: 'miConfiguration.fieldRange',
    defaultTimeout: 'miConfiguration.doorConfiguration.defaultTimeout',
    standAlone: 'miConfiguration.doorConfiguration.standAlone',
    overrideTimeout: 'miConfiguration.doorConfiguration.overrideTimeout',
    inputMode: 'miConfiguration.doorConfiguration.inputMode',
    stopMi: 'miConfiguration.doorConfiguration.stopMi',
    activeLow: 'miConfiguration.doorConfiguration.activeLow',
    enableDualTechnology: 'miConfiguration.enableDualTechnology',
    passageName: 'miConfiguration.passageName',
}

let {fieldRange, defaultTimeout, standAlone, overrideTimeout, inputMode, stopMi, activeLow, enableDualTechnology, passageName} = formName

let configurationsMi = {
    [passageName]:  null,

    [fieldRange]: null,
    [activeLow]: false,
    [standAlone]: null,
    [defaultTimeout]: null,
    [overrideTimeout]: null,
    [inputMode]: null,
    [stopMi]: null,
    [enableDualTechnology]: false,
}

const defaultValues = {
    [MiConfigurationTypes.AccessPointOnly]: {
        ...configurationsMi,
        [fieldRange]:  MiFieldRanges.Disabled,
    },

    [MiConfigurationTypes.WanderingDetection]: {
        ...configurationsMi,
        [fieldRange]:  MiFieldRanges.Small,
    },
    [MiConfigurationTypes.MuteWanderingDetection]: {
        ...configurationsMi,
        [fieldRange]:  MiFieldRanges.Small,
    },
    [MiConfigurationTypes.LockedWanderingControl]: {
        ...configurationsMi,
        [fieldRange]:  MiFieldRanges.Small,
        [standAlone]:  DoorStates.Locked,
        [defaultTimeout]: '00:00:03',
        [overrideTimeout]: '00:00:30',
        [inputMode]: InputModes.NotUsed,
        [stopMi]: false,
    },
    [MiConfigurationTypes.OpenWanderingControl]: {
        ...configurationsMi,
        [fieldRange]:  MiFieldRanges.Small,
        [standAlone]:  DoorStates.Locked,
        [defaultTimeout]: '00:00:03',
        [overrideTimeout]: '00:00:30',
        [inputMode]: InputModes.NotUsed,
        [stopMi]: false,
    },
}

 onChange={(e, newValue) => {
                console.log(defaultValues)
                Object.keys(defaultValues[newValue]).forEach(key => change(key, defaultValues[newValue][key]))
            }}
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Palaniichuk Dmytro
  • 2,943
  • 12
  • 36
  • 66
  • Where's the duplicate code that you want to avoid? Do you mean `LockedWanderingControl`/`OpenWanderingControl`? – Bergi Nov 17 '17 at 13:09
  • I have similar object for LockedWanderingControl/OpenWanderingControl how to aboid it. Or just map tghrought object and only change the field depends on (newValues(MiConfigurationTypes.OpenWanderingControl, ... etc) ) – Palaniichuk Dmytro Nov 17 '17 at 13:13

1 Answers1

1

You can just put the repeated section in an extra object literal - like you already did with configurationsMi - and refer to that.

const configurationsWanderingControl = {
    [fieldRange]:  MiFieldRanges.Small,
    [standAlone]:  DoorStates.Locked,
    [defaultTimeout]: '00:00:03',
    [overrideTimeout]: '00:00:30',
    [inputMode]: InputModes.NotUsed,
    [stopMi]: false,
};
const defaultValues = {
    …
    [MiConfigurationTypes.LockedWanderingControl]: {
        ...configurationsMi,
        ...configurationsWanderingControl,
    },
    [MiConfigurationTypes.OpenWanderingControl]: {
        ...configurationsMi,
        ...configurationsWanderingControl,
    },
};

Of course there's also the default approach at avoiding duplication: put the repeated code in a function (possibly with parameters for the little details that differ), and call that from multiple places.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375