Skip to content

Commit b375f19

Browse files
committed
perf: speed up checkout bootstrap
1 parent e196446 commit b375f19

27 files changed

Lines changed: 207 additions & 461 deletions

components/composite/OrderSummary/CouponOrGiftCard/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
GiftCardOrCouponSubmit,
44
GiftCardOrCouponForm,
55
} from "@commercelayer/react-components"
6-
import { ErrorComponentProps } from "@commercelayer/react-components/lib/cjs/typings/errors"
6+
import { ErrorComponentProps } from "@commercelayer/react-components/lib/esm/typings/errors"
77
import { useContext, useState } from "react"
88
import { useTranslation } from "react-i18next"
99

components/composite/OrderSummary/LineItemTypes/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
LineItemOption,
99
LineItemCode,
1010
} from "@commercelayer/react-components"
11-
import { LineItemType } from "@commercelayer/react-components/lib/cjs/typings"
11+
import { LineItemType } from "@commercelayer/react-components/lib/esm/typings"
1212
import { useTranslation } from "react-i18next"
1313
import styled from "styled-components"
1414
import tw from "twin.macro"

components/composite/OrderSummary/index.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import {
1111
GiftCardAmount,
1212
} from "@commercelayer/react-components"
1313
import { Trans, useTranslation } from "react-i18next"
14+
import "twin.macro"
1415

1516
import { AppProviderData } from "components/data/AppProvider"
16-
import "twin.macro"
17+
import { LINE_ITEMS_SHOPPABLE } from "components/utils/constants"
1718

1819
import { CouponOrGiftCard } from "./CouponOrGiftCard"
1920
import { LineItemTypes } from "./LineItemTypes"
@@ -50,7 +51,7 @@ export const OrderSummary: React.FC<Props> = ({ appCtx, readonly }) => {
5051
<SummarySubTitle>
5152
<LineItemsCount
5253
data-cy="items-count"
53-
typeAccepted={["skus", "gift_cards", "bundles"]}
54+
typeAccepted={LINE_ITEMS_SHOPPABLE}
5455
>
5556
{(props) => (
5657
<span data-cy="items-count">
@@ -61,9 +62,9 @@ export const OrderSummary: React.FC<Props> = ({ appCtx, readonly }) => {
6162
</SummarySubTitle>
6263
</SummaryHeader>
6364
)}
64-
<LineItemTypes type="skus" />
65-
<LineItemTypes type="bundles" />
66-
<LineItemTypes type="gift_cards" />
65+
{LINE_ITEMS_SHOPPABLE.map((type) => (
66+
<LineItemTypes type={type} key={type} />
67+
))}
6768
</LineItemsContainer>
6869
<TotalWrapper>
6970
<AmountSpacer />

components/composite/StepCustomer/AddressInputGroup/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import {
99
AddressInputName,
1010
AddressStateSelectName,
1111
BaseInputType,
12-
} from "@commercelayer/react-components/lib/cjs/typings"
12+
} from "@commercelayer/react-components/lib/esm/typings"
1313
import {
1414
ErrorComponentProps,
1515
ResourceErrorType,
16-
} from "@commercelayer/react-components/lib/cjs/typings/errors"
16+
} from "@commercelayer/react-components/lib/esm/typings/errors"
1717
import { useContext, useEffect, useState } from "react"
1818
import { useTranslation } from "react-i18next"
1919
import styled from "styled-components"

components/composite/StepCustomer/AddressSectionEmail/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CustomerInput, Errors } from "@commercelayer/react-components"
2-
import { ErrorComponentProps } from "@commercelayer/react-components/lib/cjs/typings/errors"
2+
import { ErrorComponentProps } from "@commercelayer/react-components/lib/esm/typings/errors"
33
import { useTranslation } from "react-i18next"
44
import styled from "styled-components"
55
import tw from "twin.macro"

