diff --git a/apps/checkout-js/src/__tests__/__snapshots__/exports.spec.ts.snap b/apps/checkout-js/src/__tests__/__snapshots__/exports.spec.ts.snap index e904748a..addb106f 100644 --- a/apps/checkout-js/src/__tests__/__snapshots__/exports.spec.ts.snap +++ b/apps/checkout-js/src/__tests__/__snapshots__/exports.spec.ts.snap @@ -21,8 +21,11 @@ exports[`exports > exports from index.ts 1`] = ` "_", "createPdkCheckout", "debounce", + "defaultGetPackageType", + "defaultUpdateDeliveryOptions", "deliveryOptionsIsRendered", "getDeliveryOptionsAddress", + "getEnabledShippingMethods", "getResolvedSettings", "initializeCheckoutDeliveryOptions", "initializeCheckoutSeparateAddressFields", diff --git a/apps/checkout/src/__tests__/__snapshots__/exports.spec.ts.snap b/apps/checkout/src/__tests__/__snapshots__/exports.spec.ts.snap index e904748a..addb106f 100644 --- a/apps/checkout/src/__tests__/__snapshots__/exports.spec.ts.snap +++ b/apps/checkout/src/__tests__/__snapshots__/exports.spec.ts.snap @@ -21,8 +21,11 @@ exports[`exports > exports from index.ts 1`] = ` "_", "createPdkCheckout", "debounce", + "defaultGetPackageType", + "defaultUpdateDeliveryOptions", "deliveryOptionsIsRendered", "getDeliveryOptionsAddress", + "getEnabledShippingMethods", "getResolvedSettings", "initializeCheckoutDeliveryOptions", "initializeCheckoutSeparateAddressFields", diff --git a/apps/checkout/src/index.ts b/apps/checkout/src/index.ts index 1a89cf33..4deb197f 100644 --- a/apps/checkout/src/index.ts +++ b/apps/checkout/src/index.ts @@ -9,6 +9,7 @@ export type { PdkCheckoutConfig, PdkCheckoutConfigInput, PdkCheckoutForm, + PdkFormData, StoreCallbackUpdate, } from '@myparcel-pdk/checkout-common'; @@ -29,6 +30,7 @@ export { PdkUtil as Util, globals as _, createPdkCheckout, + getEnabledShippingMethods, useCheckoutStore, useEvent, usePdkCheckout, diff --git a/libs/checkout-common/src/__tests__/__snapshots__/exports.spec.ts.snap b/libs/checkout-common/src/__tests__/__snapshots__/exports.spec.ts.snap index 7a92c8a0..332eb213 100644 --- a/libs/checkout-common/src/__tests__/__snapshots__/exports.spec.ts.snap +++ b/libs/checkout-common/src/__tests__/__snapshots__/exports.spec.ts.snap @@ -24,6 +24,10 @@ exports[`exports > exports from index.ts 1`] = ` "createConfig", "createEventName", "createPdkCheckout", + "defaultFormChange", + "defaultGetAddressType", + "defaultGetFormData", + "defaultHasDeliveryOptions", "doRequest", "eventCheckoutUpdate", "eventCheckoutUpdated", @@ -33,6 +37,7 @@ exports[`exports > exports from index.ts 1`] = ` "getAddressFieldValue", "getAddressType", "getElement", + "getEnabledShippingMethods", "getFieldValue", "getFrontendContext", "globals", diff --git a/libs/checkout-common/src/__tests__/constants.ts b/libs/checkout-common/src/__tests__/constants.ts new file mode 100644 index 00000000..8c44470f --- /dev/null +++ b/libs/checkout-common/src/__tests__/constants.ts @@ -0,0 +1,17 @@ +import {type PdkFormData} from '../types'; +import {AddressType} from '../data'; + +export const DEFAULT_MOCK_FORM_DATA = Object.freeze({ + 'address-type': AddressType.Billing, + 'b-address1': 'Straatnaam 12e', + 'b-address2': '', + 'b-city': 'Amsterdam', + 'b-country': 'NL', + 'b-postal-code': '1234AB', + 's-address1': 'Straatnaam 12e', + 's-address2': '', + 's-city': 'Amsterdam', + 's-country': 'NL', + 's-postal-code': '1234AB', + 'shipping-method': 'standard', +} satisfies PdkFormData); diff --git a/libs/checkout-common/src/__tests__/getMockCheckoutContext.ts b/libs/checkout-common/src/__tests__/getMockCheckoutContext.ts index da45d29e..317d7f68 100644 --- a/libs/checkout-common/src/__tests__/getMockCheckoutContext.ts +++ b/libs/checkout-common/src/__tests__/getMockCheckoutContext.ts @@ -1,5 +1,6 @@ import {vi} from 'vitest'; import {FrontendEndpoint} from '@myparcel-pdk/common'; +import {PackageTypeName} from '@myparcel/constants'; import {type CheckoutAppCheckoutContext} from '../types'; export const getMockCheckoutContext = vi.fn( @@ -22,7 +23,14 @@ export const getMockCheckoutContext = vi.fn( }, ...context?.settings?.actions, }, - allowedShippingMethods: [], + allowedShippingMethods: { + '-1': ['standard', 'free_shipping'], + [PackageTypeName.Package]: ['flat_rate:1'], + [PackageTypeName.PackageSmall]: ['flat_rate:2'], + [PackageTypeName.Mailbox]: ['flat_rate:3', 'flat_rate:4'], + [PackageTypeName.DigitalStamp]: ['flat_rate:5'], + [PackageTypeName.Letter]: ['flat_rate:6'], + }, carriersWithTaxFields: [], countriesWithSeparateAddressFields: [], hiddenInputName: '', diff --git a/libs/checkout-common/src/__tests__/index.ts b/libs/checkout-common/src/__tests__/index.ts index 1cf90d57..a478cf34 100644 --- a/libs/checkout-common/src/__tests__/index.ts +++ b/libs/checkout-common/src/__tests__/index.ts @@ -1,6 +1,8 @@ +export * from './constants'; export * from './getMockCheckoutContext'; export * from './getMockFormData'; export * from './getMockPdkCheckoutConfig'; export * from './mockDeliveryOptionsElement'; export * from './mockPdkCheckout'; +export * from './mockShippingMethod'; export * from './spies'; diff --git a/libs/checkout-common/src/__tests__/mockDeliveryOptionsElement.ts b/libs/checkout-common/src/__tests__/mockDeliveryOptionsElement.ts index d32f0625..b07868b9 100644 --- a/libs/checkout-common/src/__tests__/mockDeliveryOptionsElement.ts +++ b/libs/checkout-common/src/__tests__/mockDeliveryOptionsElement.ts @@ -8,6 +8,7 @@ export const mockDeliveryOptionsElement = (): void => { const wrapper = document.createElement('div'); const element = document.createElement('div'); + form.id = 'test-wrapper'; wrapper.id = 'delivery-options-wrapper'; element.id = 'delivery-options'; diff --git a/libs/checkout-common/src/__tests__/mockPdkCheckout.ts b/libs/checkout-common/src/__tests__/mockPdkCheckout.ts index b68e2e24..89d19a8b 100644 --- a/libs/checkout-common/src/__tests__/mockPdkCheckout.ts +++ b/libs/checkout-common/src/__tests__/mockPdkCheckout.ts @@ -4,7 +4,18 @@ import {createPdkCheckout} from '../createPdkCheckout'; import {mockDeliveryOptionsElement} from './mockDeliveryOptionsElement'; import {getMockPdkCheckoutConfig} from './getMockPdkCheckoutConfig'; +function reset() { + // Clear the window object + // @ts-expect-error this is correct for testing + window.MyParcelPdk = undefined; + + // Delete any wrappers added by previous tests + document.getElementById('test-wrapper')?.remove(); +} + export const mockPdkCheckout = (config?: Partial, includeElements = true): Promise => { + reset(); + return new Promise((resolve, reject) => { if (includeElements) { mockDeliveryOptionsElement(); diff --git a/libs/checkout-common/src/__tests__/mockShippingMethod.ts b/libs/checkout-common/src/__tests__/mockShippingMethod.ts new file mode 100644 index 00000000..ce1994bf --- /dev/null +++ b/libs/checkout-common/src/__tests__/mockShippingMethod.ts @@ -0,0 +1,8 @@ +import {getFormDataSpy} from './spies'; + +export const mockShippingMethod = (shippingMethod: string): void => { + getFormDataSpy.mockReturnValue({ + ...getFormDataSpy(), + 'shipping-method': shippingMethod, + }); +}; diff --git a/libs/checkout-common/src/__tests__/spies.ts b/libs/checkout-common/src/__tests__/spies.ts index e820e0f6..b6d76c14 100644 --- a/libs/checkout-common/src/__tests__/spies.ts +++ b/libs/checkout-common/src/__tests__/spies.ts @@ -1,27 +1,13 @@ import {vi} from 'vitest'; -import {AddressType} from '../data'; +import {type PdkFormData} from '../types'; +import {DEFAULT_MOCK_FORM_DATA} from './constants'; export const doRequestSpy = vi.fn(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion export const getFormSpy = vi.fn(() => document.querySelector('form')!); -export const getFormDataSpy = vi.fn( - (): Record => ({ - 'address-type': AddressType.Billing, - 'b-address1': 'Straatnaam 12e', - 'b-address2': '', - 'b-city': 'Amsterdam', - 'b-country': 'NL', - 'b-postal-code': '1234AB', - 's-address1': 'Straatnaam 12e', - 's-address2': '', - 's-city': 'Amsterdam', - 's-country': 'NL', - 's-postal-code': '1234AB', - 'shipping-method': 'shipping-method', - }), -); +export const getFormDataSpy = vi.fn((): PdkFormData => DEFAULT_MOCK_FORM_DATA); export const hasAddressTypeSpy = vi.fn(() => true); diff --git a/libs/checkout-common/src/data/utils.ts b/libs/checkout-common/src/data/utils.ts index 28984d6d..93978b3b 100644 --- a/libs/checkout-common/src/data/utils.ts +++ b/libs/checkout-common/src/data/utils.ts @@ -6,6 +6,7 @@ export enum PdkUtil { GetAddressFieldValue = 'getAddressFieldValue', GetElement = 'getElement', GetFieldValue = 'getFieldValue', + IsEnumValue = 'isEnumValue', IsOfType = 'isOfType', SetFieldValue = 'setFieldValue', TriggerEvent = 'triggerEvent', diff --git a/libs/checkout-common/src/defaults/defaultFormChange.ts b/libs/checkout-common/src/defaults/defaultFormChange.ts new file mode 100644 index 00000000..ced46f6f --- /dev/null +++ b/libs/checkout-common/src/defaults/defaultFormChange.ts @@ -0,0 +1,9 @@ +import {useConfig} from '../utils'; +import {type PdkCheckoutConfig} from '../types'; + +export const defaultFormChange = ((callback) => { + const config = useConfig(); + const form = config.getForm(); + + form.addEventListener('change', callback); +}) satisfies PdkCheckoutConfig['formChange']; diff --git a/libs/checkout-common/src/defaults/defaultGetAddressType.ts b/libs/checkout-common/src/defaults/defaultGetAddressType.ts new file mode 100644 index 00000000..c6d75de1 --- /dev/null +++ b/libs/checkout-common/src/defaults/defaultGetAddressType.ts @@ -0,0 +1,4 @@ +import {type PdkCheckoutConfig} from '../types'; +import {type AddressType} from '../data'; + +export const defaultGetAddressType = ((value) => value as AddressType) satisfies PdkCheckoutConfig['getAddressType']; diff --git a/libs/checkout-common/src/defaults/defaultGetFormData.ts b/libs/checkout-common/src/defaults/defaultGetFormData.ts new file mode 100644 index 00000000..69faf5d8 --- /dev/null +++ b/libs/checkout-common/src/defaults/defaultGetFormData.ts @@ -0,0 +1,11 @@ +import {useConfig} from '../utils'; +import {type PdkCheckoutConfig} from '../types'; + +export const defaultGetFormData = (() => { + const config = useConfig(); + const form = config.getForm(); + + const formData = new FormData(form); + + return Object.fromEntries(formData.entries()); +}) satisfies PdkCheckoutConfig['getFormData']; diff --git a/libs/checkout-common/src/defaults/defaultHasDeliveryOptions.ts b/libs/checkout-common/src/defaults/defaultHasDeliveryOptions.ts new file mode 100644 index 00000000..3d31ff5c --- /dev/null +++ b/libs/checkout-common/src/defaults/defaultHasDeliveryOptions.ts @@ -0,0 +1,8 @@ +import {getEnabledShippingMethods} from '../utils'; +import {type PdkCheckoutConfig} from '../types'; + +export const defaultHasDeliveryOptions = ((shippingMethod: string): boolean => { + const enabledShippingMethods = getEnabledShippingMethods(); + + return enabledShippingMethods.some((method) => shippingMethod === method); +}) satisfies PdkCheckoutConfig['hasDeliveryOptions']; diff --git a/libs/checkout-common/src/defaults/index.ts b/libs/checkout-common/src/defaults/index.ts new file mode 100644 index 00000000..fc5ec54a --- /dev/null +++ b/libs/checkout-common/src/defaults/index.ts @@ -0,0 +1,4 @@ +export * from './defaultFormChange'; +export * from './defaultGetAddressType'; +export * from './defaultGetFormData'; +export * from './defaultHasDeliveryOptions'; diff --git a/libs/checkout-common/src/index.ts b/libs/checkout-common/src/index.ts index 20afb0cd..e68b60fe 100644 --- a/libs/checkout-common/src/index.ts +++ b/libs/checkout-common/src/index.ts @@ -2,6 +2,7 @@ export * as tests from './__tests__'; export * from './constants'; export * from './createPdkCheckout'; export * from './data'; +export * from './defaults'; export * as globals from './globals'; export * from './init'; export * from './listeners'; diff --git a/libs/checkout-common/src/init/createConfig.ts b/libs/checkout-common/src/init/createConfig.ts index 352bfe09..70020854 100644 --- a/libs/checkout-common/src/init/createConfig.ts +++ b/libs/checkout-common/src/init/createConfig.ts @@ -1,29 +1,11 @@ -import {useConfig, useSettings} from '../utils'; import {type PdkCheckoutConfig, type PdkCheckoutConfigInput} from '../types'; -import {type AddressType} from '../data'; +import {defaultFormChange, defaultGetAddressType, defaultGetFormData, defaultHasDeliveryOptions} from '../defaults'; export const createConfig = (config: PdkCheckoutConfigInput): PdkCheckoutConfig => ({ - formChange(callback) { - const form = useConfig().getForm(); - form.addEventListener('change', callback); - }, - - getAddressType(value) { - return value as AddressType; - }, - - getFormData() { - const form = useConfig().getForm(); - const formData = new FormData(form); - - return Object.fromEntries(formData.entries()); - }, - - hasDeliveryOptions(shippingMethod: string): boolean { - const settings = useSettings(); - - return settings.allowedShippingMethods.some((method) => shippingMethod === method); - }, + formChange: defaultFormChange, + getAddressType: defaultGetAddressType, + getFormData: defaultGetFormData, + hasDeliveryOptions: defaultHasDeliveryOptions, ...config, selectors: { diff --git a/libs/checkout-common/src/init/setupGlobals.ts b/libs/checkout-common/src/init/setupGlobals.ts index 3378a583..446df17a 100644 --- a/libs/checkout-common/src/init/setupGlobals.ts +++ b/libs/checkout-common/src/init/setupGlobals.ts @@ -1,5 +1,5 @@ // eslint-disable-next-line max-lines-per-function -import {isOfType} from '@myparcel/ts-utils'; +import {isEnumValue, isOfType} from '@myparcel/ts-utils'; import { doRequest, fieldsEqual, @@ -66,6 +66,7 @@ export const setupGlobals = (config: PdkCheckoutConfig): void => { getAddressFieldValue, getElement, getFieldValue, + isEnumValue, isOfType, setFieldValue, triggerEvent, diff --git a/libs/checkout-common/src/listeners/updateCheckoutForm.ts b/libs/checkout-common/src/listeners/updateCheckoutForm.ts index 021df8cf..575bf6a4 100644 --- a/libs/checkout-common/src/listeners/updateCheckoutForm.ts +++ b/libs/checkout-common/src/listeners/updateCheckoutForm.ts @@ -1,10 +1,10 @@ import {getAddressType, useCheckoutStore, useConfig} from '../utils'; -import {type PdkCheckoutForm} from '../types'; +import {type PdkCheckoutForm, type PdkFormData} from '../types'; import {type AddressField, AddressType, type PdkField} from '../data'; -function getEntry(data: Record, value: undefined | string): string { +const getEntry = (data: PdkFormData, value: undefined | string): string => { return (data[value as string] as string | undefined) ?? ''; -} +}; export const updateCheckoutForm = (): void => { const checkout = useCheckoutStore(); diff --git a/libs/checkout-common/src/types/checkout.types.ts b/libs/checkout-common/src/types/checkout.types.ts index c21e67b5..f7fc9f8b 100644 --- a/libs/checkout-common/src/types/checkout.types.ts +++ b/libs/checkout-common/src/types/checkout.types.ts @@ -1,4 +1,4 @@ -import {type FrontendEndpoint} from '@myparcel-pdk/common'; +import {type FrontendEndpoint, type ShippingMethodTypeMap} from '@myparcel-pdk/common'; import {type PromiseOr} from '@myparcel/ts-utils'; import {type InputDeliveryOptionsConfiguration} from '@myparcel/delivery-options'; import {type CarrierName} from '@myparcel/constants'; @@ -10,13 +10,15 @@ import { } from './endpoints.types'; import {type AddressFields} from './address.types'; +export type PdkFormData = Record; + export type PdkCheckoutConfigInput = Omit< PdkCheckoutConfig, 'selectors' | 'formChange' | 'getFormData' | 'getAddressType' | 'hasDeliveryOptions' > & { formChange?(callback: () => void): void; getAddressType?(value: unknown): AddressType; - getFormData?(): Record; + getFormData?(): PdkFormData; hasDeliveryOptions?(shippingMethod: string): PromiseOr; selectors: Omit & { deliveryOptions?: string; @@ -60,7 +62,7 @@ export interface PdkCheckoutConfig { /** * Get the form data. */ - getFormData(): Record; + getFormData(): PdkFormData; /** * Check if the address type is available. @@ -104,7 +106,7 @@ export type CheckoutSettings = { }; // Delivery options - allowedShippingMethods: string[]; + allowedShippingMethods: ShippingMethodTypeMap; hasDeliveryOptions: boolean; hiddenInputName: string; diff --git a/libs/checkout-common/src/types/global.types.ts b/libs/checkout-common/src/types/global.types.ts index a5a5348a..3563f7aa 100644 --- a/libs/checkout-common/src/types/global.types.ts +++ b/libs/checkout-common/src/types/global.types.ts @@ -1,4 +1,4 @@ -import {type isOfType, type PromiseOr} from '@myparcel/ts-utils'; +import {type isEnumValue, type isOfType, type PromiseOr} from '@myparcel/ts-utils'; import { type doRequest, type fieldsEqual, @@ -33,6 +33,7 @@ export interface PdkUtils extends AdditionalUtils { [PdkUtil.GetAddressField]: typeof getAddressField; [PdkUtil.GetElement]: typeof getElement; [PdkUtil.GetFieldValue]: typeof getFieldValue; + [PdkUtil.IsEnumValue]: typeof isEnumValue; [PdkUtil.IsOfType]: typeof isOfType; [PdkUtil.SetFieldValue]: typeof setFieldValue; [PdkUtil.TriggerEvent]: typeof triggerEvent; diff --git a/libs/checkout-common/src/utils/getEnabledShippingMethods.ts b/libs/checkout-common/src/utils/getEnabledShippingMethods.ts new file mode 100644 index 00000000..c9737a74 --- /dev/null +++ b/libs/checkout-common/src/utils/getEnabledShippingMethods.ts @@ -0,0 +1,9 @@ +import {useSettings} from './useSettings'; + +export const getEnabledShippingMethods = (): string[] => { + const settings = useSettings(); + // No need to filter keys, 0 (off) is not included in the object + const entries = Object.entries(settings.allowedShippingMethods); + + return entries.flatMap(([, value]) => value); +}; diff --git a/libs/checkout-common/src/utils/index.ts b/libs/checkout-common/src/utils/index.ts index 0306f69d..80c69176 100644 --- a/libs/checkout-common/src/utils/index.ts +++ b/libs/checkout-common/src/utils/index.ts @@ -1,3 +1,4 @@ +export * from './getEnabledShippingMethods'; export * from './getFrontendContext'; export * from './global'; export * from './hasAddressType'; diff --git a/libs/checkout-delivery-options/src/__tests__/__snapshots__/exports.spec.ts.snap b/libs/checkout-delivery-options/src/__tests__/__snapshots__/exports.spec.ts.snap index ab97b4da..21a8cf1d 100644 --- a/libs/checkout-delivery-options/src/__tests__/__snapshots__/exports.spec.ts.snap +++ b/libs/checkout-delivery-options/src/__tests__/__snapshots__/exports.spec.ts.snap @@ -5,6 +5,8 @@ exports[`exports > exports from index.ts 1`] = ` "DeliveryOptionsMode", "PdkDeliveryOptionsEvent", "debounce", + "defaultGetPackageType", + "defaultUpdateDeliveryOptions", "deliveryOptionsIsRendered", "getDeliveryOptionsAddress", "getResolvedSettings", diff --git a/libs/checkout-delivery-options/src/defaults/defaultGetPackageType.spec.ts b/libs/checkout-delivery-options/src/defaults/defaultGetPackageType.spec.ts new file mode 100644 index 00000000..35fca834 --- /dev/null +++ b/libs/checkout-delivery-options/src/defaults/defaultGetPackageType.spec.ts @@ -0,0 +1,31 @@ +// @vitest-environment happy-dom + +import {describe, expect, it} from 'vitest'; +import {tests, useCheckoutStore} from '@myparcel-pdk/checkout-common'; +import {PackageTypeName} from '@myparcel/constants'; +import {defaultGetPackageType} from './defaultGetPackageType'; + +type TestInput = [string, PackageTypeName | undefined]; + +describe('defaultGetPackageType', () => { + it.each([ + ['flat_rate:1', PackageTypeName.Package], + ['flat_rate:2', PackageTypeName.PackageSmall], + ['flat_rate:3', PackageTypeName.Mailbox], + ['flat_rate:4', PackageTypeName.Mailbox], + ['flat_rate:5', PackageTypeName.DigitalStamp], + ['flat_rate:6', PackageTypeName.Letter], + ['standard', undefined], + ] satisfies TestInput[])('returns the package type for shipping method %s', async (shippingMethod, result) => { + expect.assertions(2); + + tests.mockShippingMethod(shippingMethod); + await tests.mockPdkCheckout(); + + const packageType = defaultGetPackageType(); + const checkout = useCheckoutStore(); + + expect(packageType).toBe(result); + expect(checkout.state.form.shippingMethod).toBe(shippingMethod); + }); +}); diff --git a/libs/checkout-delivery-options/src/defaults/defaultGetPackageType.ts b/libs/checkout-delivery-options/src/defaults/defaultGetPackageType.ts new file mode 100644 index 00000000..b72e7307 --- /dev/null +++ b/libs/checkout-delivery-options/src/defaults/defaultGetPackageType.ts @@ -0,0 +1,22 @@ +import {type ShippingMethodId} from '@myparcel-pdk/common'; +import {PdkUtil, useCheckoutStore, useSettings, useUtil} from '@myparcel-pdk/checkout-common'; +import {PackageTypeName} from '@myparcel/constants'; +import {type CheckoutDeliveryOptionsSettings} from '../types'; + +export const defaultGetPackageType = (() => { + const isEnumValue = useUtil(PdkUtil.IsEnumValue); + const {allowedShippingMethods} = useSettings(); + const checkout = useCheckoutStore(); + + const {shippingMethod} = checkout.state.form; + + const found = Object.entries(allowedShippingMethods).find(([, value]) => value.includes(shippingMethod)) as + | [PackageTypeName, ShippingMethodId[]] + | undefined; + + const result = found?.[0]; + + const isPackageType = isEnumValue(result, PackageTypeName); + + return isPackageType ? result : undefined; +}) satisfies CheckoutDeliveryOptionsSettings['getPackageType']; diff --git a/libs/checkout-delivery-options/src/defaults/defaultUpdateDeliveryOptions.ts b/libs/checkout-delivery-options/src/defaults/defaultUpdateDeliveryOptions.ts new file mode 100644 index 00000000..a4cb8e9b --- /dev/null +++ b/libs/checkout-delivery-options/src/defaults/defaultUpdateDeliveryOptions.ts @@ -0,0 +1,18 @@ +import {CarrierSetting} from '@myparcel/delivery-options'; +import {useDeliveryOptionsStore} from '../utils'; +import {type CheckoutDeliveryOptionsSettings} from '../types'; + +export const defaultUpdateDeliveryOptions = ((state) => { + const deliveryOptions = useDeliveryOptionsStore(); + const baseConfig = state.configuration.config; + + return { + ...Object.create(baseConfig), + + /** + * Try to get the package type based on shipping method. If it's not found, use the passed package type. + */ + [CarrierSetting.PackageType]: + deliveryOptions.state.settings.getPackageType() ?? baseConfig[CarrierSetting.PackageType], + }; +}) satisfies CheckoutDeliveryOptionsSettings['updateDeliveryOptions']; diff --git a/libs/checkout-delivery-options/src/defaults/index.ts b/libs/checkout-delivery-options/src/defaults/index.ts new file mode 100644 index 00000000..30130382 --- /dev/null +++ b/libs/checkout-delivery-options/src/defaults/index.ts @@ -0,0 +1,2 @@ +export * from './defaultGetPackageType'; +export * from './defaultUpdateDeliveryOptions'; diff --git a/libs/checkout-delivery-options/src/index.ts b/libs/checkout-delivery-options/src/index.ts index 298547a7..ef861c81 100644 --- a/libs/checkout-delivery-options/src/index.ts +++ b/libs/checkout-delivery-options/src/index.ts @@ -1,3 +1,4 @@ +export * from './defaults'; export * from './globals'; export * from './initializeCheckoutDeliveryOptions'; export * from './types'; diff --git a/libs/checkout-delivery-options/src/types/generic.types.ts b/libs/checkout-delivery-options/src/types/generic.types.ts index f5c27d51..73c12cb8 100644 --- a/libs/checkout-delivery-options/src/types/generic.types.ts +++ b/libs/checkout-delivery-options/src/types/generic.types.ts @@ -1,4 +1,5 @@ import {type InputDeliveryOptionsConfig} from '@myparcel/delivery-options'; +import {type PackageTypeName} from '@myparcel/constants'; import {type DeliveryOptionsStoreState} from './store.types'; export enum PdkDeliveryOptionsEvent { @@ -15,5 +16,10 @@ export enum DeliveryOptionsMode { export interface CheckoutDeliveryOptionsSettings { mode: DeliveryOptionsMode; + /** + * Get the package type based on the selected shipping method. + */ + getPackageType(): PackageTypeName | undefined; + updateDeliveryOptions(state: DeliveryOptionsStoreState): InputDeliveryOptionsConfig; } diff --git a/libs/checkout-delivery-options/src/utils/getResolvedSettings.ts b/libs/checkout-delivery-options/src/utils/getResolvedSettings.ts index 8c01fb91..eb575c1b 100644 --- a/libs/checkout-delivery-options/src/utils/getResolvedSettings.ts +++ b/libs/checkout-delivery-options/src/utils/getResolvedSettings.ts @@ -2,17 +2,18 @@ import { type CheckoutDeliveryOptionsSettings, type CheckoutDeliveryOptionsSettingsInput, DeliveryOptionsMode, - type DeliveryOptionsStoreState, } from '../types'; +import {defaultGetPackageType, defaultUpdateDeliveryOptions} from '../defaults'; export const getResolvedSettings = ( settings: CheckoutDeliveryOptionsSettingsInput | undefined, ): CheckoutDeliveryOptionsSettings => { return { mode: DeliveryOptionsMode.Multi, - updateDeliveryOptions(state: DeliveryOptionsStoreState) { - return state.configuration.config; - }, + + getPackageType: defaultGetPackageType, + updateDeliveryOptions: defaultUpdateDeliveryOptions, + ...settings, }; };