import React, { FC } from 'react' import cn from 'classnames' import Link from 'next/link' import { useState } from 'react' import { useRouter } from 'next/router' import useSearch from '@framework/products/use-search' import { ProductCard } from '@components/product' import { Container, Grid, Skeleton } from '@components/ui' import rangeMap from '@lib/range-map' import getSlug from '@lib/get-slug' import { filterQuery, getCategoryPath, getDesignerPath, useSearchMeta, } from '@lib/search' import { ModuleWithInit } from '@agility/nextjs' const SORT = Object.entries({ 'latest-desc': 'Latest arrivals', 'trending-desc': 'Trending', 'price-asc': 'Price: Low to high', 'price-desc': 'Price: High to low', }) interface ICustomData { categories: any brands: any } interface IModule { } const ProductSearch: ModuleWithInit = ({ customData }) => { const categories:[any] = customData.categories const brands:[any] = customData.brands const [activeFilter, setActiveFilter] = useState('') const [toggleFilter, setToggleFilter] = useState(false) const router = useRouter() const { asPath } = router const { q, sort } = router.query // `q` can be included but because categories and designers can't be searched // in the same way of products, it's better to ignore the search input if one // of those is selected const query = filterQuery({ sort }) const { pathname, category, brand } = useSearchMeta(asPath) const activeCategory = categories.find( (cat) => getSlug(cat.path) === category ) const activeBrand = brands.find( (b) => getSlug(b.node.path) === `brands/${brand}` )?.node const { data } = useSearch({ search: typeof q === 'string' ? q : '', categoryId: activeCategory?.entityId, brandId: activeBrand?.entityId, sort: typeof sort === 'string' ? sort : '', }) const handleClick = (event: any, filter: string) => { if (filter !== activeFilter) { setToggleFilter(true) } else { setToggleFilter(!toggleFilter) } setActiveFilter(filter) } return (
{/* Categories */}
{/* Designs */}
{/* Products */}
{(q || activeCategory || activeBrand) && (
{data ? ( <> Showing {data.products.length} results{' '} {q && ( <> for "{q}" )} {q ? ( <> There are no products that match "{q}" ) : ( <> There are no products that match the selected category & designer )} ) : q ? ( <> Searching for: "{q}" ) : ( <>Searching... )}
)} {data ? ( {data.products.map(({ node }) => ( ))} ) : ( {rangeMap(12, (i) => ( ))} )}
{/* Sort */}
) } export default ProductSearch