From 0a0c497204acbac562700d80f38e74aa9ffcd94e Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 1 Dec 2022 16:26:21 +0700 Subject: dynamic filter, dynamic pagination, detail brand, to title case --- src/components/Filter.js | 98 ++++++++++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 40 deletions(-) (limited to 'src/components/Filter.js') diff --git a/src/components/Filter.js b/src/components/Filter.js index 385c585d..d2a5bf1b 100644 --- a/src/components/Filter.js +++ b/src/components/Filter.js @@ -11,7 +11,8 @@ const Filter = ({ defaultCategory, defaultBrand, defaultOrderBy, - searchResults + searchResults, + disableFilter = [] }) => { const router = useRouter(); @@ -24,13 +25,22 @@ const Filter = ({ const [brands, setBrands] = useState([]); const filterRoute = () => { - let filterRoute = defaultRoute; - if (selectedBrand) filterRoute += `&brand=${selectedBrand}`; - if (selectedCategory) filterRoute += `&category=${selectedCategory}`; - if (priceFrom > 0) filterRoute += `&price_from=${priceFrom}`; - if (priceTo > 0) filterRoute += `&price_to=${priceTo}`; - if (orderBy) filterRoute += `&order_by=${orderBy}`; - return filterRoute; + let filterRoute = []; + let filterRoutePrefix = '?'; + if (selectedBrand) filterRoute.push(`brand=${selectedBrand}`); + if (selectedCategory) filterRoute.push(`category=${selectedCategory}`); + if (priceFrom > 0) filterRoute.push(`price_from=${priceFrom}`); + if (priceTo > 0) filterRoute.push(`price_to=${priceTo}`); + if (orderBy) filterRoute.push(`order_by=${orderBy}`); + + if (defaultRoute.includes('?')) filterRoutePrefix = '&'; + if (filterRoute.length > 0) { + filterRoute = filterRoutePrefix + filterRoute.join('&'); + } else { + filterRoute = ''; + } + + return defaultRoute + filterRoute; } useEffect(() => { @@ -82,40 +92,48 @@ const Filter = ({
-
- -
- - - + {!disableFilter.includes('orderBy') ? ( +
+ +
+ + + +
+
+ ) : ''} + {!disableFilter.includes('category') ? ( +
+ +
-
-
- - -
-
- - -
-
- -
- setPriceFrom(e.target.value)}/> - - setPriceTo(e.target.value)}/> + ) : ''} + {!disableFilter.includes('brand') ? ( +
+ +
-
+ ) : ''} + {!disableFilter.includes('price') ? ( +
+ +
+ setPriceFrom(e.target.value)}/> + + setPriceTo(e.target.value)}/> +
+
+ ) : ''} -- cgit v1.2.3