1
- import { useReducer , type FormEventHandler } from 'react'
1
+ import { useReducer , useState , type FormEventHandler } from 'react'
2
2
import { Link } from 'react-router'
3
- import { toast } from 'sonner'
3
+
4
+ interface Notification {
5
+ type : 'error' | 'warning'
6
+ text : string
7
+ }
4
8
5
9
export interface Discount {
6
10
code : string
@@ -66,6 +70,12 @@ export function DiscountCodeForm() {
66
70
const [ state , dispatch ] = useReducer ( discountFormReducer , {
67
71
submitting : false ,
68
72
} )
73
+ const [ notification , setNotification ] = useState < Notification > ( )
74
+
75
+ const notify = ( text : string , type : Notification [ 'type' ] = 'warning' ) => {
76
+ setNotification ( { type, text } )
77
+ setTimeout ( ( ) => setNotification ( undefined ) , 5000 )
78
+ }
69
79
70
80
const handleApplyDiscount : FormEventHandler < HTMLFormElement > = async (
71
81
event ,
@@ -77,13 +87,14 @@ export function DiscountCodeForm() {
77
87
const code = data . get ( 'discountCode' )
78
88
79
89
if ( ! code ) {
80
- toast . error ( 'Missing discount code' )
90
+ notify ( 'Missing discount code' , 'error ')
81
91
return
82
92
}
83
93
84
94
if ( typeof code !== 'string' ) {
85
- toast . error (
95
+ notify (
86
96
`Expected discount code to be a string but got ${ typeof code } ` ,
97
+ 'error' ,
87
98
)
88
99
return
89
100
}
@@ -93,11 +104,11 @@ export function DiscountCodeForm() {
93
104
dispatch ( { type : 'success' , discount } )
94
105
95
106
if ( discount . isLegacy ) {
96
- toast . warning ( `"${ code } " is a legacy code. Discount amount halfed.` )
107
+ notify ( `"${ code } " is a legacy code. Discount amount halfed.` )
97
108
}
98
109
} )
99
110
. catch ( ( ) => {
100
- toast . error ( 'Failed to apply the discount code' )
111
+ notify ( 'Failed to apply the discount code' , 'error ')
101
112
dispatch ( { type : 'idle' } )
102
113
} )
103
114
}
@@ -113,7 +124,7 @@ export function DiscountCodeForm() {
113
124
await removeDiscount ( code )
114
125
. catch ( ( error ) => {
115
126
console . error ( error )
116
- toast . error ( 'Failed to remote the discount code' )
127
+ notify ( 'Failed to remove the discount code' , 'error ')
117
128
} )
118
129
. finally ( ( ) => {
119
130
dispatch ( { type : 'idle' } )
@@ -183,6 +194,15 @@ export function DiscountCodeForm() {
183
194
</ p >
184
195
</ form >
185
196
) }
197
+
198
+ { notification ? (
199
+ < p
200
+ role = "alert"
201
+ className = { `animation-slide animate-slide-in fixed bottom-5 right-5 rounded-lg border px-5 py-2.5 font-medium ${ notification . type === 'error' ? 'border-red-800/20 bg-red-200' : 'border-yellow-800/20 bg-yellow-200' } ` }
202
+ >
203
+ { notification . text }
204
+ </ p >
205
+ ) : null }
186
206
</ section >
187
207
)
188
208
}
0 commit comments