From 70591a6a132d25656e4404d78d6364e21949e81a Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Fri, 20 Oct 2023 09:29:48 +0700 Subject: filter choices --- src/core/utils/formatValue.js | 11 ++- .../product/components/ProductFilterDesktop.jsx | 10 +- src/lib/product/components/ProductSearch.jsx | 109 ++++++++++++++++++++- 3 files changed, 119 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/core/utils/formatValue.js b/src/core/utils/formatValue.js index f2c17769..f0f65c69 100644 --- a/src/core/utils/formatValue.js +++ b/src/core/utils/formatValue.js @@ -7,4 +7,13 @@ const sellingProductFormat = (value) => { } } -export { sellingProductFormat } +const formatCurrency = (value) => { + if (value >= 1000) { + const thousands = Math.floor(value / 1000) // Menghitung ribuan + return `Rp${thousands}k` + } else { + return `Rp${value}` + } +} + +export { sellingProductFormat, formatCurrency} diff --git a/src/lib/product/components/ProductFilterDesktop.jsx b/src/lib/product/components/ProductFilterDesktop.jsx index 6118ed6b..e4a62abb 100644 --- a/src/lib/product/components/ProductFilterDesktop.jsx +++ b/src/lib/product/components/ProductFilterDesktop.jsx @@ -18,6 +18,7 @@ import { VStack } from '@chakra-ui/react' import Image from '@/core/components/elements/Image/Image' +import { formatCurrency } from '@/core/utils/formatValue' const ProductFilterDesktop = ({ brands, categories, prefixUrl, defaultBrand = null }) => { const router = useRouter() @@ -104,14 +105,7 @@ const ProductFilterDesktop = ({ brands, categories, prefixUrl, defaultBrand = nu router.push(`${prefixUrl}?${params}`) } - const formatCurrency = (value) => { - if (value >= 1000) { - const thousands = Math.floor(value / 1000) // Menghitung ribuan - return `Rp${thousands}k` - } else { - return `Rp${value}` - } - } + /*const handleIndexAccordion = async () => { if (brandValues) { diff --git a/src/lib/product/components/ProductSearch.jsx b/src/lib/product/components/ProductSearch.jsx index 9d59b305..bc69c9dd 100644 --- a/src/lib/product/components/ProductSearch.jsx +++ b/src/lib/product/components/ProductSearch.jsx @@ -15,8 +15,9 @@ import { useRouter } from 'next/router' import searchSpellApi from '@/core/api/searchSpellApi' import Link from '@/core/components/elements/Link/Link' import whatsappUrl from '@/core/utils/whatsappUrl' -import { Image } from '@chakra-ui/react' +import { HStack, Image, Tag, TagCloseButton, TagLabel } from '@chakra-ui/react' import odooApi from '@/core/api/odooApi' +import { formatCurrency } from '@/core/utils/formatValue' const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { const router = useRouter() @@ -32,6 +33,10 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { const [bannerPromotionFooter, setBannerPromotionFooter] = useState(null) const popup = useActive() const numRows = [30, 50, 80, 100] + const [brandValues, setBrand] = useState(query?.brand?.split(',') || []) + const [categoryValues, setCategory] = useState(query?.category?.split(',') || []) + const [priceFrom, setPriceFrom] = useState(query?.priceFrom || null) + const [priceTo, setPriceTo] = useState(query?.priceTo || null) const pageCount = Math.ceil(productSearch.data?.response.numFound / limit) const productStart = productSearch.data?.responseHeader.params.start @@ -98,7 +103,7 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { categories.push({ name, qty }) } } - + /*const categories = productSearch.data?.facetCounts?.facetFields?.categoryName?.filter( (value, index) => { if (index % 2 === 0) { @@ -166,12 +171,62 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { ) }, [spellings]) + const handleDeleteFilter = async (source, value) => { + let params = { + q: router.query.q, + orderBy: orderBy, + brand: brandValues.join(','), + category: categoryValues.join(','), + priceFrom, + priceTo + } + + let brands = brandValues + let catagories = categoryValues + switch (source) { + case 'brands': + brands = brandValues.filter((item) => item !== value) + params.brand = brands.join(',') + await setBrand(brands) + break + case 'category': + catagories = categoryValues.filter((item) => item !== value) + params.category = catagories.join(',') + await setCategory(catagories) + break + case 'price': + params.priceFrom = null + params.priceTo = null + break + case 'delete': + params = { + q: router.query.q, + orderBy: orderBy + } + break + } + + handleSubmitFilter(params) + } + const handleSubmitFilter = (params) => { + params = _.pickBy(params, _.identity) + params = toQuery(params) + router.push(`${prefixUrl}?${params}`) + } + return ( <> {productSearch.isLoading && }

Produk

+
{!spellings ? ( @@ -271,6 +326,13 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { )}

Hasil Pencarian

+
{!spellings ? ( @@ -388,3 +450,46 @@ const ProductSearch = ({ query, prefixUrl, defaultBrand = null }) => { } export default ProductSearch + +const FilterChoicesComponent = ({ + brandValues, + categoryValues, + priceFrom, + priceTo, + handleDeleteFilter +}) => ( +
+ + {brandValues.map((value, index) => ( + + {value} + handleDeleteFilter('brands', value)} /> + + ))} + {categoryValues.map((value, index) => ( + + {value} + handleDeleteFilter('category', value)} /> + + ))} + {priceFrom && priceTo && ( + + {formatCurrency(priceFrom) + '-' + formatCurrency(priceTo)} + handleDeleteFilter('price', priceFrom)} /> + + )} + {brandValues.length > 0 || categoryValues.length > 0 || priceFrom || priceTo ? ( + + + + ) : ( + '' + )} + +
+) -- cgit v1.2.3