feat: update success and error handling in form pages with toast notifications
This commit is contained in:
parent
0545190497
commit
c0318fac4c
@ -97,12 +97,12 @@ export const FormContentsPage = (properties: TProperties) => {
|
|||||||
const watchTags = watch('tags')
|
const watchTags = watch('tags')
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (fetcher.data?.sucess) {
|
if (fetcher.data?.success === false) {
|
||||||
toast.error(fetcher.data?.message)
|
toast.error(fetcher.data?.message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fetcher.data?.success) {
|
if (fetcher.data?.success === true) {
|
||||||
toast.success(`Artikel berhasil ${newsData ? 'diupdate' : 'dibuat'}!`)
|
toast.success(`Artikel berhasil ${newsData ? 'diupdate' : 'dibuat'}!`)
|
||||||
navigate('/lg-admin/contents')
|
navigate('/lg-admin/contents')
|
||||||
return
|
return
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import { zodResolver } from '@hookform/resolvers/zod'
|
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 { useFetcher, useNavigate } from 'react-router'
|
||||||
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
|
import { RemixFormProvider, useRemixForm } from 'remix-hook-form'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
@ -34,20 +35,21 @@ export const FormTagPage = (properties: TProperties) => {
|
|||||||
name: tagData?.name || '',
|
name: tagData?.name || '',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
const [error, setError] = useState<string>()
|
|
||||||
|
|
||||||
const { handleSubmit, watch, setValue } = formMethods
|
const { handleSubmit, watch, setValue } = formMethods
|
||||||
const watchName = watch('name')
|
const watchName = watch('name')
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!fetcher.data?.success) {
|
if (fetcher.data?.success === false) {
|
||||||
setError(fetcher.data?.message)
|
toast.error(fetcher.data?.message)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (fetcher.data?.success === true) {
|
||||||
|
toast.success(`Tag berhasil ${tagData ? 'diupdate' : 'dibuat'}!`)
|
||||||
navigate('/lg-admin/tags')
|
navigate('/lg-admin/tags')
|
||||||
setError(undefined)
|
}
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [fetcher])
|
}, [fetcher.data])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setValue('code', urlFriendlyCode(watchName))
|
setValue('code', urlFriendlyCode(watchName))
|
||||||
@ -65,9 +67,6 @@ export const FormTagPage = (properties: TProperties) => {
|
|||||||
action={`/actions/admin/tags/${tagData ? 'update' : 'create'}`}
|
action={`/actions/admin/tags/${tagData ? 'update' : 'create'}`}
|
||||||
className="space-y-4"
|
className="space-y-4"
|
||||||
>
|
>
|
||||||
{error && (
|
|
||||||
<div className="text-sm text-red-500 capitalize">{error}</div>
|
|
||||||
)}
|
|
||||||
<div className="flex items-end justify-between gap-4">
|
<div className="flex items-end justify-between gap-4">
|
||||||
<Input
|
<Input
|
||||||
id="name"
|
id="name"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user