import React, { useState, useMemo, useRef, useEffect } from 'react'; import { useQuery } from '@apollo/client'; import { Combobox } from '@headlessui/react'; import { GET_LAXIMO_BRANDS, GET_LAXIMO_CATALOG_INFO } from '@/lib/graphql'; import { LaximoBrand, LaximoVehicleSearchResult } from '@/types/laximo'; import WizardSearchForm from './WizardSearchForm'; import VehicleSearchResults from './VehicleSearchResults'; import { useRouter } from 'next/router'; const BrandWizardSearchSection: React.FC = () => { const router = useRouter(); const { data: brandsData, loading: brandsLoading, error: brandsError } = useQuery<{ laximoBrands: LaximoBrand[] }>(GET_LAXIMO_BRANDS, { errorPolicy: 'all' }); const [selectedBrand, setSelectedBrand] = useState(null); const [vehicles, setVehicles] = useState(null); const [brandQuery, setBrandQuery] = useState(''); const buttonRef = useRef(null); const inputRef = useRef(null); const [isOpen, setIsOpen] = useState(false); // Получение информации о каталоге через useQuery const { data: catalogData, loading: catalogLoading, error: catalogError } = useQuery( GET_LAXIMO_CATALOG_INFO, { variables: { catalogCode: selectedBrand?.code }, skip: !selectedBrand, errorPolicy: 'all', } ); // Мемоизация брендов для селекта const brands = useMemo(() => { if (brandsData?.laximoBrands?.length) { return [...brandsData.laximoBrands].sort((a, b) => a.name.localeCompare(b.name)); } return []; }, [brandsData]); // Фильтрация брендов по поисковому запросу const filteredBrands = useMemo(() => { if (!brandQuery) return brands; return brands.filter(b => b.name.toLowerCase().includes(brandQuery.toLowerCase())); }, [brands, brandQuery]); // Автоматически выбираем бренд из query, если есть selected useEffect(() => { if (!brandsData?.laximoBrands) return; const selected = router.query.selected; if (selected && !selectedBrand) { const found = brandsData.laximoBrands.find(b => b.name.toLowerCase() === String(selected).toLowerCase() || b.code.toLowerCase() === String(selected).toLowerCase()); if (found) setSelectedBrand(found); } }, [brandsData, router.query.selected, selectedBrand]); // Обработчик выбора бренда const handleBrandChange = (brand: LaximoBrand | null) => { setSelectedBrand(brand); setVehicles(null); }; // Обработчик найденных авто const handleVehicleFound = (vehicles: LaximoVehicleSearchResult[]) => { setVehicles(vehicles); }; // Каталожная информация const catalogInfo = catalogData?.laximoCatalogInfo; return (
{/*
Подбор автомобиля по параметрам
*/} {/* Combobox бренда */}

Марка автомобиля

{/* Невидимая кнопка поверх инпута */}
{/* Каталог и wizard */} {catalogLoading && selectedBrand && (
Загружаем каталог...
)} {catalogError && selectedBrand && (
Ошибка загрузки каталога
)} {catalogInfo && catalogInfo.supportparameteridentification2 && ( <>
{vehicles && (
)} )} {catalogInfo && !catalogInfo.supportparameteridentification2 && (
Для выбранного бренда подбор по параметрам недоступен.
)}
); }; export default BrandWizardSearchSection;