import React, { useEffect } from 'react' import { useForm, usePage } from '@inertiajs/react' import { toast } from 'react-toastify' export default function UserFormModal(props) { const { isOpen, toggle = () => {} , user = null } = props const { props: { roles, regions }} = usePage() const { data, setData, post, put, processing, errors, reset, clearErrors } = useForm({ name: '', email: '', password: '', role_id: '', is_admin: '0', region_id: '' }) const handleOnChange = (event) => { setData(event.target.name, event.target.value) } const handleReset = () => { reset() clearErrors() } const handleCancel = () => { handleReset() toggle() } const handleSubmit = () => { if(user !== null) { put(route('users.update', user), { onSuccess: () => Promise.all([ handleReset(), toggle(), toast.success('The Data has been changed'), ]), }) return } post(route('users.store'), { onSuccess: () => Promise.all([ handleReset(), toggle(), toast.success('The Data has been saved'), ]), }) } useEffect(() => { setData({ name: user?.name, email: user?.email, role_id: user?.role_id, is_admin: user?.is_admin, region_id: user?.region_id }) }, [user]) return (