From 6e509c32d12742d20306519c8540405f82418f40 Mon Sep 17 00:00:00 2001 From: Phil Bennett Date: Mon, 5 Jan 2026 15:20:53 -0600 Subject: [PATCH 1/2] apply delivery method for shipping intent if no methods found --- packages/react/package.json | 14 ++--- .../utils/use-apply-delivery-method.ts | 9 +++- .../checkout/shipping/shipping-method.tsx | 54 +++++++++++++------ 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/packages/react/package.json b/packages/react/package.json index be740631..80ea66ff 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -4,9 +4,7 @@ "version": "1.0.15", "type": "module", "types": "./dist/index.d.ts", - "files": [ - "dist" - ], + "files": ["dist"], "exports": { "./package.json": "./package.json", "./styles.css": "./dist/index.css", @@ -24,12 +22,8 @@ }, "typesVersions": { "*": { - "server": [ - "./dist/server.d.ts" - ], - "*": [ - "./dist/index.d.ts" - ] + "server": ["./dist/server.d.ts"], + "*": ["./dist/index.d.ts"] } }, "scripts": { @@ -39,7 +33,7 @@ "preview": "vite preview", "typecheck": "tsc --noEmit", "test": "vitest run", - "lint": "biome check .", + "lint": "biome check ./src", "lint:fix": "biome check --write --unsafe ./src", "prepublishOnly": "pnpm run build" }, diff --git a/packages/react/src/components/checkout/delivery/utils/use-apply-delivery-method.ts b/packages/react/src/components/checkout/delivery/utils/use-apply-delivery-method.ts index 01a6879f..669463ae 100644 --- a/packages/react/src/components/checkout/delivery/utils/use-apply-delivery-method.ts +++ b/packages/react/src/components/checkout/delivery/utils/use-apply-delivery-method.ts @@ -1,4 +1,4 @@ -import { useMutation } from '@tanstack/react-query'; +import { useMutation, useQueryClient } from '@tanstack/react-query'; import { useCheckoutContext } from '@/components/checkout/checkout'; import { useGoDaddyContext } from '@/godaddy-provider'; import { applyDeliveryMethod } from '@/lib/godaddy/godaddy'; @@ -7,6 +7,7 @@ import type { ApplyCheckoutSessionDeliveryMethodInput } from '@/types'; export function useApplyDeliveryMethod() { const { session, jwt } = useCheckoutContext(); const { apiHost } = useGoDaddyContext(); + const queryClient = useQueryClient(); return useMutation({ mutationKey: session?.id @@ -21,5 +22,11 @@ export function useApplyDeliveryMethod() { : await applyDeliveryMethod({ mode }, session, apiHost); return data; }, + onSuccess: () => { + if (!session) return; + queryClient.invalidateQueries({ + queryKey: ['draft-order', session.id], + }); + }, }); } diff --git a/packages/react/src/components/checkout/shipping/shipping-method.tsx b/packages/react/src/components/checkout/shipping/shipping-method.tsx index 8d17fcb0..9d68c82a 100644 --- a/packages/react/src/components/checkout/shipping/shipping-method.tsx +++ b/packages/react/src/components/checkout/shipping/shipping-method.tsx @@ -2,6 +2,7 @@ import { useEffect, useRef } from 'react'; import { useFormContext } from 'react-hook-form'; import { useCheckoutContext } from '@/components/checkout/checkout'; import { DeliveryMethods } from '@/components/checkout/delivery/delivery-method'; +import { useApplyDeliveryMethod } from '@/components/checkout/delivery/utils/use-apply-delivery-method'; import { useDraftOrder, useDraftOrderShipping, @@ -73,6 +74,7 @@ export function ShippingMethodForm() { }); const applyShippingMethod = useApplyShippingMethod(); + const applyDeliveryMethod = useApplyDeliveryMethod(); // Track the last processed state to avoid duplicate API calls const lastProcessedStateRef = useRef<{ @@ -80,11 +82,13 @@ export function ShippingMethodForm() { cost: number | null; hadShippingMethods: boolean; wasPickup: boolean; + appliedDeliveryMethod: boolean; }>({ serviceCode: null, cost: null, hadShippingMethods: false, wasPickup: false, + appliedDeliveryMethod: false, }); useEffect(() => { @@ -95,22 +99,38 @@ export function ShippingMethodForm() { const currentCost = shippingLines?.amount?.value ?? null; const lastState = lastProcessedStateRef.current; - // Case 1: No shipping methods available but shipping line exists - clear it - // Only clear once when transitioning from having methods to no methods, or when switching to pickup - if ( - !hasShippingMethods && - hasShippingAddress && - ((currentServiceCode && lastState.hadShippingMethods) || - (isPickup && !lastState.wasPickup)) - ) { - form.setValue('shippingMethod', '', { shouldDirty: false }); - applyShippingMethod.mutate([]); - lastProcessedStateRef.current = { - serviceCode: null, - cost: null, - hadShippingMethods: false, - wasPickup: isPickup, - }; + // Case 1: No shipping methods available + if (!hasShippingMethods && hasShippingAddress) { + // If pickup mode, clear the shipping method + if (isPickup && (currentServiceCode || !lastState.wasPickup)) { + form.setValue('shippingMethod', '', { shouldDirty: false }); + applyShippingMethod.mutate([]); + lastProcessedStateRef.current = { + serviceCode: null, + cost: null, + hadShippingMethods: false, + wasPickup: true, + appliedDeliveryMethod: false, + }; + return; + } + + // If shipping mode with no methods, apply SHIP delivery method + // Apply if: transitioning from having methods OR haven't applied it yet + if ( + !isPickup && + (lastState.hadShippingMethods || !lastState.appliedDeliveryMethod) + ) { + form.setValue('shippingMethod', '', { shouldDirty: false }); + applyDeliveryMethod.mutate(DeliveryMethods.SHIP); + lastProcessedStateRef.current = { + serviceCode: null, + cost: null, + hadShippingMethods: false, + wasPickup: false, + appliedDeliveryMethod: true, + }; + } return; } @@ -162,6 +182,7 @@ export function ShippingMethodForm() { cost: methodCost, hadShippingMethods: true, wasPickup: false, + appliedDeliveryMethod: false, }; } } @@ -172,6 +193,7 @@ export function ShippingMethodForm() { isShippingMethodsLoading, form, applyShippingMethod, + applyDeliveryMethod, updateTaxes.mutate, session?.enableTaxCollection, isPickup, From f5516d773eeec9c72f9e179a19d3c8e6b7f70bf8 Mon Sep 17 00:00:00 2001 From: Phil Bennett Date: Mon, 5 Jan 2026 15:21:39 -0600 Subject: [PATCH 2/2] add changeset --- .changeset/cute-ravens-kneel.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/cute-ravens-kneel.md diff --git a/.changeset/cute-ravens-kneel.md b/.changeset/cute-ravens-kneel.md new file mode 100644 index 00000000..c69dbb64 --- /dev/null +++ b/.changeset/cute-ravens-kneel.md @@ -0,0 +1,5 @@ +--- +"@godaddy/react": patch +--- + +Apply shipping intent when no shipping methods returned