"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { updateStop } from "@/actions/stops/update-stop"; import { AdminInput, AdminTextInput, AdminSelect, AdminButton, useToast } from "./design-system"; type Brand = { id: string; name: string; slug: string; }; type StopEditFormProps = { stop: { id: string; city: string; state: string; date: string; time: string; location: string; slug: string; active: boolean; brand_id: string; address?: string | null; zip?: string | null; cutoff_time?: string | null; }; brands: Brand[]; }; export default function StopEditForm({ stop, brands }: StopEditFormProps) { const router = useRouter(); const { success: showSuccess, error: showError } = useToast(); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [saved, setSaved] = useState(false); const [city, setCity] = useState(stop.city); const [state, setState] = useState(stop.state); const [date, setDate] = useState(stop.date); const [time, setTime] = useState(stop.time); const [location, setLocation] = useState(stop.location); const [active, setActive] = useState(stop.active); const [brand_id, setBrand_id] = useState(stop.brand_id); const [address, setAddress] = useState(stop.address ?? ""); const [zip, setZip] = useState(stop.zip ?? ""); const [cutoff_time, setCutoff_time] = useState(stop.cutoff_time ?? ""); // Validation errors const [fieldErrors, setFieldErrors] = useState>({}); function validateForm(): boolean { const errors: Record = {}; if (!city.trim()) { errors.city = "City is required"; } if (!state.trim()) { errors.state = "State is required"; } if (!date.trim()) { errors.date = "Date is required"; } setFieldErrors(errors); return Object.keys(errors).length === 0; } async function handleSave() { if (!validateForm()) { showError("Validation failed", "Please fix the errors below"); return; } setSaving(true); setError(null); setSaved(false); const result = await updateStop(stop.id, brand_id, { city, state, location, date, time, active, address: address || null, zip: zip || null, cutoff_time: cutoff_time || null, }); if (!result.success) { showError("Failed to save", result.error ?? "Please try again"); setError(result.error ?? "Failed to save"); setSaving(false); return; } showSuccess("Stop updated", `${city}, ${state} has been saved`); setSaved(true); setSaving(false); router.refresh(); } return (
{error && (
{error}
)}
{ setCity(e.target.value); setFieldErrors((prev) => { const next = { ...prev }; delete next.city; return next; }); }} placeholder="City name" className={`w-full rounded-xl border px-3 py-2.5 text-sm text-[var(--admin-text-primary)] outline-none focus:ring-2 focus:ring-[var(--admin-accent)]/20 ${ fieldErrors.city ? "border-red-400 bg-red-50" : "border-[var(--admin-border)] bg-white focus:border-[var(--admin-accent)]" }`} /> {fieldErrors.city &&

{fieldErrors.city}

}
{ setState(e.target.value); setFieldErrors((prev) => { const next = { ...prev }; delete next.state; return next; }); }} placeholder="State code" className={`w-full rounded-xl border px-3 py-2.5 text-sm text-[var(--admin-text-primary)] outline-none focus:ring-2 focus:ring-[var(--admin-accent)]/20 ${ fieldErrors.state ? "border-red-400 bg-red-50" : "border-[var(--admin-border)] bg-white focus:border-[var(--admin-accent)]" }`} /> {fieldErrors.state &&

{fieldErrors.state}

}
{ setDate(e.target.value); setFieldErrors((prev) => { const next = { ...prev }; delete next.date; return next; }); }} className={`w-full rounded-xl border px-3 py-2.5 text-sm text-[var(--admin-text-primary)] outline-none focus:ring-2 focus:ring-[var(--admin-accent)]/20 ${ fieldErrors.date ? "border-red-400 bg-red-50" : "border-[var(--admin-border)] bg-white focus:border-[var(--admin-accent)]" }`} /> {fieldErrors.date &&

{fieldErrors.date}

}
setTime(e.target.value)} placeholder="e.g. 8:00 AM – 2:00 PM" className="w-full rounded-xl border border-[var(--admin-border)] bg-white px-3 py-2.5 text-sm text-[var(--admin-text-primary)] outline-none focus:border-[var(--admin-accent)] focus:ring-2 focus:ring-[var(--admin-accent)]/20" />
setLocation(e.target.value)} placeholder="Street address or intersection" className="w-full rounded-xl border border-[var(--admin-border)] bg-white px-3 py-2.5 text-sm text-[var(--admin-text-primary)] outline-none focus:border-[var(--admin-accent)] focus:ring-2 focus:ring-[var(--admin-accent)]/20" />
setAddress(e.target.value)} placeholder="123 Main St" className="w-full rounded-xl border border-[var(--admin-border)] bg-white px-3 py-2.5 text-sm text-[var(--admin-text-primary)] outline-none focus:border-[var(--admin-accent)] focus:ring-2 focus:ring-[var(--admin-accent)]/20" />
setZip(e.target.value)} placeholder="80102" maxLength={10} className="w-full rounded-xl border border-[var(--admin-border)] bg-white px-3 py-2.5 text-sm text-[var(--admin-text-primary)] outline-none focus:border-[var(--admin-accent)] focus:ring-2 focus:ring-[var(--admin-accent)]/20" />
setCutoff_time(e.target.value)} className="w-full rounded-xl border border-[var(--admin-border)] bg-white px-3 py-2.5 text-sm text-[var(--admin-text-primary)] outline-none focus:border-[var(--admin-accent)] focus:ring-2 focus:ring-[var(--admin-accent)]/20" /> setBrand_id(e.target.value)} options={brands.map((b) => ({ value: b.id, label: b.name }))} />
{saving ? "Saving..." : "Save Changes"}
); }