import { Card, CardContent, CardHeader, CardTitle } from '@components/common/ui/Card.js'; import { Item, ItemActions, ItemContent, ItemDescription, ItemTitle } from '@components/common/ui/Item.js'; import { RadioGroup, RadioGroupItem } from '@components/common/ui/RadioGroup.js'; import { useCheckout } from '@components/frontStore/checkout/CheckoutContext.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { AddressGraphql } from '@evershop/evershop/types/address'; import { Package } from 'lucide-react'; import React from 'react'; interface ShippingMethod { code: string; /** * Provider code (e.g., 'core', 'usps'). Threaded through to * `addShippingMethod` so the server can call the right provider's * validateMethod. Optional at the type level for back-compat with cached * availableShippingMethods data that pre-dates phase 4. */ providerCode?: string; name: string; /** Carrier display name (e.g., 'USPS'). Themes can render it next to name. */ carrier?: string; cost?: { value: number; text: string; }; description?: string; isSelected?: boolean; } // Skeleton component for loading state function ShippingMethodSkeleton() { return (
{[1, 2, 3, 4].map((index) => (
))}
); } export function ShippingMethods({ methods, shippingAddress, addressReady, isLoading, onSelect }: { methods: ShippingMethod[]; shippingAddress?: AddressGraphql; /** * True once the country and every REQUIRED geographic field of the * country's schema are filled (spec § 9 AC1: Hong Kong has no postal code, * Germany no administrative area). Decides which empty state to show. */ addressReady?: boolean; isLoading?: boolean; onSelect?: (method: ShippingMethod) => Promise | boolean; }) { const ready = addressReady ?? Boolean(shippingAddress?.country); const { form } = useCheckout(); const { formState, setValue, watch } = form; const [isProcessing, setIsProcessing] = React.useState(false); const currentValue = watch('shippingMethod'); const handleMethodSelect = async (method: ShippingMethod) => { if (!onSelect) { // If no onSelect function provided, allow normal behavior setValue('shippingMethod', method.code); return; } if (isProcessing || formState.disabled) { return; } try { setIsProcessing(true); const result = await Promise.resolve(onSelect(method)); if (result) { // Only update the form value if onSelect returns true setValue('shippingMethod', method.code); } // If result is false, keep the current selection } catch (error) { // Keep the current selection on error } finally { setIsProcessing(false); } }; return (
{_('Shipping Method')}
{isLoading ? ( ) : ( <>
{methods?.length === 0 ? (
{!ready ? (
{_( 'Available shipping methods will appear once you provide your address details' )}
) : (
{_('No shipping methods available')}
{_( 'No shipping options are available for your location' )}
)}
) : ( { const method = methods.find((m) => m.code === value); if (method) { handleMethodSelect(method); } else { setValue('shippingMethod', value); } }} > {methods.map((method: ShippingMethod) => ( // The whole card selects the method, not just the radio // dot. Base UI radios react only to pointer events on // their own [role="radio"] element — a wrapping )}
{formState.errors.shippingMethod && (
{formState.errors.shippingMethod?.message?.toString() || _('Please select a shipping method')}
)} )}
); }