feat: update success and error handling in form pages with toast notifications

This commit is contained in:
Ardeman 2025-03-12 20:20:30 +08:00
parent 0545190497
commit c0318fac4c
2 changed files with 12 additions and 13 deletions

View File

@ -97,12 +97,12 @@ export const FormContentsPage = (properties: TProperties) => {
const watchTags = watch('tags')
useEffect(() => {
if (fetcher.data?.sucess) {
if (fetcher.data?.success === false) {
toast.error(fetcher.data?.message)
return
}
if (fetcher.data?.success) {
if (fetcher.data?.success === true) {
toast.success(`Artikel berhasil ${newsData ? 'diupdate' : 'dibuat'}!`)
navigate('/lg-admin/contents')
return

View File

@ -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'
@ -34,20 +35,21 @@ export const FormTagPage = (properties: TProperties) => {
name: tagData?.name || '',
},
})
const [error, setError] = useState<string>()
const { handleSubmit, watch, setValue } = formMethods
const watchName = watch('name')
useEffect(() => {
if (!fetcher.data?.success) {
setError(fetcher.data?.message)
return
if (fetcher.data?.success === false) {
toast.error(fetcher.data?.message)
}
if (fetcher.data?.success === true) {
toast.success(`Tag berhasil ${tagData ? 'diupdate' : 'dibuat'}!`)
navigate('/lg-admin/tags')
setError(undefined)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetcher])
}, [fetcher.data])
useEffect(() => {
setValue('code', urlFriendlyCode(watchName))
@ -65,9 +67,6 @@ export const FormTagPage = (properties: TProperties) => {
action={`/actions/admin/tags/${tagData ? '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"