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 { props: { groups } } = usePage() const { data, setData, post, put, processing, errors, reset, clearErrors } = useForm({ name: '', group_id: '' }) 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 region = modalState.data if(region !== null) { put(route('regions.update', region), { onSuccess: () => Promise.all([ handleReset(), modalState.toggle(), toast.success('The Data has been changed'), ]), }) return } post(route('regions.store'), { onSuccess: () => Promise.all([ handleReset(), modalState.toggle(), toast.success('The Data has been saved'), ]), }) } useEffect(() => { const region = modalState.data if (region !== null) { setData({ name: region?.name, group_id: region?.group_id }) } }, [modalState]) return (

Region

Simpan
Batal
) }