import { useEffect, useState } from "react"; import PropTypes from "prop-types"; import { updateBranch } from "../api/brandApi"; import { Map as MapIcon, X } from "lucide-react"; import GeofenceMap from "./GeofenceMap"; export default function EditBranchModal({ isOpen, branch, onClose, onUpdated }) { const [form, setForm] = useState({ branch_name: "", address: "", phone: "", description: "", geofence_location: "", geofence_polygon: null, latitude: null, longitude: null, }); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(""); const [showMap, setShowMap] = useState(false); useEffect(() => { if (!isOpen || !branch) return; setForm({ branch_name: branch.name || branch.branch_name || "", address: branch.address || "", phone: branch.phone || "", description: branch.description || "", geofence_location: branch.geofence_location || "", geofence_polygon: branch.geofence_polygon || null, latitude: branch.latitude ?? null, longitude: branch.longitude ?? null, }); setError(""); setShowMap(false); }, [isOpen, branch]); const updateField = (field, value) => { setForm((prev) => ({ ...prev, [field]: value, })); }; const handleSubmit = async (event) => { event.preventDefault(); const branchName = form.branch_name.trim(); if (!branchName) { setError("Branch name is required."); return; } setSubmitting(true); setError(""); try { const result = await updateBranch({ branch_id: branch.branch_id, branch_name: branchName, address: form.address.trim(), phone: form.phone.trim(), description: form.description.trim(), geofence_location: form.geofence_location.trim(), geofence_polygon: form.geofence_polygon, latitude: form.latitude, longitude: form.longitude, }); onUpdated?.(result); } catch (err) { setError(err?.message || "Failed to update branch."); } finally { setSubmitting(false); } }; if (!isOpen || !branch) return null; return (

Edit Branch

Update branch details

{error ? (
{error}
) : null}
updateField("branch_name", e.target.value)} placeholder="e.g. Main Street Branch" className="w-full px-3 py-2 text-sm transition-colors bg-white border rounded-lg border-slate-200 focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500" />
updateField("address", e.target.value)} placeholder="e.g. 123 Main St, City" className="w-full px-3 py-2 text-sm transition-colors bg-white border rounded-lg border-slate-200 focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500" />
updateField("phone", e.target.value)} placeholder="e.g. 555-0123" className="w-full px-3 py-2 text-sm transition-colors bg-white border rounded-lg border-slate-200 focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500" />