import React, { useState, useEffect } from "react"; import { useParams, useNavigate } from "react-router"; import { toast } from "react-toastify"; import { getLocationById, addLocation, editLocation } from "../api"; export default function LocationFormPage() { const { id } = useParams(); const navigate = useNavigate(); const isEdit = !!id; const [formData, setFormData] = useState({ name: "", address: "", city: "", state: "IL", zipCode: "", phone: "", contact: "", contactEmail: "", status: "Active", accountId: "", }); const [loading, setLoading] = useState(false); useEffect(() => { if (isEdit && id) { const loadLocation = async () => { try { const location = await getLocationById(id); if (location) { setFormData(location); } } catch (err) { console.error("Failed to load location", err); toast.error("Failed to load location"); } }; loadLocation(); } }, [id, isEdit]); const handleChange = (e) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value, })); }; const handleSave = async (e) => { e.preventDefault(); setLoading(true); try { if (isEdit) { await editLocation(formData); toast.success("Location updated successfully!"); } else { await addLocation(formData); toast.success("Location created successfully!"); } navigate("/locations"); } catch (err) { console.error(err); toast.error("Failed to save location"); } finally { setLoading(false); } }; const handleCancel = () => { navigate("/locations"); }; return (
{isEdit ? "Edit Location" : "Create Location"}
{/* BASIC INFO SECTION */}
Location Information
{/* CONTACT INFO SECTION */}
Contact Information
{/* STATUS SECTION */}
Status
{/* ACTION BUTTONS */}
); }