Skip to content

Commit 7dc936c

Browse files
committed
perf: Get order from callback
1 parent 2e9c7a2 commit 7dc936c

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

components/composite/StepCustomer/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Order } from "@commercelayer/sdk"
12
import classNames from "classnames"
23
import { Fragment, useContext, useEffect, useState } from "react"
34
import { useTranslation } from "react-i18next"
@@ -113,8 +114,10 @@ export const StepCustomer: React.FC<Props> = () => {
113114
setDisabledShipToDifferentAddress(disableToggle)
114115
}
115116

116-
const handleSave = async () => {
117+
const handleSave = async (params: { success: boolean; order?: Order }) => {
118+
console.log("params", params)
117119
setIsLocalLoader(true)
120+
debugger
118121
await setAddresses()
119122

120123
// it is used temporarily to scroll

components/composite/StepPlaceOrder/index.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Order } from "@commercelayer/sdk"
12
import { useContext, useState } from "react"
23
import { Trans, useTranslation } from "react-i18next"
34

@@ -44,10 +45,16 @@ const StepPlaceOrder: React.FC<Props> = ({
4445

4546
const { placeOrder } = appCtx
4647

47-
const handlePlaceOrder = async ({ placed }: { placed: boolean }) => {
48+
const handlePlaceOrder = async ({
49+
placed,
50+
order,
51+
}: {
52+
placed: boolean
53+
order?: Order
54+
}) => {
4855
if (placed) {
4956
setIsPlacingOrder(true)
50-
await placeOrder()
57+
await placeOrder(order)
5158
if (gtmCtx?.firePurchase && gtmCtx?.fireAddPaymentInfo) {
5259
await gtmCtx.fireAddPaymentInfo()
5360
await gtmCtx.firePurchase()

components/data/AppProvider/index.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import CommerceLayer, {
2-
ShippingMethod as ShippingMethodCollection,
3-
PaymentMethod,
4-
Order,
2+
type ShippingMethod as ShippingMethodCollection,
3+
type PaymentMethod,
4+
type Order,
55
} from "@commercelayer/sdk"
66
import { changeLanguage } from "i18next"
77
import { createContext, useEffect, useReducer, useRef } from "react"
@@ -103,6 +103,7 @@ export const AppProvider: React.FC<AppProviderProps> = ({
103103
})
104104

105105
const getOrder = (order: Order) => {
106+
console.log("order", order)
106107
orderRef.current = order
107108
}
108109

@@ -143,15 +144,13 @@ export const AppProvider: React.FC<AppProviderProps> = ({
143144
})
144145
}
145146

146-
const setAddresses = async () => {
147+
const setAddresses = async (order?: Order) => {
147148
dispatch({ type: ActionType.START_LOADING })
148-
149-
const order = await getOrderFromRef()
150-
149+
const currentOrder = order ?? (await getOrderFromRef())
151150
const isShipmentRequired = await checkIfShipmentRequired(cl, orderId)
152151

153152
const others = calculateSettings(
154-
order,
153+
currentOrder,
155154
isShipmentRequired,
156155
// FIX We are using customer addresses saved in reducer because
157156
// we don't receive them from fetchOrder
@@ -161,7 +160,7 @@ export const AppProvider: React.FC<AppProviderProps> = ({
161160
dispatch({
162161
type: ActionType.SET_ADDRESSES,
163162
payload: {
164-
order,
163+
order: currentOrder,
165164
others,
166165
},
167166
})
@@ -265,13 +264,13 @@ export const AppProvider: React.FC<AppProviderProps> = ({
265264
})
266265
}
267266

268-
const placeOrder = async () => {
267+
const placeOrder = async (order?: Order) => {
269268
dispatch({ type: ActionType.START_LOADING })
270-
const order = await getOrderFromRef()
269+
const currentOrder = order ?? (await getOrderFromRef())
271270

272271
dispatch({
273272
type: ActionType.PLACE_ORDER,
274-
payload: { order },
273+
payload: { order: currentOrder },
275274
})
276275
}
277276

0 commit comments

Comments
 (0)