From fd96f81bdf1ad6bfe8c7a60571eb9ea70f432ff8 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Thu, 20 Jun 2024 11:54:11 +0700 Subject: update filter promotion-program --- .../components/ProductFilterDesktopPromotion.jsx | 186 ++++++++++++--------- 1 file changed, 110 insertions(+), 76 deletions(-) (limited to 'src/lib') diff --git a/src/lib/product/components/ProductFilterDesktopPromotion.jsx b/src/lib/product/components/ProductFilterDesktopPromotion.jsx index 332d2374..0815b881 100644 --- a/src/lib/product/components/ProductFilterDesktopPromotion.jsx +++ b/src/lib/product/components/ProductFilterDesktopPromotion.jsx @@ -1,98 +1,132 @@ -import { useRouter } from 'next/router' -import { useEffect, useState } from 'react' -import _ from 'lodash' -import { toQuery } from 'lodash-contrib' -import { Button } from '@chakra-ui/react' -import { MultiSelect } from 'primereact/multiselect'; +import { useRouter } from 'next/router'; +import { useEffect, useState } from 'react'; +import _ from 'lodash'; +import { toQuery } from 'lodash-contrib'; +import { Button } from '@chakra-ui/react'; +import { MultiSelect } from 'react-multi-select-component'; const ProductFilterDesktop = ({ brands, categories, prefixUrl }) => { - const router = useRouter() - const { query } = router + const router = useRouter(); + const { query } = router; + const [order, setOrder] = useState(query?.orderBy); + const [brandValues, setBrand] = useState([]); + const [categoryValues, setCategory] = useState([]); + const [priceFrom, setPriceFrom] = useState(query?.priceFrom); + const [priceTo, setPriceTo] = useState(query?.priceTo); + const [stock, setStock] = useState(query?.stock); + const [activeRange, setActiveRange] = useState(null); + const [isBrandDropdownClicked, setIsBrandDropdownClicked] = useState(false); + const [isCategoryDropdownClicked, setIsCategoryDropdownClicked] = useState(false); - const [selectedBrand, setSelectedBrand] = useState(null); - const [selectedCategory, setSelectedCategory] = useState(null); + // Effect to set brandValues from query parameter 'brand' + useEffect(() => { + const brandParam = query?.brand; + if (brandParam) { + const brandsArray = brandParam.split(',').map((b) => ({ + label: b, + value: b, + })); + setBrand(brandsArray); + } + + }, [query.brand]); // Trigger effect whenever query.brand changes + + useEffect(() => { + const categoryParam = query?.category; + if (categoryParam) { + const categoriesArray = categoryParam.split(',').map((c) => ({ + label: c, + value: c, + })); + setCategory(categoriesArray); + } + }, [query.category]); // Trigger effect whenever query.category changes const handleSubmit = () => { let params = { q: router.query.q, - orderBy: query?.orderBy, - brand: selectedBrand ? selectedBrand.map(b => b.code).join(',') : '', - category: query?.category, - priceFrom: query?.priceFrom, - priceTo: query?.priceTo, - stock: query?.stock - } - params = _.pickBy(params, _.identity) - params = toQuery(params) + orderBy: order, + brand: brandValues.map((b) => b.value).join(','), + category: categoryValues.map((c) => c.value).join(','), + priceFrom, + priceTo, + stock: stock, + }; + params = _.pickBy(params, _.identity); + params = toQuery(params); - const slug = Array.isArray(router.query.slug) ? router.query.slug[0] : router.query.slug; + const slug = Array.isArray(router.query.slug) + ? router.query.slug[0] + : router.query.slug; if (slug) { - router.push(`${prefixUrl}/${slug}?${params}`) + router.push(`${prefixUrl}/${slug}?${params}`); } else { - router.push(`${prefixUrl}?${params}`) + router.push(`${prefixUrl}?${params}`); } - } - - const countryTemplate = (option) => { - return ( -
-
{option.name}
-
- ); }; - const brandOptions = brands.map(brand => ({ - name: `${brand.brand} (${brand.qty})`, - code: brand.brand - })); - const categoryOptions = categories.map(category => ({ - name: `${category.name} (${category.qty})`, - code: category.name + const brandOptions = brands.map((brand) => ({ + label: `${brand.brand} (${brand.qty})`, + value: brand.brand, })); - const panelFooterTemplate = () => { - return ( - - ); - }; + const categoryOptions = categories.map((category) => ({ + label: `${category.name} (${category.qty})`, + value: category.name, + })); return ( -
-
- - setSelectedBrand(e.value)} - optionLabel="name" - placeholder="Pilih Brand" - itemTemplate={countryTemplate} - panelFooterTemplate={panelFooterTemplate} - className="w-full" - display="chip" - /> -
+ <> +
+ {/* Brand MultiSelect */} +
+
+ +
+ setIsBrandDropdownClicked(isOpen)} + hasSelectAll={false} + /> +
+
+
+ + {/* Category MultiSelect */} +
+
+ +
+ + setIsCategoryDropdownClicked(!isCategoryDropdownClicked) + } + hasSelectAll={false} + /> +
+
+
-
- - setSelectedCategory(e.value)} - optionLabel="name" - placeholder="Pilih Kategori" - itemTemplate={countryTemplate} - panelFooterTemplate={panelFooterTemplate} - className="w-full" - display="chip" - /> + {/* Apply Button */} +
+
+ +
+
-
- ) -} + + ); +}; -export default ProductFilterDesktop +export default ProductFilterDesktop; -- cgit v1.2.3