import React, { useEffect, useState } from 'react' import { toast } from 'react-toastify' import { Modal } from '@/Components/Modal' import { useForm } from '@inertiajs/inertia-react' import Button from '@/Components/Button' import Input from '@/Components/Input' import { create, update } from '@/Services/Karyawan' import { getAll as GetAllJabatan } from '@/Services/Jabatan' export default function FormModal({ modalState, refresh }) { const [loading, setLoading] = useState(false) const { data, setData, reset } = useForm({ nik: '', name: '', username: '', password: '', jenisKelamin: "Laki-Laki", jabatan: '', status: 'Karyawan Tetap', is_admin: false }) const [jabatans, setJabatans] = useState([]) useEffect(() => { GetAllJabatan() .then((items) => setJabatans(items)) },[]) useEffect(() => { if (modalState.isOpen === false) { reset() modalState.setData(null) } if (modalState.data !== null) { setData(modalState.data.data) } }, [modalState]) const onHandleChange = (event) => { setData( event.target.name, event.target.type === 'checkbox' ? event.target.checked : event.target.value ) } const onHandleJabatanChange = (event) => { if (event.target.value == "") { setData({ ...data, jabatan: "" }) return } const jabatan = jabatans.find(item => item.id == event.target.value) setData({ ...data, jabatan: {...jabatan.data, id : jabatan.id} }) } const submit = (e) => { e.preventDefault() setLoading(true) if (modalState.data !== null) { update(data, modalState.data.id) .finally(() => { reset() toast.success("berhasil update") setLoading(false) modalState.toggle() refresh() }) } else { create(data) .then((id) => console.log(id)) .finally(() => { reset() toast.success("berhasil simpan") setLoading(false) modalState.toggle() refresh() }) } } return (
Karyawan
) }