"use client"; import { Plus, Save, Trash2 } from "lucide-react"; import { useRouter } from "next/navigation"; import { useState } from "react"; import type { Family, Person } from "@/lib/types"; import { ImageUploader } from "./ImageUploader"; const blankPerson = (): Person => ({ id: crypto.randomUUID(), firstName: "", lastName: "", email: "", phone: "", birthday: "" }); const usRegions = [ ["AL", "Alabama"], ["AK", "Alaska"], ["AZ", "Arizona"], ["AR", "Arkansas"], ["CA", "California"], ["CO", "Colorado"], ["CT", "Connecticut"], ["DE", "Delaware"], ["DC", "District of Columbia"], ["FL", "Florida"], ["GA", "Georgia"], ["HI", "Hawaii"], ["ID", "Idaho"], ["IL", "Illinois"], ["IN", "Indiana"], ["IA", "Iowa"], ["KS", "Kansas"], ["KY", "Kentucky"], ["LA", "Louisiana"], ["ME", "Maine"], ["MD", "Maryland"], ["MA", "Massachusetts"], ["MI", "Michigan"], ["MN", "Minnesota"], ["MS", "Mississippi"], ["MO", "Missouri"], ["MT", "Montana"], ["NE", "Nebraska"], ["NV", "Nevada"], ["NH", "New Hampshire"], ["NJ", "New Jersey"], ["NM", "New Mexico"], ["NY", "New York"], ["NC", "North Carolina"], ["ND", "North Dakota"], ["OH", "Ohio"], ["OK", "Oklahoma"], ["OR", "Oregon"], ["PA", "Pennsylvania"], ["RI", "Rhode Island"], ["SC", "South Carolina"], ["SD", "South Dakota"], ["TN", "Tennessee"], ["TX", "Texas"], ["UT", "Utah"], ["VT", "Vermont"], ["VA", "Virginia"], ["WA", "Washington"], ["WV", "West Virginia"], ["WI", "Wisconsin"], ["WY", "Wyoming"], ["AS", "American Samoa"], ["GU", "Guam"], ["MP", "Northern Mariana Islands"], ["PR", "Puerto Rico"], ["VI", "U.S. Virgin Islands"], ] as const; const canadianRegions = [["AB", "Alberta"], ["BC", "British Columbia"], ["MB", "Manitoba"], ["NB", "New Brunswick"], ["NL", "Newfoundland and Labrador"], ["NS", "Nova Scotia"], ["NT", "Northwest Territories"], ["NU", "Nunavut"], ["ON", "Ontario"], ["PE", "Prince Edward Island"], ["QC", "Quebec"], ["SK", "Saskatchewan"], ["YT", "Yukon"]] as const; function PersonFields({ person, onChange, onRemove }: { person: Person; onChange: (person: Person) => void; onRemove: () => void }) { return
onChange({ ...person, photoId })}/>
onChange({ ...person, firstName: event.target.value })}/>
onChange({ ...person, lastName: event.target.value })}/>
onChange({ ...person, email: event.target.value })}/>
onChange({ ...person, phone: event.target.value })}/>
onChange({ ...person, birthday: event.target.value })}/>
; } export function FamilyEditor({ initialFamily }: { initialFamily: Family }) { const router = useRouter(); const [family, setFamily] = useState(initialFamily); const [pending, setPending] = useState(false); const [message, setMessage] = useState(""); function updatePerson(group: "adults" | "children", index: number, person?: Person) { const next = [...family[group]]; if (person) next[index] = person; else next.splice(index, 1); setFamily({ ...family, [group]: next }); } async function save(event: React.FormEvent) { event.preventDefault(); setPending(true); setMessage(""); try { const response = await fetch("/api/family", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(family) }); const result = await response.json(); if (response.ok) { setFamily(result.family); setMessage("Your family profile has been saved."); router.refresh(); } else setMessage(result.error); } catch { setMessage("Your changes could not be saved. Check your connection and try again."); } finally { setPending(false); } } return
setFamily({ ...family, photoId })}/>
setFamily({ ...family, familyName: event.target.value })}/>
setFamily({ ...family, address: event.target.value })}/>
setFamily({ ...family, city: event.target.value })}/>
setFamily({ ...family, postalCode: event.target.value })}/>
setFamily({ ...family, homePhone: event.target.value })}/>

Parents & adults

At least one adult is required.

{family.adults.map((person, index) => updatePerson("adults", index, next)} onRemove={() => updatePerson("adults", index)}/>)}

Children

Full birth dates are checked to ensure children are under 18.

{family.children.map((person, index) => updatePerson("children", index, next)} onRemove={() => updatePerson("children", index)}/>)}

{message}

; }