import { useState } from "react"; import type { CampaignId, Location } from "@/lib/types"; import { pb } from "@/lib/pocketbase"; /** * Renders a form to add a new location. Calls onCreate with the new location document. */ export const LocationForm = ({ campaign, onCreate, }: { campaign: CampaignId; onCreate: (location: Location) => Promise; }) => { const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [adding, setAdding] = useState(false); const [error, setError] = useState(null); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!name.trim()) return; setAdding(true); setError(null); try { const locationDoc: Location = await pb.collection("documents").create({ campaign, data: { location: { name, description, }, }, }); setName(""); setDescription(""); await onCreate(locationDoc); } catch (e: any) { setError(e?.message || "Failed to add location."); } finally { setAdding(false); } } return (

Create new location

setName(e.target.value)} disabled={adding} aria-label="Name" />