components/composite/StepPayment/CheckoutCustomerPayment.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
PaymentMethod,
44
PaymentSource,
55
} from "@commercelayer/react-components"
6-
import { CustomerSaveToWalletProps } from "@commercelayer/react-components/lib/cjs/components/PaymentSource"
6+
import { CustomerSaveToWalletProps } from "@commercelayer/react-components/lib/esm/components/PaymentSource"
77
import { MouseEvent, useState } from "react"
88
import { useTranslation } from "react-i18next"
99
import "twin.macro"

components/composite/StepPlaceOrder/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PlaceOrderContainer } from "@commercelayer/react-components"
2-
import { ErrorComponentProps } from "@commercelayer/react-components/lib/cjs/typings/errors"
2+
import { ErrorComponentProps } from "@commercelayer/react-components/lib/esm/typings/errors"
33
import { useRouter } from "next/router"
44
import { useContext, useState } from "react"
55
import { Trans, useTranslation } from "react-i18next"

components/composite/StepShipping/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
DeliveryLeadTime,
1515
ShipmentField,
1616
} from "@commercelayer/react-components"
17-
import { LineItemType } from "@commercelayer/react-components/lib/cjs/typings"
17+
import { LineItemType } from "@commercelayer/react-components/lib/esm/typings"
1818
import { ShippingMethod as ShippingMethodCollection } from "@commercelayer/sdk"
1919
import classNames from "classnames"
2020
import { useTranslation, Trans } from "next-i18next"
@@ -29,6 +29,7 @@ import { SpinnerIcon } from "components/ui/SpinnerIcon"
2929
import { StepContainer } from "components/ui/StepContainer"
3030
import { StepContent } from "components/ui/StepContent"
3131
import { StepHeader } from "components/ui/StepHeader"
32+
import { LINE_ITEMS_SHIPPABLE } from "components/utils/constants"
3233

3334
import {
3435
ShippingWrapper,
@@ -89,7 +90,7 @@ export const StepHeaderShipping: React.FC<HeaderProps> = ({ step }) => {
8990
)
9091
}
9192

92-
const ShippingLineItems: LineItemType[] = ["skus", "bundles"]
93+
const ShippingLineItems: LineItemType[] = LINE_ITEMS_SHIPPABLE
9394

