Skip to content

Commit bfdd630

Browse files
Apply shipping intent (#1255)
* apply delivery method for shipping intent if no methods found * add changeset
1 parent e3ba09b commit bfdd630

File tree

4 files changed

+55
-27
lines changed

4 files changed

+55
-27
lines changed

.changeset/cute-ravens-kneel.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+
Apply shipping intent when no shipping methods returned

packages/react/package.json

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
"version": "1.0.15",
55
"type": "module",
66
"types": "./dist/index.d.ts",
7-
"files": [
8-
"dist"
9-
],
7+
"files": ["dist"],
108
"exports": {
119
"./package.json": "./package.json",
1210
"./styles.css": "./dist/index.css",
@@ -24,12 +22,8 @@
2422
},
2523
"typesVersions": {
2624
"*": {
27-
"server": [
28-
"./dist/server.d.ts"
29-
],
30-
"*": [
31-
"./dist/index.d.ts"
32-
]
25+
"server": ["./dist/server.d.ts"],
26+
"*": ["./dist/index.d.ts"]
3327
}
3428
},
3529
"scripts": {
@@ -39,7 +33,7 @@
3933
"preview": "vite preview",
4034
"typecheck": "tsc --noEmit",
4135
"test": "vitest run",
42-
"lint": "biome check .",
36+
"lint": "biome check ./src",
4337
"lint:fix": "biome check --write --unsafe ./src",
4438
"prepublishOnly": "pnpm run build"
4539
},

packages/react/src/components/checkout/delivery/utils/use-apply-delivery-method.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useMutation } from '@tanstack/react-query';
1+
import { useMutation, useQueryClient } from '@tanstack/react-query';
22
import { useCheckoutContext } from '@/components/checkout/checkout';
33
import { useGoDaddyContext } from '@/godaddy-provider';
44
import { applyDeliveryMethod } from '@/lib/godaddy/godaddy';
@@ -7,6 +7,7 @@ import type { ApplyCheckoutSessionDeliveryMethodInput } from '@/types';
77
export function useApplyDeliveryMethod() {
88
const { session, jwt } = useCheckoutContext();
99
const { apiHost } = useGoDaddyContext();
10+
const queryClient = useQueryClient();
1011

1112
return useMutation({
1213
mutationKey: session?.id
@@ -21,5 +22,11 @@ export function useApplyDeliveryMethod() {
2122
: await applyDeliveryMethod({ mode }, session, apiHost);
2223
return data;
2324
},
25+
onSuccess: () => {
26+
if (!session) return;
27+
queryClient.invalidateQueries({
28+
queryKey: ['draft-order', session.id],
29+
});
30+
},
2431
});
2532
}

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

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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';
56
import {
67
useDraftOrder,
78
useDraftOrderShipping,
@@ -73,18 +74,21 @@ export function ShippingMethodForm() {
7374
});
7475

7576
const applyShippingMethod = useApplyShippingMethod();
77+
const applyDeliveryMethod = useApplyDeliveryMethod();
7678

7779
// Track the last processed state to avoid duplicate API calls
7880
const lastProcessedStateRef = useRef<{
7981
serviceCode: string | null;
8082
cost: number | null;
8183
hadShippingMethods: boolean;
8284
wasPickup: boolean;
85+
appliedDeliveryMethod: boolean;
8386
}>({
8487
serviceCode: null,
8588
cost: null,
8689
hadShippingMethods: false,
8790
wasPickup: false,
91+
appliedDeliveryMethod: false,
8892
});
8993

9094
useEffect(() => {
@@ -95,22 +99,38 @@ export function ShippingMethodForm() {
9599
const currentCost = shippingLines?.amount?.value ?? null;
96100
const lastState = lastProcessedStateRef.current;
97101

98-
// Case 1: No shipping methods available but shipping line exists - clear it
99-
// Only clear once when transitioning from having methods to no methods, or when switching to pickup
100-
if (
101-
!hasShippingMethods &&
102-
hasShippingAddress &&
103-
((currentServiceCode && lastState.hadShippingMethods) ||
104-
(isPickup && !lastState.wasPickup))
105-
) {
106-
form.setValue('shippingMethod', '', { shouldDirty: false });
107-
applyShippingMethod.mutate([]);
108-
lastProcessedStateRef.current = {
109-
serviceCode: null,
110-
cost: null,
111-
hadShippingMethods: false,
112-
wasPickup: isPickup,
113-
};
102+
// Case 1: No shipping methods available
103+
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+
}
117+
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+
) {
124+
form.setValue('shippingMethod', '', { shouldDirty: false });
125+
applyDeliveryMethod.mutate(DeliveryMethods.SHIP);
126+
lastProcessedStateRef.current = {
127+
serviceCode: null,
128+
cost: null,
129+
hadShippingMethods: false,
130+
wasPickup: false,
131+
appliedDeliveryMethod: true,
132+
};
133+
}
114134
return;
115135
}
116136

@@ -162,6 +182,7 @@ export function ShippingMethodForm() {
162182
cost: methodCost,
163183
hadShippingMethods: true,
164184
wasPickup: false,
185+
appliedDeliveryMethod: false,
165186
};
166187
}
167188
}
@@ -172,6 +193,7 @@ export function ShippingMethodForm() {
172193
isShippingMethodsLoading,
173194
form,
174195
applyShippingMethod,
196+
applyDeliveryMethod,
175197
updateTaxes.mutate,
176198
session?.enableTaxCollection,
177199
isPickup,

0 commit comments

Comments
 (0)