import React, { useEffect } from 'react' import { useForm, usePage } from '@inertiajs/react' import { toast } from 'react-toastify' export default function FormModal(props) { const { modalState } = props const { data, setData, post, put, processing, errors, reset, clearErrors } = useForm({ name: '', }) const handleOnChange = (event) => { setData(event.target.name, event.target.type === 'checkbox' ? event.target.checked : event.target.value); } const handleReset = () => { reset() clearErrors() modalState.setData(null) } const handleCancel = () => { handleReset() modalState.toggle() } const handleSubmit = () => { const group = modalState.data if(group !== null) { put(route('groups.update', group), { onSuccess: () => Promise.all([ handleReset(), modalState.toggle(), toast.success('The Data has been changed'), ]), }) return } post(route('groups.store'), { onSuccess: () => Promise.all([ handleReset(), modalState.toggle(), toast.success('The Data has been saved'), ]), }) } useEffect(() => { const group = modalState.data if (group !== null) { setData({ name: group?.name, }) } }, [modalState]) return (