diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-08-22 13:10:29 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-08-22 13:10:29 +0700 |
| commit | 5e2a8d315fa0bc58a18d4df5a995e402388995c9 (patch) | |
| tree | 09602b444795a4c85a5e5a422662939333466ced | |
| parent | 24cc59486efbe9caebfb10228283ee515afb5934 (diff) | |
<iman> update category dropdown
| -rw-r--r-- | src/lib/category/api/popularProduct.js | 3 | ||||
| -rw-r--r-- | src/lib/category/components/Category.jsx | 30 | ||||
| -rw-r--r-- | src/lib/category/components/PopularBrand.jsx | 45 | ||||
| -rw-r--r-- | src/styles/globals.css | 3 |
4 files changed, 36 insertions, 45 deletions
diff --git a/src/lib/category/api/popularProduct.js b/src/lib/category/api/popularProduct.js index 48f8a2a0..3fdfc41c 100644 --- a/src/lib/category/api/popularProduct.js +++ b/src/lib/category/api/popularProduct.js @@ -3,7 +3,7 @@ export const fetchPopulerProductSolr = async (category_id_ids) => { let sort ='sort=qty_sold_f desc'; try { const queryParams = new URLSearchParams({ q: category_id_ids }); - const response = await fetch(`/solr/product/select?${queryParams.toString()}&rows=2000&fl=manufacture_name_s,manufacture_id_i,id,display_name_s,qty_sold_f&${sort}`); + const response = await fetch(`/solr/product/select?${queryParams.toString()}&rows=2000&fl=manufacture_name_s,manufacture_id_i,id,display_name_s,qty_sold_f,qty_sold_f&${sort}`); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } @@ -24,6 +24,7 @@ export const fetchPopulerProductSolr = async (category_id_ids) => { name: promotion.display_name_s, manufacture_name: promotion.manufacture_name_s, manufacture_id: promotion.manufacture_id_i, + qty_sold: promotion.qty_sold_f, }; result.push(data); } diff --git a/src/lib/category/components/Category.jsx b/src/lib/category/components/Category.jsx index 374cdf78..f76e6e42 100644 --- a/src/lib/category/components/Category.jsx +++ b/src/lib/category/components/Category.jsx @@ -15,7 +15,6 @@ const Category = () => { const [banner, setBanner] = useState([]); const promotionProgram = useQuery('banner-promo-category-card', bannerApi({ type: 'banner-promo-category-card' })); - // const promotionProgram = useQuery('promotionProgram', bannerApi({ type: 'banner-promotion' })); useEffect(() => { const loadCategories = async () => { @@ -36,28 +35,6 @@ const Category = () => { } loadCategories() }, []) - - // useEffect(() => { - // const loadCategories = async () => { - // let dataCategories = await odooApi('GET', '/api/v1/category/tree') - // dataCategories = dataCategories?.map((category) => { - // category.childs = category.childs.map((child1Category) => { - // return { - // ...child1Category, - // isOpen: false - // } - // }) - // return { - // ...category, - // isOpen: false - // } - // }) - // setCategories(dataCategories) - // } - // loadCategories() - // }, []) - // console.log("categories",categories) - return ( <DesktopView> <div className='category-mega-box'> @@ -67,8 +44,8 @@ const Category = () => { href={createSlug('/shop/category/', category.name, category.id)} className='category-mega-box__parent flex items-center' > - <div className='w-6 h-6 border mr-2 rounded-full flex justify-center items-center'> - <Image src={category.image} alt='' width={16} height={16} /> + <div className='mr-2 flex justify-center items-center'> + <Image src={category.image} alt='' width={25} height={25} /> </div> {category.name} </Link> @@ -111,9 +88,8 @@ const Category = () => { </div> <div className='category-mega-box__child-wrapper !w-[260px] !flex !flex-col !gap-4'> <PopularBrand category={category} /> - {promotionProgram?.data[0]?.map((banner, index)=>( + {Array.isArray(promotionProgram?.data) && promotionProgram?.data.length > 0 && promotionProgram?.data[0]?.map((banner, index) => ( <div key={index} className='flex w-60 h-20 object-cover'> - {/* <Image src='https://erp.indoteknik.com/api/image/x_banner.banner/x_banner_image/397' alt='' width={275} height={4} /> */} <Image src={`${banner.image}`} alt={`${banner.name}`} width={275} height={4} /> </div> ))} diff --git a/src/lib/category/components/PopularBrand.jsx b/src/lib/category/components/PopularBrand.jsx index f0b12e2c..4777fded 100644 --- a/src/lib/category/components/PopularBrand.jsx +++ b/src/lib/category/components/PopularBrand.jsx @@ -18,28 +18,41 @@ const PopularBrand = ({ category }) => { const fetchTopBrands = async () => { try { const items = await fetchPopulerProductSolr(`category_id_ids:(${category?.categoryDataIds?.join(' OR ')})`); - // console.log("id",items) - // Fungsi untuk deduplikasi dan mengambil 12 nama brand teratas const getTop12UniqueBrands = (prod) => { - const brandSet = new Set(); - const topBrands = []; - + const brandMap = new Map(); + for (const product of prod) { - if (!brandSet.has(product.manufacture_name)) { - brandSet.add(product.manufacture_name); - topBrands.push({ name: product.manufacture_name, id: product.manufacture_id }); - }else{ + const { manufacture_name, manufacture_id, qty_sold } = product; + + if (brandMap.has(manufacture_name)) { + // Update the existing brand's qty_sold + brandMap.set(manufacture_name, { + name: manufacture_name, + id: manufacture_id, + qty_sold: brandMap.get(manufacture_name).qty_sold + qty_sold + }); + } else { + // Add a new brand to the map + brandMap.set(manufacture_name, { + name: manufacture_name, + id: manufacture_id, + qty_sold + }); } - if (topBrands.length === 18) break; } - return topBrands; - } - - // Menggunakan hasil pencarian produk + + // Convert the map to an array and sort by qty_sold in descending order + const sortedBrands = Array.from(brandMap.values()).sort((a, b) => b.qty_sold - a.qty_sold); + + // Return the top 12 brands + return sortedBrands.slice(0, 18); + }; + + // Using the fetched products const products = items; const top12UniqueBrands = getTop12UniqueBrands(products); - - // console.log('top12UniqueBrands', top12UniqueBrands); + + // Set the top 12 brands to the state setTopBrands(top12UniqueBrands); } catch (error) { console.error("Error fetching data from Solr", error); diff --git a/src/styles/globals.css b/src/styles/globals.css index 505dcab4..b3ca85f4 100644 --- a/src/styles/globals.css +++ b/src/styles/globals.css @@ -563,7 +563,8 @@ button { } .category-mega-box > div:hover .category-mega-box__parent { - @apply bg-gray_r-5; + @apply bg-gray_r-5 + w-full; } .category-mega-box > div:hover .category-mega-box__child-wrapper { |
