Skip to content

Commit 08c1d0f

Browse files
committed
fix(payments-ui): Remove coupon from URL params
Because - coupon remained on the cart despite being removed This pull request - removes coupon from query params when removing coupon - fixes console warnings in Chrome Closes: FXA-11718
1 parent 985f4c5 commit 08c1d0f

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

  • libs/payments/ui/src/lib/client/components/CouponForm

libs/payments/ui/src/lib/client/components/CouponForm/index.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { Localized } from '@fluent/react';
88
import * as Form from '@radix-ui/react-form';
99
import classNames from 'classnames';
10-
import { useSearchParams } from 'next/navigation';
10+
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
1111
import { forwardRef, useEffect, useState } from 'react';
1212
import { useFormState, useFormStatus } from 'react-dom';
1313
import { 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

Comments
 (0)