Skip to content

Commit a6e1a30

Browse files
committed
fix: check for customer email if addresses already present
1 parent 8c9626d commit a6e1a30

9 files changed

Lines changed: 337 additions & 43 deletions

File tree

components/data/AppProvider/index.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,22 @@ export const AppProvider: React.FC<AppProviderProps> = ({
133133

134134
const setAddresses = async () => {
135135
dispatch({ type: ActionType.START_LOADING })
136+
137+
// TODO Set shipping method if only one
138+
136139
const order = await cl.orders.retrieve(orderId, {
137140
fields: {
138141
orders: ["shipping_address", "billing_address", "shipments"],
142+
shipments: ["shipping_method"],
139143
},
140-
include: ["shipping_address", "billing_address", "shipments"],
144+
include: [
145+
"shipping_address",
146+
"billing_address",
147+
"shipments",
148+
"shipments.shipping_method",
149+
],
141150
})
142151

143-
// TODO Set shipping method if only one
144-
145152
dispatch({
146153
type: ActionType.SET_ADDRESSES,
147154
payload: {

components/data/AppProvider/reducer.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
calculateAddresses,
1313
calculateSelectedShipments,
1414
creditCardPayment,
15+
hasShippingMethodSet,
1516
} from "components/data/AppProvider/utils"
1617

1718
export enum ActionType {
@@ -101,15 +102,20 @@ export function reducer(state: AppStateData, action: Action): AppStateData {
101102
hasEmailAddress: Boolean(action.payload.customerEmail),
102103
isLoading: false,
103104
}
104-
case ActionType.SET_ADDRESSES:
105+
case ActionType.SET_ADDRESSES: {
106+
const preparedShipments = prepareShipments(action.payload.order.shipments)
107+
let { hasShippingMethod } = hasShippingMethodSet(preparedShipments)
108+
if (!state.isShipmentRequired) {
109+
hasShippingMethod = true
110+
}
105111
return {
106112
...state,
107113
...calculateAddresses(action.payload.order, state.customerAddresses),
108-
shipments: state.isShipmentRequired
109-
? prepareShipments(action.payload.order.shipments)
110-
: [],
114+
shipments: state.isShipmentRequired ? preparedShipments : [],
115+
hasShippingMethod,
111116
isLoading: false,
112117
}
118+
}
113119
case ActionType.SELECT_SHIPMENT: {
114120
return {
115121
...state,

components/data/AppProvider/utils.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,6 @@ export async function checkIfShipmentRequired(
264264
return lineItems.length > 0
265265
}
266266

267-
export function prepareShipments(shipments?: Shipment[]) {
268-
return (shipments || []).map((a) => {
269-
return {
270-
shipmentId: a.id,
271-
shippingMethodId: a.shipping_method?.id,
272-
shippingMethodName: a.shipping_method?.name,
273-
}
274-
})
275-
}
276-
277267
function isPaymentRequired(order: Order) {
278268
return !(order.total_amount_with_taxes_float === 0)
279269
}
@@ -392,13 +382,26 @@ export function calculateSelectedShipments(
392382
}
393383
: shipment
394384
})
385+
const hasShippingMethod = hasShippingMethodSet(shipmentsSelected)
386+
return { shipments: shipmentsSelected, ...hasShippingMethod }
387+
}
388+
389+
export function prepareShipments(shipments?: Shipment[]) {
390+
return (shipments || []).map((a) => {
391+
return {
392+
shipmentId: a.id,
393+
shippingMethodId: a.shipping_method?.id,
394+
shippingMethodName: a.shipping_method?.name,
395+
}
396+
})
397+
}
395398

396-
const shippingMethods = shipmentsSelected?.map(
399+
export function hasShippingMethodSet(shipments: ShipmentSelected[]) {
400+
const shippingMethods = shipments?.map(
397401
(a: ShipmentSelected) => a.shippingMethodId
398402
)
399403
const hasShippingMethod = Boolean(
400404
shippingMethods?.length && !shippingMethods?.includes(undefined)
401405
)
402-
403-
return { shipments: shipmentsSelected, hasShippingMethod }
406+
return { hasShippingMethod }
404407
}

components/hooks/useActiveStep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const useActiveStep = (): UseActiveStep => {
5454
}
5555

5656
const canSelectCustomerAddress =
57-
ctx.hasShippingAddress && ctx.hasBillingAddress
57+
ctx.hasShippingAddress && ctx.hasBillingAddress && ctx.hasEmailAddress
5858
const canSelectShippingMethod =
5959
canSelectCustomerAddress &&
6060
(ctx.hasShippingAddress || !ctx.isShipmentRequired)

playwright.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const config: PlaywrightTestConfig = {
1010
retries: 0,
1111
// Artifacts folder where screenshots, videos, and traces are stored.
1212
outputDir: "test-results/",
13+
workers: 1,
14+
maxFailures: 2,
1315

1416
// Run your local dev server before starting the tests:
1517
// https://playwright.dev/docs/test-advanced#launching-a-development-web-server-during-the-tests

0 commit comments

Comments
 (0)