Skip to content

Commit 95916f3

Browse files
committed
03/05: remove sonner imports
1 parent 5883dc9 commit 95916f3

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

exercises/03.best-practices/05.problem.page-navigation/src/discount-code-form.tsx

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import { useReducer, type FormEventHandler } from 'react'
1+
import { useReducer, useState, type FormEventHandler } from 'react'
22
import { Link } from 'react-router'
3-
import { toast } from 'sonner'
3+
4+
interface Notification {
5+
type: 'error' | 'warning'
6+
text: string
7+
}
48

59
export 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
}

exercises/03.best-practices/05.problem.page-navigation/src/main.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { StrictMode } from 'react'
22
import { createRoot } from 'react-dom/client'
3-
import { Toaster } from 'sonner'
43
import './index.css'
54
import { App } from './app.jsx'
65

@@ -14,7 +13,6 @@ async function enableMocking() {
1413
enableMocking().then(() => {
1514
createRoot(document.getElementById('root')!).render(
1615
<StrictMode>
17-
<Toaster richColors />
1816
<App />
1917
</StrictMode>,
2018
)

0 commit comments

Comments
 (0)