1
1
"use client"
2
2
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" ;
5
5
6
- import { applyPromotions , submitPromotionForm } from "@lib/data/cart"
6
+ import { applyPromotions } from "@lib/data/cart"
7
7
import { convertToLocale } from "@lib/util/money"
8
- import { InformationCircleSolid } from "@medusajs/icons"
9
8
import { HttpTypes } from "@medusajs/types"
10
9
import Trash from "@modules/common/icons/trash"
11
10
import ErrorMessage from "../error-message"
@@ -19,8 +18,9 @@ type DiscountCodeProps = {
19
18
20
19
const DiscountCode : React . FC < DiscountCodeProps > = ( { cart } ) => {
21
20
const [ isOpen , setIsOpen ] = React . useState ( false )
21
+ const [ errorMessage , setErrorMessage ] = React . useState ( "" )
22
22
23
- const { items = [ ] , promotions = [ ] } = cart
23
+ const { promotions = [ ] } = cart
24
24
const removePromotionCode = async ( code : string ) => {
25
25
const validPromotions = promotions . filter (
26
26
( promotion ) => promotion . code !== code
@@ -32,6 +32,8 @@ const DiscountCode: React.FC<DiscountCodeProps> = ({ cart }) => {
32
32
}
33
33
34
34
const addPromotionCode = async ( formData : FormData ) => {
35
+ setErrorMessage ( "" )
36
+
35
37
const code = formData . get ( "code" )
36
38
if ( ! code ) {
37
39
return
@@ -42,15 +44,17 @@ const DiscountCode: React.FC<DiscountCodeProps> = ({ cart }) => {
42
44
. map ( ( p ) => p . code ! )
43
45
codes . push ( code . toString ( ) )
44
46
45
- await applyPromotions ( codes )
47
+ try {
48
+ await applyPromotions ( codes )
49
+ } catch ( e : any ) {
50
+ setErrorMessage ( e . message )
51
+ }
46
52
47
53
if ( input ) {
48
54
input . value = ""
49
55
}
50
56
}
51
57
52
- const [ message , formAction ] = useActionState ( submitPromotionForm , null )
53
-
54
58
return (
55
59
< div className = "w-full bg-white flex flex-col" >
56
60
< div className = "txt-medium" >
@@ -90,7 +94,7 @@ const DiscountCode: React.FC<DiscountCodeProps> = ({ cart }) => {
90
94
</ div >
91
95
92
96
< ErrorMessage
93
- error = { message }
97
+ error = { errorMessage }
94
98
data-testid = "discount-error-message"
95
99
/>
96
100
</ >
0 commit comments