import { useForm } from "@inertiajs/react"; import React, { useEffect } from "react"; import { formatDate } from "@/Utils"; export default function FormModal(props) { const { isOpen, toggle = () => {}, booking = null } = props; const { data, setData, post, put, processing, errors, clearErrors } = useForm({ booked: "", departure: "", destination: "", flight_number: "", jumlah_koli: 0, kemasan: "", master_awb: "", used: 0, is_available: 0, }); const handleOnChange = (event) => { setData(event.target.name, event.target.value); }; const handleReset = () => { setData({ booked: '', departure: '', destination: '', flight_number: '', jumlah_koli: 0, kemasan: '', master_awb: '', used: 0, is_available: 0, }) clearErrors(); }; const handleCancel = () => { handleReset(); toggle(); }; const handleSubmit = () => { if (booking !== null) { put(route("monitoring-booking.update", booking), { onSuccess: () => Promise.all([handleReset(), toggle()]), }); return; } post(route("monitoring-booking.store"), { onSuccess: () => Promise.all([handleReset(), toggle()]), }); }; useEffect(() => { setData({ booked: booking?.booked ? booking.booked : '', departure: booking?.departure ? formatDate(booking.departure).format('yyyy-MM-DD') : '', destination: booking?.destination ? booking.destination : '', flight_number: booking?.flight_number ? booking.flight_number : '', jumlah_koli: booking?.jumlah_koli ? booking.jumlah_koli : '', kemasan: booking?.kemasan ? booking.kemasan : '', master_awb: booking?.master_awb ? booking.master_awb : '', used: booking?.used ? booking.used : '', is_available: booking?.is_available ? booking.is_available : 0, }) }, [booking]); return (