feat: integrate toast notifications for success and error handling
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { zodResolver } from '@hookform/resolvers/zod'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useEffect } from 'react'
|
||||
import toast from 'react-hot-toast'
|
||||
import { useFetcher, useNavigate } from 'react-router'
|
||||
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
|
||||
import { z } from 'zod'
|
||||
@@ -38,20 +39,25 @@ export const FormCategoryPage = (properties: TProperties) => {
|
||||
description: categoryData?.description || '',
|
||||
},
|
||||
})
|
||||
const [error, setError] = useState<string>()
|
||||
|
||||
const { handleSubmit, watch, setValue } = formMethods
|
||||
const watchName = watch('name')
|
||||
|
||||
useEffect(() => {
|
||||
if (!fetcher.data?.success) {
|
||||
setError(fetcher.data?.message)
|
||||
if (fetcher.data?.success === false) {
|
||||
toast.error(fetcher.data?.message)
|
||||
return
|
||||
}
|
||||
|
||||
if (fetcher.data?.success === true) {
|
||||
toast.success(
|
||||
`Kategori berhasil ${categoryData ? 'diupdate' : 'dibuat'}!`,
|
||||
)
|
||||
navigate('/lg-admin/categories')
|
||||
return
|
||||
}
|
||||
navigate('/lg-admin/categories')
|
||||
setError(undefined)
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [fetcher])
|
||||
}, [fetcher.data])
|
||||
|
||||
useEffect(() => {
|
||||
setValue('code', urlFriendlyCode(watchName))
|
||||
@@ -69,9 +75,6 @@ export const FormCategoryPage = (properties: TProperties) => {
|
||||
action={`/actions/admin/categories/${categoryData ? 'update' : 'create'}`}
|
||||
className="space-y-4"
|
||||
>
|
||||
{error && (
|
||||
<div className="text-sm text-red-500 capitalize">{error}</div>
|
||||
)}
|
||||
<div className="flex items-end justify-between gap-4">
|
||||
<Input
|
||||
id="name"
|
||||
|
||||
Reference in New Issue
Block a user