Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cute-ravens-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@godaddy/react": patch
---

Apply shipping intent when no shipping methods returned
14 changes: 4 additions & 10 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -24,12 +22,8 @@
},
"typesVersions": {
"*": {
"server": [
"./dist/server.d.ts"
],
"*": [
"./dist/index.d.ts"
]
"server": ["./dist/server.d.ts"],
"*": ["./dist/index.d.ts"]
}
},
"scripts": {
Expand All @@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
Expand All @@ -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],
});
},
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -73,18 +74,21 @@ export function ShippingMethodForm() {
});

const applyShippingMethod = useApplyShippingMethod();
const applyDeliveryMethod = useApplyDeliveryMethod();

// Track the last processed state to avoid duplicate API calls
const lastProcessedStateRef = useRef<{
serviceCode: string | null;
cost: number | null;
hadShippingMethods: boolean;
wasPickup: boolean;
appliedDeliveryMethod: boolean;
}>({
serviceCode: null,
cost: null,
hadShippingMethods: false,
wasPickup: false,
appliedDeliveryMethod: false,
});

useEffect(() => {
Expand All @@ -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;
}

Expand Down Expand Up @@ -162,6 +182,7 @@ export function ShippingMethodForm() {
cost: methodCost,
hadShippingMethods: true,
wasPickup: false,
appliedDeliveryMethod: false,
};
}
}
Expand All @@ -172,6 +193,7 @@ export function ShippingMethodForm() {
isShippingMethodsLoading,
form,
applyShippingMethod,
applyDeliveryMethod,
updateTaxes.mutate,
session?.enableTaxCollection,
isPickup,
Expand Down