Skip to content

Commit

Permalink
fix: fix incorrect uhhh data in uhhh store
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 19, 2024
1 parent 6070318 commit ba5605a
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 59 deletions.
7 changes: 3 additions & 4 deletions libs/checkout-common/src/__tests__/getMockCheckoutContext.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {vi} from 'vitest';
import {FrontendEndpoint} from '@myparcel-pdk/common';
import {FrontendEndpoint, type Plugin} from '@myparcel-pdk/common';
import {KEY_CONFIG} from '@myparcel/delivery-options';
import {PackageTypeName} from '@myparcel/constants';
import {type CheckoutAppCheckoutContext} from '../types';
import {getMockDeliveryOptionsConfig} from './getMockDeliveryOptionsConfig';

export const getMockCheckoutContext = vi.fn(
(context?: Partial<CheckoutAppCheckoutContext>): CheckoutAppCheckoutContext => {
(context?: Partial<Plugin.ModelContextCheckoutContext>): Plugin.ModelContextCheckoutContext => {
return {
settings: {
hasDeliveryOptions: true,
Expand Down Expand Up @@ -40,6 +39,6 @@ export const getMockCheckoutContext = vi.fn(
},
[KEY_CONFIG]: getMockDeliveryOptionsConfig(),
...context,
} as CheckoutAppCheckoutContext;
} as Plugin.ModelContextCheckoutContext;
},
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {type CheckoutAppContext} from '../types';
import {ATTRIBUTE_CONTEXT} from '../constants';
import {getMockCheckoutContext} from './getMockCheckoutContext';

Expand All @@ -12,10 +11,7 @@ export const mockDeliveryOptionsElement = (): void => {
wrapper.id = 'delivery-options-wrapper';
element.id = 'delivery-options';

wrapper.setAttribute(
ATTRIBUTE_CONTEXT,
JSON.stringify({checkout: getMockCheckoutContext()} satisfies CheckoutAppContext),
);
wrapper.setAttribute(ATTRIBUTE_CONTEXT, JSON.stringify({checkout: getMockCheckoutContext()}));

wrapper.appendChild(element);
form.appendChild(wrapper);
Expand Down
36 changes: 2 additions & 34 deletions libs/checkout-common/src/types/checkout.types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import {type FrontendEndpoint, type ShippingMethodTypeMap, type TriState} from '@myparcel-pdk/common';
import {type FrontendEndpoint} from '@myparcel-pdk/common';
import {type PromiseOr} from '@myparcel/ts-utils';
import {type InputDeliveryOptionsConfiguration} from '@myparcel/delivery-options';
import {type CarrierName} from '@myparcel/constants';
import {type AddressType, type PdkField} from '../data';
import {
type FrontendEndpointData,
type FrontendEndpointResponse,
type FrontendPdkEndpointObject,
} from './endpoints.types';
import {type FrontendEndpointData, type FrontendEndpointResponse} from './endpoints.types';
import {type AddressFields} from './address.types';

export type PdkFormData = Record<string, FormDataEntryValue | undefined>;
Expand Down Expand Up @@ -90,29 +84,3 @@ export type InitializeCallback = () => void;
export interface PdkCheckout {
onInitialize(callback: InitializeCallback): void;
}

export interface CheckoutAppCheckoutContext extends InputDeliveryOptionsConfiguration {
settings: CheckoutSettings;
}

export interface CheckoutAppContext {
checkout: CheckoutAppCheckoutContext;
}

export type CheckoutSettings = {
actions: {
baseUrl: string;
endpoints: FrontendPdkEndpointObject;
};

// Delivery options
allowedShippingMethods: Omit<ShippingMethodTypeMap, TriState.Off>;
hasDeliveryOptions: boolean;
hiddenInputName: string;

// Separate address fields
countriesWithSeparateAddressFields: string[];

// Tax fields
carriersWithTaxFields: CarrierName[];
};
6 changes: 3 additions & 3 deletions libs/checkout-common/src/types/store.types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {type Keyable} from '@myparcel-pdk/common';
import {type Keyable, type Plugin} from '@myparcel-pdk/common';
import {type PromiseOr} from '@myparcel/ts-utils';
import {type AddressType, type StoreListener} from '../data';
import {type CheckoutAppCheckoutContext, type PdkCheckoutForm} from './checkout.types';
import {type PdkCheckoutForm} from './checkout.types';

type StoreListeners<T extends StoreState> = {
[StoreListener.Update]: StoreCallbackUpdate<T>;
Expand Down Expand Up @@ -32,7 +32,7 @@ export type Store<S extends StoreState = StoreState> = {
export type CheckoutStoreState = {
addressType: AddressType;
addressTypes: AddressType[];
context: CheckoutAppCheckoutContext;
context: Plugin.ModelContextCheckoutContext;
form: PdkCheckoutForm;
};

Expand Down
9 changes: 5 additions & 4 deletions libs/checkout-common/src/utils/getFrontendContext.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {type CheckoutAppContext} from '../types';
import {type Plugin} from '@myparcel-pdk/common';
import {ATTRIBUTE_CONTEXT} from '../constants';
import {useConfig} from './useConfig';
import {getElement} from './global';

export const getFrontendContext = (): CheckoutAppContext['checkout'] => {
export const getFrontendContext = (): Plugin.ModelContextCheckoutContext => {
const config = useConfig();

const wrapper = getElement(config.selectors.deliveryOptionsWrapper, false);
Expand All @@ -15,7 +15,8 @@ export const getFrontendContext = (): CheckoutAppContext['checkout'] => {

wrapper.removeAttribute(ATTRIBUTE_CONTEXT);

const {checkout} = JSON.parse(context) as CheckoutAppContext;
const {checkout} = JSON.parse(context) as Plugin.ModelContextContextBag;

return checkout;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return checkout!;
};
4 changes: 2 additions & 2 deletions libs/checkout-common/src/utils/useSettings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {type CheckoutSettings} from '../types';
import {type Plugin} from '@myparcel-pdk/common';

export const useSettings = (): CheckoutSettings => {
export const useSettings = (): Plugin.ModelContextCheckoutContext['settings'] => {
return window.MyParcelPdk.stores.checkout.state.context.settings;
};
7 changes: 2 additions & 5 deletions libs/checkout-delivery-options/src/utils/updateContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {type Plugin} from '@myparcel-pdk/common';
import {useCheckoutStore} from '@myparcel-pdk/checkout-common';
import {useDeliveryOptionsStore} from './useDeliveryOptionsStore';
import {fetchCheckoutContext} from './fetchCheckoutContext';
Expand All @@ -7,14 +6,14 @@ import {fetchCheckoutContext} from './fetchCheckoutContext';
* Fetch and update the delivery options config. For use with changing shipping methods, for example, as doing so
* changes the prices of delivery and any extra options.
*/
export const updateContext = async (): Promise<Plugin.ModelContextCheckoutContext> => {
export const updateContext = async (): Promise<void> => {
const context = await fetchCheckoutContext();

Check warning on line 10 in libs/checkout-delivery-options/src/utils/updateContext.ts

View check run for this annotation

Codecov / codecov/patch

libs/checkout-delivery-options/src/utils/updateContext.ts#L10

Added line #L10 was not covered by tests

const checkout = useCheckoutStore();
const deliveryOptions = useDeliveryOptionsStore();

await Promise.all([
checkout.set(context.settings),
checkout.set({context}),

Check warning on line 16 in libs/checkout-delivery-options/src/utils/updateContext.ts

View check run for this annotation

Codecov / codecov/patch

libs/checkout-delivery-options/src/utils/updateContext.ts#L16

Added line #L16 was not covered by tests
deliveryOptions.set({
configuration: {
...deliveryOptions.state.configuration,
Expand All @@ -29,6 +28,4 @@ export const updateContext = async (): Promise<Plugin.ModelContextCheckoutContex
},
}),
]);

return context;
};
26 changes: 24 additions & 2 deletions libs/common/src/types/php-pdk.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

import {type SupportedDeliveryTypeName, type SupportedPackageTypeName} from '@myparcel/delivery-options';
import {type CarrierId, type CarrierName} from '@myparcel/constants';
import {type FrontendEndpoint, type TriState} from '../data';
import {type ShippingMethodTypeMap} from './pluginSettings.types';
import {type DateTime, type DateTimeImmutable} from './generic.types';
import {type EndpointObject} from './endpoints.types';

export namespace Account {
export type GetAccountsRequest = Base.Request;
Expand Down Expand Up @@ -367,10 +370,29 @@ export namespace Plugin {
carrierSettings: unknown[];
};

export type ModelContextCheckoutContextSettings = {
actions: {
baseUrl: string;
endpoints: EndpointObject<FrontendEndpoint>;
};

// Delivery options
allowedShippingMethods: Omit<ShippingMethodTypeMap, TriState.Off>;
hasDeliveryOptions: boolean;
hiddenInputName: string;

// Separate address fields
countriesWithSeparateAddressFields: string[];

// Tax fields
carriersWithTaxFields: CarrierName[];
};

export type ModelContextCheckoutContext = {
config: Shipment.ModelDeliveryOptions;
endpoints: EndpointRequestCollection;
settings: ModelContextCheckoutContextSettings;
strings: Record<string, string>;
config?: Shipment.ModelDeliveryOptions;
settings: Record<string, unknown>;
};

export type ModelContextGlobalContext = {
Expand Down

0 comments on commit ba5605a

Please sign in to comment.