From f4fb8d1f93024cfe907a5783215cf9c939dd59bc Mon Sep 17 00:00:00 2001
From: "HATEC\\SPVDEV001" <tri.susilo@altama.co.id>
Date: Wed, 11 Oct 2023 11:02:04 +0700
Subject: filter by limit rows

---
 src/lib/product/components/ProductSearch.jsx | 40 +++++++++++++++++++++++-----
 1 file changed, 34 insertions(+), 6 deletions(-)

(limited to 'src/lib/product/components/ProductSearch.jsx')

diff --git a/src/lib/product/components/ProductSearch.jsx b/src/lib/product/components/ProductSearch.jsx
index 3c3dbfd2..c79e355e 100644
--- a/src/lib/product/components/ProductSearch.jsx
+++ b/src/lib/product/components/ProductSearch.jsx
@@ -20,17 +20,20 @@ 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]
 
+  console.log('ini product search', productSearch.data)
   const pageCount = Math.ceil(
-    productSearch.data?.response.numFound / productSearch.data?.responseHeader.params.rows
+    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(() => {
@@ -101,7 +104,17 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => {
   const handleOrderBy = (e) => {
     let params = {
       ...router.query,
-      orderBy: e.target.value
+      orderBy: e.target.value,
+    }
+    params = _.pickBy(params, _.identity)
+    params = toQuery(params)
+    router.push(`${prefixUrl}?${params}`)
+  }
+
+  const handleLimit = (e) => {
+    let params = {
+      ...router.query,
+      limit: e.target.value,
     }
     params = _.pickBy(params, _.identity)
     params = toQuery(params)
@@ -210,9 +223,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)}
                         &nbsp;dari&nbsp;
                       </>
                     ) : (
@@ -247,6 +260,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 />}
-- 
cgit v1.2.3

