Skip to content

Commit

Permalink
feat(order): add helpful text when using postnl age check
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Oct 4, 2024
1 parent 36b9fbf commit f9a5fdd
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 22 deletions.
4 changes: 3 additions & 1 deletion apps/admin/src/forms/helpers/addBulkEditNotification.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {Variant} from '@myparcel-pdk/common';
import {KEY_DESCRIPTION} from '../shipmentOptions';
import {type PdkNotification} from '../../types';
import {useNotificationStore} from '../../stores';
import {NotificationCategory} from '../../data';
import {createLabel} from './createLabel';

const TRANSLATION_KEY = 'bulk_orders_warning';

Expand All @@ -11,7 +13,7 @@ export const addBulkEditNotification = (isModal: boolean): void => {
notificationStore.add({
id: TRANSLATION_KEY,
variant: Variant.Info,
content: `${TRANSLATION_KEY}_description`,
content: createLabel(TRANSLATION_KEY, KEY_DESCRIPTION),

Check warning on line 16 in apps/admin/src/forms/helpers/addBulkEditNotification.ts

View check run for this annotation

Codecov / codecov/patch

apps/admin/src/forms/helpers/addBulkEditNotification.ts#L16

Added line #L16 was not covered by tests
category: isModal ? NotificationCategory.Modal : NotificationCategory.General,
title: TRANSLATION_KEY,
timeout: false,
Expand Down
3 changes: 3 additions & 0 deletions apps/admin/src/forms/helpers/createLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const createLabel = <Label extends string | undefined>(label: Label, type: string): Label => {
return (label ? `${label}_${type}` : undefined) as Label;
};
6 changes: 4 additions & 2 deletions apps/admin/src/forms/helpers/defineFormField.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {defineField, type InteractiveElementConfiguration} from '@myparcel/vue-form-builder';
import {KEY_DESCRIPTION, KEY_SUBTEXT} from '../shipmentOptions';
import {createLabel} from './createLabel';

export const defineFormField = (config: InteractiveElementConfiguration): InteractiveElementConfiguration => {
const {props, label} = config;
Expand All @@ -9,8 +11,8 @@ export const defineFormField = (config: InteractiveElementConfiguration): Intera
return defineField({
...config,
props: {
description: `${label}_description`,
subtext: `${label}_subtext`,
description: createLabel(label, KEY_DESCRIPTION),
subtext: createLabel(label, KEY_SUBTEXT),
...props,
},
});
Expand Down
6 changes: 6 additions & 0 deletions apps/admin/src/forms/helpers/getFieldLabel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {snakeCase} from 'lodash-unified';
import {type Shipment} from '@myparcel-pdk/common';
import {SHIPMENT_OPTIONS} from '../shipmentOptions';

export const getFieldLabel = (name: keyof Shipment.ModelShipmentOptions): string =>
snakeCase(`${SHIPMENT_OPTIONS}_${name}`);

Check warning on line 6 in apps/admin/src/forms/helpers/getFieldLabel.ts

View check run for this annotation

Codecov / codecov/patch

apps/admin/src/forms/helpers/getFieldLabel.ts#L6

Added line #L6 was not covered by tests
11 changes: 11 additions & 0 deletions apps/admin/src/forms/helpers/hasPostNlAgeCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {type FormInstance} from '@myparcel/vue-form-builder';
import {CarrierName} from '@myparcel/constants';
import {FIELD_AGE_CHECK} from '../shipmentOptions';
import {triStateFieldIsEnabled} from './triStateFieldIsEnabled';
import {getFormCarrierName} from './getFormCarrierName';

export const hasPostNlAgeCheck = (form: FormInstance): boolean => {
const carrier = getFormCarrierName(form);

return CarrierName.PostNl === carrier && triStateFieldIsEnabled(form, FIELD_AGE_CHECK);
};

Check warning on line 11 in apps/admin/src/forms/helpers/hasPostNlAgeCheck.ts

View check run for this annotation

Codecov / codecov/patch

apps/admin/src/forms/helpers/hasPostNlAgeCheck.ts#L8-L11

Added lines #L8 - L11 were not covered by tests
4 changes: 4 additions & 0 deletions apps/admin/src/forms/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
export * from './addBulkEditNotification';
export * from './createDefaultOption';
export * from './createHasShipmentOptionWatcher';
export * from './createLabel';
export * from './defineFormField';
export * from './getCarrier';
export * from './getFieldLabel';
export * from './getFormCarrierName';
export * from './getInsuranceOptions';
export * from './getPackageTypes';
export * from './hasPostNlAgeCheck';
export * from './hasShipmentOption';
export * from './isPackageTypePackage';
export * from './resolveFormComponent';
export * from './setFieldProp';
export * from './setPostNlAgeCheckSubtext';
export * from './triStateFieldIsEnabled';
export * from './updateFieldsDefaults';
17 changes: 17 additions & 0 deletions apps/admin/src/forms/helpers/setPostNlAgeCheckSubtext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {type InteractiveElementInstance} from '@myparcel/vue-form-builder';
import {CarrierName} from '@myparcel/constants';
import {AGE_CHECK, FIELD_AGE_CHECK, KEY_SUBTEXT} from '../shipmentOptions';
import {setFieldProp} from './setFieldProp';
import {hasPostNlAgeCheck} from './hasPostNlAgeCheck';
import {getFieldLabel} from './getFieldLabel';
import {createLabel} from './createLabel';

export const setPostNlAgeCheckSubtext = ({form}: InteractiveElementInstance): void => {
const defaultKey = createLabel(getFieldLabel(AGE_CHECK), KEY_SUBTEXT);

if (hasPostNlAgeCheck(form)) {
setFieldProp(form, FIELD_AGE_CHECK, KEY_SUBTEXT, `${defaultKey}_${CarrierName.PostNl}`);
} else {
setFieldProp(form, FIELD_AGE_CHECK, KEY_SUBTEXT, defaultKey);
}
};

Check warning on line 17 in apps/admin/src/forms/helpers/setPostNlAgeCheckSubtext.ts

View check run for this annotation

Codecov / codecov/patch

apps/admin/src/forms/helpers/setPostNlAgeCheckSubtext.ts#L10-L17

Added lines #L10 - L17 were not covered by tests
4 changes: 4 additions & 0 deletions apps/admin/src/forms/shipmentOptions/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export const MANUAL_WEIGHT = 'manualWeight';

export const PROP_OPTIONS = 'options';

export const KEY_DESCRIPTION = 'description';

export const KEY_SUBTEXT = 'subtext';

export const FIELD_CARRIER: FieldName = `${DELIVERY_OPTIONS_PREFIX}.${CARRIER}`;

export const FIELD_LABEL_AMOUNT: FieldName = `${DELIVERY_OPTIONS_PREFIX}.${LABEL_AMOUNT}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import {type PromiseOr} from '@myparcel/ts-utils';
import {CarrierName} from '@myparcel/constants';
import {type ShipmentOptionsRefs} from '../types';
import {FIELD_AGE_CHECK, FIELD_ONLY_RECIPIENT, FIELD_SIGNATURE} from '../field';
import {getFormCarrierName, triStateFieldIsEnabled} from '../../helpers';
import {getFormCarrierName, setPostNlAgeCheckSubtext, triStateFieldIsEnabled} from '../../helpers';
import {createShipmentOptionField} from './createShipmentOptionField';

export const createAgeCheckField = (refs: ShipmentOptionsRefs): InteractiveElementConfiguration => {
return createShipmentOptionField(refs, FIELD_AGE_CHECK, {
afterUpdate(field): PromiseOr<void> {
const {form} = field;

setPostNlAgeCheckSubtext(field);

Check warning on line 16 in apps/admin/src/forms/shipmentOptions/fields/createAgeCheckField.ts

View check run for this annotation

Codecov / codecov/patch

apps/admin/src/forms/shipmentOptions/fields/createAgeCheckField.ts#L15-L16

Added lines #L15 - L16 were not covered by tests
if (!triStateFieldIsEnabled(field.form, FIELD_AGE_CHECK)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getPackageTypes,
resolveFormComponent,
setFieldProp,
setPostNlAgeCheckSubtext,
updateFieldsDefaults,
} from '../../helpers';
import {createAssetUrl} from '../../../utils';
Expand Down Expand Up @@ -73,6 +74,8 @@ export const createCarrierField = (

setFieldProp(field.form, FIELD_INSURANCE, PROP_OPTIONS, getInsuranceOptions(field, formatter));
setFieldProp(field.form, FIELD_PACKAGE_TYPE, PROP_OPTIONS, getPackageTypes(field.form));

setPostNlAgeCheckSubtext(field);

Check warning on line 78 in apps/admin/src/forms/shipmentOptions/fields/createCarrierField.ts

View check run for this annotation

Codecov / codecov/patch

apps/admin/src/forms/shipmentOptions/fields/createCarrierField.ts#L77-L78

Added lines #L77 - L78 were not covered by tests
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import {type InteractiveElementConfiguration} from '@myparcel/vue-form-builder';
import {CarrierName} from '@myparcel/constants';
import {type ShipmentOptionsRefs} from '../types';
import {FIELD_AGE_CHECK, FIELD_ONLY_RECIPIENT} from '../field';
import {getFormCarrierName, triStateFieldIsEnabled} from '../../helpers';
import {getFormCarrierName, hasPostNlAgeCheck} from '../../helpers';
import {createShipmentOptionField} from './createShipmentOptionField';

export const createOnlyRecipientField = (refs: ShipmentOptionsRefs): InteractiveElementConfiguration => {
return createShipmentOptionField(refs, FIELD_ONLY_RECIPIENT, {
readOnlyWhen({form}) {
const carrier = getFormCarrierName(form);

return CarrierName.PostNl === carrier && triStateFieldIsEnabled(form, FIELD_AGE_CHECK);
},
readOnlyWhen: ({form}) => hasPostNlAgeCheck(form),

Check warning on line 11 in apps/admin/src/forms/shipmentOptions/fields/createOnlyRecipientField.ts

View check run for this annotation

Codecov / codecov/patch

apps/admin/src/forms/shipmentOptions/fields/createOnlyRecipientField.ts#L11

Added line #L11 was not covered by tests

afterUpdate({form}, value) {
if (!(CarrierName.DhlForYou === getFormCarrierName(form) && TriState.On === value)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {snakeCase} from 'lodash-unified';
import {type Shipment, TriState} from '@myparcel-pdk/common';
import {type InteractiveElementConfiguration} from '@myparcel/vue-form-builder';
import {type ShipmentOptionsRefs} from '../types';
import {type FieldName, SHIPMENT_OPTIONS} from '../field';
import {createHasShipmentOptionWatcher, defineFormField, resolveFormComponent} from '../../helpers';
import {type FieldName} from '../field';
import {createHasShipmentOptionWatcher, defineFormField, getFieldLabel, resolveFormComponent} from '../../helpers';
import {AdminComponent} from '../../../data';
import {createRef} from './createRef';

Expand All @@ -18,7 +17,7 @@ export const createShipmentOptionField = (
name: fieldName,
component: resolveFormComponent(AdminComponent.TriStateInput),
ref: createRef(refs, fieldName, TriState.Inherit),
label: snakeCase(`${SHIPMENT_OPTIONS}_${name}`),
label: getFieldLabel(name),

Check warning on line 20 in apps/admin/src/forms/shipmentOptions/fields/createShipmentOptionField.ts

View check run for this annotation

Codecov / codecov/patch

apps/admin/src/forms/shipmentOptions/fields/createShipmentOptionField.ts#L20

Added line #L20 was not covered by tests
visibleWhen: createHasShipmentOptionWatcher(name),
disabledWhen: createHasShipmentOptionWatcher(name, true),
...config,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import {type InteractiveElementConfiguration} from '@myparcel/vue-form-builder';
import {CarrierName} from '@myparcel/constants';
import {type ShipmentOptionsRefs} from '../types';
import {FIELD_AGE_CHECK, FIELD_SIGNATURE} from '../field';
import {getFormCarrierName, triStateFieldIsEnabled} from '../../helpers';
import {FIELD_SIGNATURE} from '../field';
import {hasPostNlAgeCheck} from '../../helpers';
import {createShipmentOptionField} from './createShipmentOptionField';

export const createSignatureField = (refs: ShipmentOptionsRefs): InteractiveElementConfiguration => {
return createShipmentOptionField(refs, FIELD_SIGNATURE, {
readOnlyWhen({form}) {
const carrier = getFormCarrierName(form);

return CarrierName.PostNl === carrier && triStateFieldIsEnabled(form, FIELD_AGE_CHECK);
},
readOnlyWhen: ({form}) => hasPostNlAgeCheck(form),

Check warning on line 9 in apps/admin/src/forms/shipmentOptions/fields/createSignatureField.ts

View check run for this annotation

Codecov / codecov/patch

apps/admin/src/forms/shipmentOptions/fields/createSignatureField.ts#L9

Added line #L9 was not covered by tests
});
};

0 comments on commit f9a5fdd

Please sign in to comment.