'use client' import { CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from 'cmdk' import {Check, ChevronsUpDown, MapPinCheck, MapPinPlus} from 'lucide-react' import {useLocale, useTranslations} from 'next-intl' import {useState} from 'react' import {useDebouncedCallback} from 'use-debounce' import { type Settlement, Street, formatSettlement, getApi } from '@/lib/nova-post-helper' import {cn, dump} from '@/lib/utils' import {Button} from '@/ui/button' import {Command} from '@/ui/command' import {Popover, PopoverContent, PopoverTrigger} from '@/ui/popover' const url = '/api/nova-post' export default function SearchAddress({ onSelectHandler }: { onSelectHandler: any }) { const t = useTranslations('cart.post') const locale = useLocale() const [streets, setStreets] = useState([]) const [streetsOpen, setStreetsOpen] = useState(false) const [streetsValue, setStreetsValue] = useState('') const handleStreetSearch = useDebouncedCallback( async (e: string): Promise => { if (e.length < 3) { setStreets([]) return } const response = await getApi(url + `?scope=streets&q=` + encodeURI(e)) if (response.ok) { let json = JSON.parse(JSON.stringify(await response.json())) const {Addresses} = json[0] setStreets(Addresses) } else { setStreets([]) } }, 1000 ) const streetDescription = (streetsValue: string): string => { const street: Street | undefined = streets.find( (street: Street) => street.Present === streetsValue ) if (!street) { return '' } return streetsValue } return (
{/*
{dump(streets[0]['Addresses'])}
*/}
handleStreetSearch(e)} /> {t('notFount')} {streets.map((street: Street, index: number) => ( { setStreetsValue( currentValue === streetsValue ? '' : currentValue ) onSelectHandler( currentValue === streetsValue ? {} : { Ref: street.SettlementStreetRef, Description: street.Present, DescriptionRu: street.SettlementStreetDescriptionRu } ) setStreetsOpen(false) }} > {street?.Present} ))}
) }