diff --git a/src/components/PlansSection.tsx b/src/components/PlansSection.tsx index b064022..398ba33 100644 --- a/src/components/PlansSection.tsx +++ b/src/components/PlansSection.tsx @@ -6,8 +6,6 @@ import { toast } from "sonner"; import { z } from "zod"; import { StripeEmbeddedCheckout } from "@/components/StripeEmbeddedCheckout"; import { getStripeEnvironment, hasPaymentsConfigured } from "@/lib/stripe"; -import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; -import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command"; import { cn } from "@/lib/utils"; const STORSTOCKHOLM_CITIES = [ @@ -112,6 +110,14 @@ const PlansSection = () => { kommentarer: "", }); + const filteredCities = useMemo(() => { + const query = form.stad.trim().toLocaleLowerCase("sv-SE"); + if (!query) return STORSTOCKHOLM_CITIES; + return STORSTOCKHOLM_CITIES.filter((city) => + city.toLocaleLowerCase("sv-SE").includes(query) + ); + }, [form.stad]); + const recommendedKg = useMemo( () => Math.max(1, Math.round(employees * KG_PER_EMPLOYEE)), [employees] @@ -127,18 +133,30 @@ const PlansSection = () => { .select("company_name, phone, street_address, postal_code, city") .eq("user_id", user.id) .maybeSingle(); - setForm((prev) => ({ - ...prev, - företag: prev.företag || data?.company_name || "", - epost: prev.epost || user.email || "", - telefon: prev.telefon || data?.phone || "", - adress: prev.adress || data?.street_address || "", - postnummer: prev.postnummer || data?.postal_code || "", - stad: prev.stad || data?.city || "", - })); + setForm((prev) => { + const postalCode = prev.postnummer || data?.postal_code || ""; + const suggestedCity = cityFromPostalCode(postalCode); + + return { + ...prev, + företag: prev.företag || data?.company_name || "", + epost: prev.epost || user.email || "", + telefon: prev.telefon || data?.phone || "", + adress: prev.adress || data?.street_address || "", + postnummer: postalCode, + stad: suggestedCity || prev.stad || data?.city || "", + }; + }); })(); }, [user, showForm]); + useEffect(() => { + const suggestedCity = cityFromPostalCode(form.postnummer); + if (!suggestedCity || form.stad === suggestedCity) return; + setForm((prev) => ({ ...prev, stad: suggestedCity })); + setErrors((prev) => ({ ...prev, stad: "", postnummer: "" })); + }, [form.postnummer, form.stad]); + const handleChange = ( e: React.ChangeEvent ) => { @@ -476,47 +494,71 @@ const PlansSection = () => { - - - - - - - - - Ingen stad hittades. - - {STORSTOCKHOLM_CITIES.map((city) => ( - { - setForm((f) => ({ ...f, stad: v })); - setErrors((e) => ({ ...e, stad: "" })); - setCityOpen(false); - }} - > - - {city} - - ))} - - - - - + + {cityOpen && ( +
+ {filteredCities.length > 0 ? ( + filteredCities.map((city) => ( + + )) + ) : ( +
+ Ingen stad hittades. +
+ )} +
+ )} + {errors.stad && (

{errors.stad}

)}