Skip to content

Commit 34354ab

Browse files
feat: adjust shipping intent logic (#1257)
1 parent 7aaf1fc commit 34354ab

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

.changeset/rich-ends-film.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@godaddy/react": patch
3+
---
4+
5+
Adjust shipping intent logic to clear shippingLines when no new shipping methods are returned

packages/react/src/components/checkout/shipping/shipping-method.tsx

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { useEffect, useRef } from 'react';
22
import { useFormContext } from 'react-hook-form';
33
import { useCheckoutContext } from '@/components/checkout/checkout';
44
import { DeliveryMethods } from '@/components/checkout/delivery/delivery-method';
5-
import { useApplyDeliveryMethod } from '@/components/checkout/delivery/utils/use-apply-delivery-method';
65
import {
76
useDraftOrder,
87
useDraftOrderShipping,
@@ -74,21 +73,20 @@ export function ShippingMethodForm() {
7473
});
7574

7675
const applyShippingMethod = useApplyShippingMethod();
77-
const applyDeliveryMethod = useApplyDeliveryMethod();
7876

7977
// Track the last processed state to avoid duplicate API calls
8078
const lastProcessedStateRef = useRef<{
8179
serviceCode: string | null;
8280
cost: number | null;
8381
hadShippingMethods: boolean;
8482
wasPickup: boolean;
85-
appliedDeliveryMethod: boolean;
83+
clearedShippingMethod: boolean;
8684
}>({
8785
serviceCode: null,
8886
cost: null,
8987
hadShippingMethods: false,
9088
wasPickup: false,
91-
appliedDeliveryMethod: false,
89+
clearedShippingMethod: false,
9290
});
9391

9492
useEffect(() => {
@@ -99,36 +97,24 @@ export function ShippingMethodForm() {
9997
const currentCost = shippingLines?.amount?.value ?? null;
10098
const lastState = lastProcessedStateRef.current;
10199

102-
// Case 1: No shipping methods available
100+
// Case 1: No shipping methods available - clear shipping and set fulfillment to SHIP
103101
if (!hasShippingMethods && hasShippingAddress) {
104-
// If pickup mode, clear the shipping method
105-
if (isPickup && (currentServiceCode || !lastState.wasPickup)) {
106-
form.setValue('shippingMethod', '', { shouldDirty: false });
107-
applyShippingMethod.mutate([]);
108-
lastProcessedStateRef.current = {
109-
serviceCode: null,
110-
cost: null,
111-
hadShippingMethods: false,
112-
wasPickup: true,
113-
appliedDeliveryMethod: false,
114-
};
115-
return;
116-
}
102+
// Apply empty shipping method if:
103+
// - Pickup mode and has shipping code OR wasn't pickup before
104+
// - Shipping mode and (had methods before OR haven't cleared yet)
105+
const shouldClearShipping = isPickup
106+
? currentServiceCode || !lastState.wasPickup
107+
: lastState.hadShippingMethods || !lastState.clearedShippingMethod;
117108

118-
// If shipping mode with no methods, apply SHIP delivery method
119-
// Apply if: transitioning from having methods OR haven't applied it yet
120-
if (
121-
!isPickup &&
122-
(lastState.hadShippingMethods || !lastState.appliedDeliveryMethod)
123-
) {
109+
if (shouldClearShipping) {
124110
form.setValue('shippingMethod', '', { shouldDirty: false });
125-
applyDeliveryMethod.mutate(DeliveryMethods.SHIP);
111+
applyShippingMethod.mutate([]);
126112
lastProcessedStateRef.current = {
127113
serviceCode: null,
128114
cost: null,
129115
hadShippingMethods: false,
130-
wasPickup: false,
131-
appliedDeliveryMethod: true,
116+
wasPickup: isPickup,
117+
clearedShippingMethod: true,
132118
};
133119
}
134120
return;
@@ -182,7 +168,7 @@ export function ShippingMethodForm() {
182168
cost: methodCost,
183169
hadShippingMethods: true,
184170
wasPickup: false,
185-
appliedDeliveryMethod: false,
171+
clearedShippingMethod: false,
186172
};
187173
}
188174
}
@@ -193,7 +179,6 @@ export function ShippingMethodForm() {
193179
isShippingMethodsLoading,
194180
form,
195181
applyShippingMethod,
196-
applyDeliveryMethod,
197182
updateTaxes.mutate,
198183
session?.enableTaxCollection,
199184
isPickup,

0 commit comments

Comments
 (0)