77import { Localized } from '@fluent/react' ;
88import * as Form from '@radix-ui/react-form' ;
99import classNames from 'classnames' ;
10- import { useSearchParams } from 'next/navigation' ;
10+ import { usePathname , useRouter , useSearchParams } from 'next/navigation' ;
1111import { forwardRef , useEffect , useState } from 'react' ;
1212import { useFormState , useFormStatus } from 'react-dom' ;
1313import { ButtonVariant } from '../BaseButton' ;
@@ -30,8 +30,21 @@ const WithCoupon = ({
3030 couponCode,
3131 readOnly,
3232} : WithCouponProps ) => {
33+ const pathname = usePathname ( ) ;
34+ const router = useRouter ( ) ;
35+ const searchParams = useSearchParams ( ) ;
36+ const promoCode = searchParams . get ( 'coupon' ) ;
37+
3338 async function removeCoupon ( ) {
3439 await applyCouponAction ( cartId , cartVersion , '' ) ;
40+
41+ if ( promoCode ) {
42+ const params = new URLSearchParams ( searchParams . toString ( ) ) ;
43+ params . delete ( 'coupon' ) ;
44+ const newQuery = params . toString ( ) ;
45+ const newUrl = newQuery ? `${ pathname } ?${ newQuery } ` : pathname ;
46+ router . push ( newUrl ) ;
47+ }
3548 }
3649
3750 return (
@@ -42,9 +55,7 @@ const WithCoupon = ({
4255 >
4356 < Form . Field name = "appliedCouponCode" >
4457 < Localized id = "next-coupon-promo-code-applied" >
45- < Form . Label className = "font-semibold text-grey-600" >
46- < h2 > Promo Code Applied</ h2 >
47- </ Form . Label >
58+ < h2 className = "font-semibold text-grey-600" > Promo Code Applied</ h2 >
4859 </ Localized >
4960 < div className = "mt-4 flex gap-4 justify-between items-center" >
5061 < span className = "break-all" > { couponCode } </ span >
@@ -92,6 +103,7 @@ const CouponInput = forwardRef(
92103 }
93104 ) }
94105 type = "text"
106+ id = "coupon"
95107 name = "coupon"
96108 data-testid = "coupon-input"
97109 placeholder = "Enter code"
@@ -146,7 +158,7 @@ const WithoutCoupon = ({
146158 >
147159 < Form . Field name = "couponCode" >
148160 < Localized id = "next-coupon-promo-code" >
149- < Form . Label className = "font-semibold text-grey-600" >
161+ < Form . Label htmlFor = "coupon" className = "font-semibold text-grey-600" >
150162 < h2 > Promo Code</ h2 >
151163 </ Form . Label >
152164 </ Localized >
0 commit comments