Skip to content

Commit 00d90bc

Browse files
committed
feat(core): CHECKOUT-9513 Pass in initial state in separate microtask
1 parent 1c54ac3 commit 00d90bc

File tree

7 files changed

+139
-143
lines changed

7 files changed

+139
-143
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"prettier": "@bigcommerce/eslint-config/prettier",
4949
"homepage": "https://github.com/bigcommerce/checkout-js#readme",
5050
"dependencies": {
51-
"@bigcommerce/checkout-sdk": "github:bigcommerce/checkout-sdk-js#dbc080e6cd4d6647ec15d5c50916e9a70cd31007",
51+
"@bigcommerce/checkout-sdk": "github:bigcommerce/checkout-sdk-js#f2438c3ba5fa581035772cbc7bc9e909ea00c1f2",
5252
"@bigcommerce/citadel": "^2.15.1",
5353
"@bigcommerce/form-poster": "^1.2.2",
5454
"@bigcommerce/memoize": "^1.0.0",

packages/core/src/app/billing/Billing.test.tsx

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,15 @@ describe('Billing step', () => {
193193
});
194194

195195
it('should show order comments', async () => {
196-
checkoutService = createCheckoutService({
197-
initialState: {
198-
config: checkoutSettings,
199-
checkout: {
200-
...checkoutWithBillingEmail,
201-
cart: checkoutWithDigitalCart.cart,
202-
},
203-
formFields,
196+
checkoutService = createCheckoutService();
197+
checkoutService.hydrateInitialState({
198+
config: checkoutSettings,
199+
checkout: {
200+
...checkoutWithBillingEmail,
201+
cart: checkoutWithDigitalCart.cart,
204202
},
203+
formFields,
204+
extensions: [],
205205
});
206206

207207
render(<CheckoutTest {...defaultProps} />);
@@ -212,18 +212,18 @@ describe('Billing step', () => {
212212
});
213213

214214
it('should show PoweredByPayPalFastlaneLabel', async () => {
215-
checkoutService = createCheckoutService({
216-
initialState: {
217-
config: checkoutSettings,
218-
checkout: {
219-
...checkoutWithShipping,
220-
billingAddress:checkoutWithBillingEmail.billingAddress,
221-
payments:[
222-
getCheckoutPayment(),
223-
],
224-
},
225-
formFields,
215+
checkoutService = createCheckoutService();
216+
checkoutService.hydrateInitialState({
217+
config: checkoutSettings,
218+
checkout: {
219+
...checkoutWithShipping,
220+
billingAddress:checkoutWithBillingEmail.billingAddress,
221+
payments:[
222+
getCheckoutPayment(),
223+
],
226224
},
225+
formFields,
226+
extensions: [],
227227
});
228228

229229
render(<CheckoutTest {...defaultProps} />);
@@ -302,12 +302,12 @@ describe('Billing step', () => {
302302

303303
describe('registered customer', () => {
304304
it('completes the billing step after selecting a valid address', async () => {
305-
checkoutService = createCheckoutService({
306-
initialState: {
307-
config: checkoutSettings,
308-
checkout: checkoutWithCustomer,
309-
formFields,
310-
},
305+
checkoutService = createCheckoutService();
306+
checkoutService.hydrateInitialState({
307+
config: checkoutSettings,
308+
checkout: checkoutWithCustomer,
309+
formFields,
310+
extensions: [],
311311
});
312312

313313
jest.spyOn(checkoutService, 'updateBillingAddress');
@@ -368,12 +368,12 @@ describe('Billing step', () => {
368368
phone: shippingAddress3.phone,
369369
} as BillingAddress;
370370

371-
checkoutService = createCheckoutService({
372-
initialState: {
373-
config: checkoutSettings,
374-
checkout: checkoutWithCustomer,
375-
formFields,
376-
},
371+
checkoutService = createCheckoutService();
372+
checkoutService.hydrateInitialState({
373+
config: checkoutSettings,
374+
checkout: checkoutWithCustomer,
375+
formFields,
376+
extensions: [],
377377
});
378378

379379
jest.spyOn(checkoutService, 'updateBillingAddress');
@@ -423,12 +423,12 @@ describe('Billing step', () => {
423423
});
424424

425425
it('completes the billing step after creating a new address even with existing addresses', async () => {
426-
checkoutService = createCheckoutService({
427-
initialState: {
428-
config: checkoutSettings,
429-
checkout: checkoutWithCustomer,
430-
formFields,
431-
},
426+
checkoutService = createCheckoutService();
427+
checkoutService.hydrateInitialState({
428+
config: checkoutSettings,
429+
checkout: checkoutWithCustomer,
430+
formFields,
431+
extensions: [],
432432
});
433433

434434
jest.spyOn(checkoutService, 'updateBillingAddress');

packages/core/src/app/checkout/CheckoutApp.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const CheckoutApp = (props: CheckoutAppProps): ReactElement => {
3535
const checkoutService = useMemo(() => createCheckoutService({
3636
locale: getLanguageService().getLocale(),
3737
shouldWarnMutation: process.env.NODE_ENV === 'development',
38-
initialState: props.initialState,
3938
}), []);
4039
const embeddedStylesheet = useMemo(() => createEmbeddedCheckoutStylesheet(), []);
4140
const embeddedSupport = useMemo(() => createEmbeddedCheckoutSupport(getLanguageService()), []);

packages/core/src/app/checkout/CheckoutIntermediate.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
type CheckoutInitialState,
23
type EmbeddedCheckoutMessenger,
34
type EmbeddedCheckoutMessengerOptions
45
} from '@bigcommerce/checkout-sdk';
@@ -19,12 +20,13 @@ interface CheckoutIntermediateProps {
1920
embeddedStylesheet: EmbeddedCheckoutStylesheet;
2021
embeddedSupport: CheckoutSupport;
2122
errorLogger: ErrorLogger;
23+
initialState?: CheckoutInitialState;
2224
createEmbeddedMessenger(options: EmbeddedCheckoutMessengerOptions): EmbeddedCheckoutMessenger;
2325
}
2426

2527
const CheckoutIntermediate:React.FC<CheckoutIntermediateProps>= (props) => {
26-
const { checkoutId } = props;
27-
const { isLoadingCheckout } = useLoadCheckout(checkoutId);
28+
const { checkoutId, initialState } = props;
29+
const { isLoadingCheckout } = useLoadCheckout(checkoutId, initialState);
2830
const { themeV2 } = useThemeContext();
2931

3032
if (isLoadingCheckout) {

packages/core/src/app/checkout/hooks/useLoadCheckout.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
import { type CheckoutInitialState } from '@bigcommerce/checkout-sdk';
12
import { useEffect, useState } from 'react';
23

34
import { useExtensions } from '@bigcommerce/checkout/checkout-extension';
45
import { useCheckout } from '@bigcommerce/checkout/payment-integration-api';
56

6-
export const useLoadCheckout = (checkoutId: string): {isLoadingCheckout: boolean} => {
7-
const { checkoutService, checkoutState: { data } } = useCheckout();
8-
const initialStateLoaded = !!data.getCheckout();
9-
const [ isLoadingCheckout, setIsLoadingCheckout ] = useState(!initialStateLoaded);
7+
export const useLoadCheckout = (checkoutId: string, initialState?: CheckoutInitialState): {isLoadingCheckout: boolean} => {
8+
const { checkoutService } = useCheckout();
9+
const [ isLoadingCheckout, setIsLoadingCheckout ] = useState(true);
1010
const { extensionService } = useExtensions();
1111

1212
const fetchData = async () => {
@@ -44,13 +44,18 @@ export const useLoadCheckout = (checkoutId: string): {isLoadingCheckout: boolean
4444
};
4545

4646
useEffect(() => {
47-
if (!initialStateLoaded) {
47+
if (!initialState) {
4848
// If the initial data has not been preloaded from the server, we need to make API calls to fetch it.
4949
fetchDataWithRetry()
5050
.then(() => setIsLoadingCheckout(false))
5151
.catch((error) => {
5252
throw error;
5353
});
54+
} else if (initialState) {
55+
requestAnimationFrame(async () => {
56+
await checkoutService.hydrateInitialState(initialState);
57+
setIsLoadingCheckout(false);
58+
});
5459
}
5560
}, []);
5661

0 commit comments

Comments
 (0)