Skip to content

Commit 6e66ba0

Browse files
authored
Merge pull request #506 from medusajs/fix/show-error-message-on-invalid-promo
fix(checkout): handle error on invalid promo code
2 parents 21e61d2 + f0efc43 commit 6e66ba0

File tree

1 file changed

+13
-9
lines changed
  • src/modules/checkout/components/discount-code

1 file changed

+13
-9
lines changed

src/modules/checkout/components/discount-code/index.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
"use client"
22

3-
import { Badge, Heading, Input, Label, Text, Tooltip } from "@medusajs/ui"
4-
import React, { useActionState } from "react";
3+
import { Badge, Heading, Input, Label, Text } from "@medusajs/ui"
4+
import React from "react";
55

6-
import { applyPromotions, submitPromotionForm } from "@lib/data/cart"
6+
import { applyPromotions } from "@lib/data/cart"
77
import { convertToLocale } from "@lib/util/money"
8-
import { InformationCircleSolid } from "@medusajs/icons"
98
import { HttpTypes } from "@medusajs/types"
109
import Trash from "@modules/common/icons/trash"
1110
import ErrorMessage from "../error-message"
@@ -19,8 +18,9 @@ type DiscountCodeProps = {
1918

2019
const DiscountCode: React.FC<DiscountCodeProps> = ({ cart }) => {
2120
const [isOpen, setIsOpen] = React.useState(false)
21+
const [errorMessage, setErrorMessage] = React.useState("")
2222

23-
const { items = [], promotions = [] } = cart
23+
const { promotions = [] } = cart
2424
const removePromotionCode = async (code: string) => {
2525
const validPromotions = promotions.filter(
2626
(promotion) => promotion.code !== code
@@ -32,6 +32,8 @@ const DiscountCode: React.FC<DiscountCodeProps> = ({ cart }) => {
3232
}
3333

3434
const addPromotionCode = async (formData: FormData) => {
35+
setErrorMessage("")
36+
3537
const code = formData.get("code")
3638
if (!code) {
3739
return
@@ -42,15 +44,17 @@ const DiscountCode: React.FC<DiscountCodeProps> = ({ cart }) => {
4244
.map((p) => p.code!)
4345
codes.push(code.toString())
4446

45-
await applyPromotions(codes)
47+
try {
48+
await applyPromotions(codes)
49+
} catch (e: any) {
50+
setErrorMessage(e.message)
51+
}
4652

4753
if (input) {
4854
input.value = ""
4955
}
5056
}
5157

52-
const [message, formAction] = useActionState(submitPromotionForm, null)
53-
5458
return (
5559
<div className="w-full bg-white flex flex-col">
5660
<div className="txt-medium">
@@ -90,7 +94,7 @@ const DiscountCode: React.FC<DiscountCodeProps> = ({ cart }) => {
9094
</div>
9195

9296
<ErrorMessage
93-
error={message}
97+
error={errorMessage}
9498
data-testid="discount-error-message"
9599
/>
96100
</>

0 commit comments

Comments
 (0)