Skip to content

Commit

Permalink
refactor: improve readability of long method
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Mar 7, 2024
1 parent 23482b6 commit bc4e7f7
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions apps/sandbox/src/form/availableInPlatform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {toValue} from 'vue';
import {useMemoize} from '@vueuse/core';
import {useMemoize, isDefined} from '@vueuse/core';
import {
type ConfigKey,
useLogger,
Expand All @@ -17,6 +17,29 @@ import {getAllSandboxConfigOptions} from './getAllSandboxConfigOptions';

const ALWAYS_ENABLED_FIELDS: readonly string[] = Object.freeze([CarrierSetting.AllowDeliveryOptions]);

const isValidCarrier = (carrierIdentifier?: CarrierIdentifier): carrierIdentifier is CarrierIdentifier => {
return isDefined(carrierIdentifier) && isEnumValue(resolveCarrierName(carrierIdentifier), CarrierName);
};

const featureIsEnabled = (
carrierIdentifier: CarrierIdentifier,
platformName: SupportedPlatformName,
field: ConfigKey,
): boolean => {
const {features} = useCarrier({carrierIdentifier, platformName});

const isEnabled = toValue(features).has(field);

if (!isEnabled) {
const options = getAllSandboxConfigOptions();
const match = options.find((option) => option.key === field);

return match?.parents?.some((parent) => toValue(features).has(parent)) ?? false;
}

return isEnabled;
};

export const availableInCarrier = useMemoize((fieldName: string, platformName: SupportedPlatformName): boolean => {
const logger = useLogger();

Expand All @@ -35,7 +58,7 @@ export const availableInCarrier = useMemoize((fieldName: string, platformName: S

const carrierIdentifier = split?.[split.indexOf(KEY_CARRIER_SETTINGS) + 1] as CarrierIdentifier | undefined;

if (!carrierIdentifier || !isEnumValue(resolveCarrierName(carrierIdentifier), CarrierName)) {
if (!isValidCarrier(carrierIdentifier)) {
return true;
}

Expand All @@ -45,16 +68,5 @@ export const availableInCarrier = useMemoize((fieldName: string, platformName: S
return false;
}

const {features} = useCarrier({carrierIdentifier, platformName});

const isEnabled = toValue(features).has(baseField);

if (!isEnabled) {
const options = getAllSandboxConfigOptions();
const match = options.find((option) => option.key === baseField);

return match?.parents?.some((parent) => toValue(features).has(parent)) ?? false;
}

return isEnabled;
return featureIsEnabled(carrierIdentifier, platformName, baseField);
});

0 comments on commit bc4e7f7

Please sign in to comment.