diff options
Diffstat (limited to 'src/lib/product/components/ProductSearch.jsx')
| -rw-r--r-- | src/lib/product/components/ProductSearch.jsx | 87 |
1 files changed, 72 insertions, 15 deletions
diff --git a/src/lib/product/components/ProductSearch.jsx b/src/lib/product/components/ProductSearch.jsx index f6987051..edc98197 100644 --- a/src/lib/product/components/ProductSearch.jsx +++ b/src/lib/product/components/ProductSearch.jsx @@ -20,17 +20,17 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { const router = useRouter() const { page = 1 } = query const [q, setQ] = useState(query?.q || '*') + const [limit, setLimit] = useState(query?.limit || 30) if (defaultBrand) query.brand = defaultBrand.toLowerCase() - const { productSearch } = useProductSearch({ query: { ...query, q } }) + const { productSearch } = useProductSearch({ query: { ...query, q, limit } }) const [products, setProducts] = useState(null) const [spellings, setSpellings] = useState(null) const popup = useActive() + const numRows = [30, 50, 80, 100] - const pageCount = Math.ceil( - productSearch.data?.response.numFound / productSearch.data?.responseHeader.params.rows - ) + const pageCount = Math.ceil(productSearch.data?.response.numFound / limit) const productStart = productSearch.data?.responseHeader.params.start - const productRows = productSearch.data?.responseHeader.params.rows + const productRows = limit const productFound = productSearch.data?.response.numFound useEffect(() => { @@ -63,13 +63,26 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { } }, [productFound, query, spellings]) - const brands = productSearch.data?.facetCounts?.facetFields?.manufactureName?.filter( + const brands = [] + for ( + let i = 0; + i < productSearch.data?.facetCounts?.facetFields?.manufactureName.length; + i += 2 + ) { + const brand = productSearch.data?.facetCounts?.facetFields?.manufactureName[i] + const qty = productSearch.data?.facetCounts?.facetFields?.manufactureName[i + 1] + brands.push({ brand, qty }) + } + /*const brandsList = productSearch.data?.facetCounts?.facetFields?.manufactureName?.filter( (value, index) => { if (index % 2 === 0) { - return true + const brand = value + const qty = index + 1 + brands.push({ brand, qty }) } } - ) + )*/ + const categories = productSearch.data?.facetCounts?.facetFields?.categoryName?.filter( (value, index) => { if (index % 2 === 0) { @@ -95,6 +108,16 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { router.push(`${prefixUrl}?${params}`) } + const handleLimit = (e) => { + let params = { + ...router.query, + limit: e.target.value + } + params = _.pickBy(params, _.identity) + params = toQuery(params) + router.push(`${prefixUrl}?${params}`) + } + useEffect(() => { setProducts(productSearch.data?.response?.products) }, [productSearch]) @@ -127,9 +150,9 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { {pageCount > 1 ? ( <> {productStart + 1}- - {productStart + productRows > productFound + {parseInt(productStart) + parseInt(productRows) > productFound ? productFound - : productStart + productRows} + : parseInt(productStart) + parseInt(productRows)} dari </> ) : ( @@ -149,9 +172,28 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { </div> {productFound > 0 && ( - <button className='btn-light mb-6 py-2 px-5' onClick={popup.activate}> - Filter - </button> + <div className='flex items-center gap-x-2 mb-5 justify-between'> + <div> + <button className='btn-light py-2 px-5 h-[40px]' onClick={popup.activate}> + Filter + </button> + </div> + <div className=''> + <select + name='limit' + className='form-input w-24' + value={router.query?.limit || ''} + onChange={(e) => handleLimit(e)} + > + {numRows.map((option, index) => ( + <option key={index} value={option}> + {' '} + {option}{' '} + </option> + ))} + </select> + </div> + </div> )} <div className='grid grid-cols-2 gap-3'> @@ -197,9 +239,9 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { {pageCount > 1 ? ( <> {productStart + 1}- - {productStart + productRows > productFound + {parseInt(productStart) + parseInt(productRows) > productFound ? productFound - : productStart + productRows} + : parseInt(productStart) + parseInt(productRows)} dari </> ) : ( @@ -234,6 +276,21 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { ))} </select> </div> + <div className='ml-3'> + <select + name='limit' + className='form-input mt-2' + value={router.query?.limit || ''} + onChange={(e) => handleLimit(e)} + > + {numRows.map((option, index) => ( + <option key={index} value={option}> + {' '} + {option}{' '} + </option> + ))} + </select> + </div> </div> </div> {productSearch.isLoading && <ProductSearchSkeleton />} |
