"use client"; import { useState, useCallback } from "react"; import { createLocation } from "@/actions/locations"; import GlassModal from "@/components/admin/GlassModal"; const inputStyle = { background: "rgba(0, 0, 0, 0.02)", border: "1px solid rgba(0, 0, 0, 0.06)", outline: "none", }; function handleFocus(e: React.FocusEvent) { e.target.style.background = "rgba(16, 185, 129, 0.04)"; e.target.style.border = "1px solid rgba(16, 185, 129, 0.5)"; e.target.style.boxShadow = "0 0 0 4px rgba(16, 185, 129, 0.08), 0 2px 8px rgba(0, 0, 0, 0.04)"; } function handleBlur(e: React.FocusEvent) { e.target.style.background = "rgba(0, 0, 0, 0.02)"; e.target.style.border = "1px solid rgba(0, 0, 0, 0.06)"; e.target.style.boxShadow = "none"; } type Props = { isOpen: boolean; onClose: () => void; brandId: string; onSuccess?: (locationId: string) => void; }; export default function AddLocationModal({ isOpen, onClose, brandId, onSuccess }: Props) { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [name, setName] = useState(""); const [address, setAddress] = useState(""); const [city, setCity] = useState(""); const [stateVal, setStateVal] = useState(""); const [zip, setZip] = useState(""); const [phone, setPhone] = useState(""); const [contactName, setContactName] = useState(""); const [contactEmail, setContactEmail] = useState(""); const [notes, setNotes] = useState(""); const reset = useCallback(() => { setName(""); setAddress(""); setCity(""); setStateVal(""); setZip(""); setPhone(""); setContactName(""); setContactEmail(""); setNotes(""); setError(null); }, []); const handleClose = useCallback(() => { if (loading) return; reset(); onClose(); }, [loading, reset, onClose]); const handleSubmit = useCallback( async (e: React.FormEvent) => { e.preventDefault(); setError(null); if (!name.trim()) { setError("Venue name is required."); return; } setLoading(true); try { const result = await createLocation(brandId, { name: name.trim(), address: address.trim() || null, city: city.trim() || null, state: stateVal.trim().toUpperCase() || null, zip: zip.trim() || null, phone: phone.trim() || null, contact_name: contactName.trim() || null, contact_email: contactEmail.trim() || null, notes: notes.trim() || null, active: true, }); if (result.success) { onSuccess?.(result.id); reset(); onClose(); } else { setError(result.error ?? "Failed to create location"); } } catch { setError("Network error. Please try again."); } finally { setLoading(false); } }, [brandId, name, address, city, stateVal, zip, phone, contactName, contactEmail, notes, onSuccess, reset, onClose] ); if (!isOpen) return null; return (
{error && (
{error}
)}
setName(e.target.value)} placeholder="Tractor Supply" className="w-full rounded-xl px-4 py-3 text-sm text-stone-900 placeholder:text-stone-400 transition-all" style={inputStyle} onFocus={handleFocus} onBlur={handleBlur} required />
setAddress(e.target.value)} placeholder="13778 E I-25 Frontage Rd" className="w-full rounded-xl px-4 py-3 text-sm text-stone-900 placeholder:text-stone-400 transition-all" style={inputStyle} onFocus={handleFocus} onBlur={handleBlur} />
setCity(e.target.value)} placeholder="Wellington" className="w-full rounded-xl px-4 py-3 text-sm text-stone-900 placeholder:text-stone-400 transition-all" style={inputStyle} onFocus={handleFocus} onBlur={handleBlur} />
setStateVal(e.target.value.toUpperCase())} placeholder="CO" maxLength={2} className="w-full rounded-xl px-4 py-3 text-sm text-stone-900 placeholder:text-stone-400 transition-all" style={inputStyle} onFocus={handleFocus} onBlur={handleBlur} />
setZip(e.target.value)} placeholder="80549" className="w-full rounded-xl px-4 py-3 text-sm text-stone-900 placeholder:text-stone-400 transition-all" style={inputStyle} onFocus={handleFocus} onBlur={handleBlur} />
setPhone(e.target.value)} placeholder="(970) 555-1234" className="w-full rounded-xl px-4 py-3 text-sm text-stone-900 placeholder:text-stone-400 transition-all" style={inputStyle} onFocus={handleFocus} onBlur={handleBlur} />
setContactName(e.target.value)} placeholder="Store manager" className="w-full rounded-xl px-4 py-3 text-sm text-stone-900 placeholder:text-stone-400 transition-all" style={inputStyle} onFocus={handleFocus} onBlur={handleBlur} />
setContactEmail(e.target.value)} placeholder="manager@example.com" className="w-full rounded-xl px-4 py-3 text-sm text-stone-900 placeholder:text-stone-400 transition-all" style={inputStyle} onFocus={handleFocus} onBlur={handleBlur} />