diff options
Diffstat (limited to 'src/lib/product/components')
| -rw-r--r-- | src/lib/product/components/ProductSearch.jsx | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/src/lib/product/components/ProductSearch.jsx b/src/lib/product/components/ProductSearch.jsx index 30722c82..ae9618d2 100644 --- a/src/lib/product/components/ProductSearch.jsx +++ b/src/lib/product/components/ProductSearch.jsx @@ -19,8 +19,9 @@ import whatsappUrl from '@/core/utils/whatsappUrl' const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { const router = useRouter() const { page = 1 } = query + const [q, setQ] = useState(query?.q || '*') if (defaultBrand) query.brand = defaultBrand.toLowerCase() - const { productSearch } = useProductSearch({ query }) + const { productSearch } = useProductSearch({ query: { ...query, q } }) const [products, setProducts] = useState(null) const [spellings, setSpellings] = useState(null) const popup = useActive() @@ -33,7 +34,7 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { const productFound = productSearch.data?.response.numFound useEffect(() => { - if (productFound == 0 && query.q) { + if (productFound == 0 && query.q && !spellings) { searchSpellApi({ query: query.q }).then((response) => { const oddIndexSuggestions = response.data.spellcheck.suggestions.filter( (_, index) => index % 2 === 1 @@ -51,10 +52,12 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { return acc }, []) + setQ(dataSpellings[0]) + setSpellings(dataSpellings) }) } - }, [productFound, query]) + }, [productFound, query, spellings]) const brands = productSearch.data?.facetCounts?.facetFields?.manufactureName?.filter( (value, index) => { @@ -89,10 +92,8 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { } useEffect(() => { - if (!products) { - setProducts(productSearch.data?.response?.products) - } - }, [query, products, productSearch]) + setProducts(productSearch.data?.response?.products) + }, [productSearch]) const SpellingComponent = useMemo(() => { return ( @@ -116,7 +117,7 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { <h1 className='mb-2 font-semibold text-h-sm'>Produk</h1> <div className='mb-2 leading-6 text-gray_r-11'> - {productFound > 0 ? ( + {!spellings ? ( <> Menampilkan {pageCount > 1 ? ( @@ -186,7 +187,7 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { <h1 className='text-2xl mb-2 font-semibold'>Hasil Pencarian</h1> <div className='flex justify-between items-center mb-2'> <div className='mb-2 leading-6 text-gray_r-11'> - {productFound > 0 ? ( + {!spellings ? ( <> Menampilkan {pageCount > 1 ? ( @@ -248,9 +249,13 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { <span> Barang yang anda cari tidak ada?{' '} <a - href={query?.q ? whatsappUrl('productSearch', { - name: query.q - }) : whatsappUrl()} + href={ + query?.q + ? whatsappUrl('productSearch', { + name: query.q + }) + : whatsappUrl() + } className='text-danger-500' > Hubungi Kami |