9495
export const StepShipping: React.FC<Props> = () => {
9596
const appCtx = useContext(AppContext)

components/data/AppProvider/fetchOrderById.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { TypeAccepted } from "@commercelayer/react-components/lib/esm/utils/getLineItemsCount"
12
import CommerceLayer, {
23
Order,
34
Address,
@@ -15,6 +16,8 @@ import CommerceLayer, {
1516
} from "@commercelayer/sdk"
1617
import i18n from "i18next"
1718

19+
import { LINE_ITEMS_SHIPPABLE } from "components/utils/constants"
20+
1821
interface FetchOrderByIdProps {
1922
orderId: string
2023
accessToken: string
@@ -198,7 +201,7 @@ async function checkIfShipmentRequired(
198201
})
199202
).line_items?.filter(
200203
(line_item) =>
201-
(line_item.item_type === "skus" || line_item.item_type === "bundles") &&
204+
LINE_ITEMS_SHIPPABLE.includes(line_item.item_type as TypeAccepted) &&
202205
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
203206
// @ts-ignore
204207
!line_item.item?.do_not_ship
@@ -226,6 +229,29 @@ export const fetchOrderById = async ({
226229
try {
227230
const fetchOrder = async () => {
228231
return cl.orders.retrieve(orderId, {
232+
fields: {
233+
orders: [
234+
"id",
235+
"guest",
236+
"shipping_country_code_lock",
237+
"customer_email",
238+
"status",
239+
"return_url",
240+
"tax_included",
241+
"requires_billing_info",
242+
"total_amount_with_taxes_float",
243+
"language_code",
244+
"shipping_address",
245+
"billing_address",
246+
"shipments",
247+
"payment_method",
248+
"payment_source",
249+
"customer",
250+
],
251+
shipments: ["shipping_method"],
252+
customer: ["customer_addresses"],
253+
customer_addresses: ["address"],
254+
},
229255
include: [
230256
"shipping_address",
231257
"billing_address",
@@ -409,9 +435,11 @@ export const fetchOrderById = async ({
409435
paymentMethod?.payment_source_type === "stripe_payments" ||
410436
paymentMethod?.payment_source_type === "braintree_payments"
411437

412-
const allAvailablePaymentMethods = (await cl.payment_methods.list()).filter(
413-
({ disabled_at }) => disabled_at === null
414-
)
438+
const allAvailablePaymentMethods = (
439+
await cl.payment_methods.list({
440+
fields: { payment_methods: ["disabled_at", "id"] },
441+
})
442+
).filter(({ disabled_at }) => disabled_at === null)
415443

416444
// If we have a customer with a single payment method
417445
// the payment method is automatically selected
@@ -420,7 +448,8 @@ export const fetchOrderById = async ({
420448
//! isGuest &&
421449
isPaymentRequired &&
422450
!hasPaymentMethod &&
423-
allAvailablePaymentMethods.length === 1
451+
allAvailablePaymentMethods.length === 1 &&
452+
!order.payment_method?.id
424453
) {
425454
try {
426455
await cl.orders.update({

components/data/GTMProvider/index.tsx

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { TypeAccepted } from "@commercelayer/react-components/lib/esm/utils/getLineItemsCount"
12
import CommerceLayer, { LineItem } from "@commercelayer/sdk"
23
import { createContext, useEffect, useContext } from "react"
34
import TagManager from "react-gtm-module"
45

56
import { AppContext } from "components/data/AppProvider"
7+
import { LINE_ITEMS_SHOPPABLE } from "components/utils/constants"
68

79
interface GTMProviderData {
810
fireAddShippingInfo: () => Promise<void>
@@ -73,12 +75,6 @@ export const GTMProvider: React.FC<GTMProviderProps> = ({
7375

7476
const fetchOrder = async () => {
7577
return cl.orders.retrieve(orderId, {
76-
include: [
77-
"line_items",
78-
"shipments",
79-
"shipments.shipping_method",
80-
"payment_method",
81-
],
8278
fields: {
8379
orders: [
8480
"number",
@@ -89,7 +85,9 @@ export const GTMProvider: React.FC<GTMProviderProps> = ({
8985
"total_tax_amount_float",
9086
"payment_method",
9187
],
88+
payment_method: ["price_amount_float", "name"],
9289
},
90+
include: ["payment_method"],
9391
})
9492
}
9593

@@ -141,11 +139,7 @@ export const GTMProvider: React.FC<GTMProviderProps> = ({
141139
},
142140
})
143141
).line_items?.filter((line_item) => {
144-
return (
145-
line_item.item_type === "skus" ||
146-
line_item.item_type === "gift_cards" ||
147-
line_item.item_type === "bundles"
148-
)
142+
return LINE_ITEMS_SHOPPABLE.includes(line_item.item_type as TypeAccepted)
149143
})
150144

151145
return pushDataLayer({
@@ -223,11 +217,7 @@ export const GTMProvider: React.FC<GTMProviderProps> = ({
223217
},
224218
})
225219
).line_items?.filter((line_item) => {
226-
return (
227-
line_item.item_type === "skus" ||
228-
line_item.item_type === "gift_cards" ||
229-
line_item.item_type === "bundles"
230-
)
220+
return LINE_ITEMS_SHOPPABLE.includes(line_item.item_type as TypeAccepted)
231221
})
232222

233223
const paymentMethod = order.payment_method
@@ -262,11 +252,7 @@ export const GTMProvider: React.FC<GTMProviderProps> = ({
262252
},
263253
})
264254
).line_items?.filter((line_item) => {
265-
return (
266-
line_item.item_type === "skus" ||
267-
line_item.item_type === "gift_cards" ||
268-
line_item.item_type === "bundles"
269-
)
255+
return LINE_ITEMS_SHOPPABLE.includes(line_item.item_type as TypeAccepted)
270256
})
271257

272258
return pushDataLayer({

0 commit comments

Comments
 (0)