"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { createStop } from "@/actions/stops/create-stop"; import { AdminInput, AdminTextInput, AdminSelect, AdminButton, useToast } from "./design-system"; type Stop = { city: string; state: string; location: string; date: string; time: string; brand_id: string; active: boolean; address?: string | null; zip?: string | null; cutoff_time?: string | null; }; type Props = { duplicateFrom?: Stop | null; }; export default function NewStopForm({ duplicateFrom }: Props) { const router = useRouter(); const { success: showSuccess, error: showError } = useToast(); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const defaultBrand = duplicateFrom?.brand_id ?? "64294306-5f42-463d-a5e8-2ad6c81a96de"; // Form state const [city, setCity] = useState(duplicateFrom?.city ?? ""); const [state, setState] = useState(duplicateFrom?.state ?? ""); const [location, setLocation] = useState(duplicateFrom?.location ?? ""); const [date, setDate] = useState(duplicateFrom?.date ?? ""); const [time, setTime] = useState(duplicateFrom?.time ?? ""); const [brandId, setBrandId] = useState(defaultBrand); const [active, setActive] = useState("true"); const [address, setAddress] = useState(duplicateFrom?.address ?? ""); const [zip, setZip] = useState(duplicateFrom?.zip ?? ""); const [cutoffTime, setCutoffTime] = useState(duplicateFrom?.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 (!location.trim()) { errors.location = "Location is required"; } if (!date.trim()) { errors.date = "Date is required"; } if (!time.trim()) { errors.time = "Time is required"; } if (!brandId) { errors.brandId = "Brand is required"; } setFieldErrors(errors); return Object.keys(errors).length === 0; } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!validateForm()) { showError("Validation failed", "Please fix the errors below"); return; } setLoading(true); setError(null); const result = await createStop(brandId, { city, state, location, date, time, active: active === "true", address: address || null, zip: zip || null, cutoff_time: cutoffTime || null, }); if (!result.success) { showError("Failed to create stop", result.error ?? "Please try again"); setError(result.error ?? "Failed to create stop"); setLoading(false); return; } showSuccess("Stop created", `${city}, ${state} has been added`); if (result.id) { router.push(`/admin/stops/${result.id}`); } else { router.push("/admin/stops"); } router.refresh(); } return (
{error && (
{error}
)}
{ setCity(e.target.value); setFieldErrors((prev) => { const next = { ...prev }; delete next.city; return next; }); }} placeholder="e.g. Denver" 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="e.g. CO" 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}

}
{ setLocation(e.target.value); setFieldErrors((prev) => { const next = { ...prev }; delete next.location; return next; }); }} placeholder="e.g. Southwest Plaza Parking Lot" 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.location ? "border-red-400 bg-red-50" : "border-[var(--admin-border)] bg-white focus:border-[var(--admin-accent)]" }`} /> {fieldErrors.location &&

{fieldErrors.location}

}
{ 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); setFieldErrors((prev) => { const next = { ...prev }; delete next.time; return next; }); }} placeholder="e.g. 8:00 AM – 2:00 PM" 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.time ? "border-red-400 bg-red-50" : "border-[var(--admin-border)] bg-white focus:border-[var(--admin-accent)]" }`} /> {fieldErrors.time &&

{fieldErrors.time}

}
{fieldErrors.brandId &&

{fieldErrors.brandId}

}
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" />

Customers must order before this time to be included at this stop.

setCutoffTime(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" />
{loading ? "Creating..." : "Create Stop"} Cancel
); }