1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
import odooApi from '@/core/api/odooApi'
import React, { useEffect, useState } from 'react'
import axios from 'axios';
import { useQuery } from 'react-query'
import Link from '@/core/components/elements/Link/Link'
import { createSlug } from '@/core/utils/slug'
import Image from 'next/image'
import { ChevronRightIcon } from '@heroicons/react/24/outline'
import useProductSearch from '../../../lib/product/hooks/useProductSearch';
import { SolrResponse } from "~/types/solr";
import { fetchPromoItemsSolr } from '../api/popularProduct'
const SOLR_HOST = process.env.SOLR_HOST
const PopularBrand = ({ category }) => {
const [topBrands, setTopBrands] = useState([]);
const fetchTopBrands = async () => {
try {
const items = await fetchPromoItemsSolr(`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 = [];
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{
}
if (topBrands.length === 18) break;
}
return topBrands;
}
// Menggunakan hasil pencarian produk
const products = items;
const top12UniqueBrands = getTop12UniqueBrands(products);
// console.log('top12UniqueBrands', top12UniqueBrands);
setTopBrands(top12UniqueBrands);
} catch (error) {
console.error("Error fetching data from Solr", error);
throw error;
}
}
useEffect(() => {
fetchTopBrands();
}, [category]);
return (
<div className='flex flex-col'>
<div className='grid grid-cols-3 max-h-full w-full gap-2'>
{topBrands.map((brand, index) => (
<div key={index} className='w-full flex items-center justify-center pb-2'>
<Link
href={createSlug('/shop/brands/', brand.name, brand.id)}
className='category-mega-box__child-one w-8 h-full flex items-center justify-center '
>
<Image src={`https://erp.indoteknik.com/api/image/x_manufactures/x_logo_manufacture/${brand.id}` } alt={`${brand.name}`} width={104} height={44} objectFit='cover' />
</Link>
</div>
))}
</div>
{/* {topBrands.length > 8 && (
<div className='flex hover:bg-gray_r-8/35 rounded-10'>
<Link
href={createSlug('/shop/category/', category.name, category.id)}
className='category-mega-box__child-one flex items-center gap-4 font-bold hover:ml-4'
>
<p className='mt-2 mb-0 text-danger-500 font-semibold'>Lihat Semua Brand</p>
<ChevronRightIcon className='w-4 text-danger-500 font-bold' />
</Link>
</div>
)} */}
</div>
)
}
export default PopularBrand;
|