1- import { useReducer , type FormEventHandler } from 'react'
1+ import { useReducer , useState , type FormEventHandler } from 'react'
22import { Link } from 'react-router'
3- import { toast } from 'sonner'
3+
4+ interface Notification {
5+ type : 'error' | 'warning'
6+ text : string
7+ }
48
59export interface Discount {
610 code : string
@@ -66,6 +70,12 @@ export function DiscountCodeForm() {
6670 const [ state , dispatch ] = useReducer ( discountFormReducer , {
6771 submitting : false ,
6872 } )
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+ }
6979
7080 const handleApplyDiscount : FormEventHandler < HTMLFormElement > = async (
7181 event ,
@@ -77,13 +87,14 @@ export function DiscountCodeForm() {
7787 const code = data . get ( 'discountCode' )
7888
7989 if ( ! code ) {
80- toast . error ( 'Missing discount code' )
90+ notify ( 'Missing discount code' , 'error ')
8191 return
8292 }
8393
8494 if ( typeof code !== 'string' ) {
85- toast . error (
95+ notify (
8696 `Expected discount code to be a string but got ${ typeof code } ` ,
97+ 'error' ,
8798 )
8899 return
89100 }
@@ -93,11 +104,11 @@ export function DiscountCodeForm() {
93104 dispatch ( { type : 'success' , discount } )
94105
95106 if ( discount . isLegacy ) {
96- toast . warning ( `"${ code } " is a legacy code. Discount amount halfed.` )
107+ notify ( `"${ code } " is a legacy code. Discount amount halfed.` )
97108 }
98109 } )
99110 . catch ( ( ) => {
100- toast . error ( 'Failed to apply the discount code' )
111+ notify ( 'Failed to apply the discount code' , 'error ')
101112 dispatch ( { type : 'idle' } )
102113 } )
103114 }
@@ -113,7 +124,7 @@ export function DiscountCodeForm() {
113124 await removeDiscount ( code )
114125 . catch ( ( error ) => {
115126 console . error ( error )
116- toast . error ( 'Failed to remote the discount code' )
127+ notify ( 'Failed to remove the discount code' , 'error ')
117128 } )
118129 . finally ( ( ) => {
119130 dispatch ( { type : 'idle' } )
@@ -183,6 +194,15 @@ export function DiscountCodeForm() {
183194 </ p >
184195 </ form >
185196 ) }
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 }
186206 </ section >
187207 )
188208}
0 commit comments