import React, { useState, useEffect } from "react"; import { useParams, useNavigate } from "react-router"; import { toast } from "react-toastify"; import { getFollowUpById, addFollowUp, editFollowUp } from "../api"; export default function FollowUpFormPage() { const { id } = useParams(); const navigate = useNavigate(); const isEdit = !!id; const [formData, setFormData] = useState({ workOrder: "", reason: "", scheduledDate: "", status: "Pending", createdBy: "", createdDate: new Date().toISOString().split("T")[0], notes: "", }); const [loading, setLoading] = useState(false); useEffect(() => { if (isEdit && id) { const loadFollowUp = async () => { try { const followUp = await getFollowUpById(id); if (followUp) { setFormData(followUp); } } catch (err) { console.error("Failed to load follow up", err); toast.error("Failed to load follow up"); } }; loadFollowUp(); } }, [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 editFollowUp(formData); toast.success("Follow-up updated successfully!"); } else { await addFollowUp(formData); toast.success("Follow-up created successfully!"); } navigate("/followups"); } catch (err) { console.error(err); toast.error("Failed to save follow-up"); } finally { setLoading(false); } }; const handleCancel = () => { navigate("/followups"); }; return (
{isEdit ? "Edit Follow-up" : "Create Follow-up"}
{/* BASIC INFO SECTION */}
Follow-up Details
{/* CREATOR INFO SECTION */}
Creator Information
{/* NOTES SECTION */}
Notes