11"use client"
22
33import ErrorMessage from "@/components/molecules/ErrorMessage/ErrorMessage"
4- import { setShippingMethod } from "@/lib/data/cart"
4+ import { removeShippingMethod , setShippingMethod } from "@/lib/data/cart"
55import { calculatePriceForShippingOption } from "@/lib/data/fulfillment"
66import { convertToLocale } from "@/lib/helpers/money"
77import { CheckCircleSolid , ChevronUpDown , Loader } from "@medusajs/icons"
@@ -58,6 +58,8 @@ const CartShippingMethodsSection: React.FC<ShippingProps> = ({
5858 availableShippingMethods,
5959} ) => {
6060 const [ isLoadingPrices , setIsLoadingPrices ] = useState ( false )
61+ const [ isRemovingShippingMethod , setIsRemovingShippingMethod ] =
62+ useState ( false )
6163 const [ calculatedPricesMap , setCalculatedPricesMap ] = useState <
6264 Record < string , number >
6365 > ( { } )
@@ -149,6 +151,12 @@ const CartShippingMethodsSection: React.FC<ShippingProps> = ({
149151 }
150152 }
151153
154+ const handleRemoveShippingMethod = async ( methodId : string ) => {
155+ setIsRemovingShippingMethod ( true )
156+ await removeShippingMethod ( methodId )
157+ setIsRemovingShippingMethod ( false )
158+ }
159+
152160 useEffect ( ( ) => {
153161 setError ( null )
154162 } , [ isOpen ] )
@@ -315,13 +323,14 @@ const CartShippingMethodsSection: React.FC<ShippingProps> = ({
315323 </ div >
316324 )
317325 } ) }
318- { cart && ( cart . shipping_methods ?. length ?? 0 ) > 0 && (
326+ { ! ! cart ? .shipping_methods ?. length && (
319327 < div className = "flex flex-col" >
320328 { cart . shipping_methods ?. map ( ( method ) => (
321329 < CartShippingMethodRow
322330 key = { method . id }
323331 method = { method }
324332 currency_code = { cart . currency_code }
333+ onRemoveShippingMethod = { handleRemoveShippingMethod }
325334 />
326335 ) ) }
327336 </ div >
@@ -337,7 +346,7 @@ const CartShippingMethodsSection: React.FC<ShippingProps> = ({
337346 < Button
338347 onClick = { handleSubmit }
339348 variant = "tonal"
340- disabled = { ! cart . shipping_methods ?. [ 0 ] }
349+ disabled = { ! cart . shipping_methods ?. [ 0 ] || isRemovingShippingMethod }
341350 loading = { isLoadingPrices }
342351 >
343352 Continue to payment
0 commit comments