summaryrefslogtreecommitdiff
path: root/src/lib/home/components/CategoryDynamic.jsx
blob: fa1df2862ea556d9cdac880e37dc6dc96e645046 (plain)
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
84
import React, { useEffect, useState } from 'react';
import useCategoryManagement from '../hooks/useCategoryManagement';
import NextImage from 'next/image';
import Link from "next/link";
import router from 'next/router';
import { createSlug } from '@/core/utils/slug';

const CategoryDynamic = () => {
  const { categoryManagement } = useCategoryManagement();

  const calculateLevel3Products = (category) => {
    return category.childFrontendIdI.reduce((total, child) => total + (child.numFound || 0), 0);
  };

  const calculateLevel2Products = (category) => {
    return category.categories.reduce((total, subCategory) => {
      const level3Products = calculateLevel3Products(subCategory);
      return total + (subCategory.numFound || 0) + level3Products;
    }, 0);
  };

  return (
    <div>
      {categoryManagement && categoryManagement.data?.map((category) => {
        const countLevel2 = calculateLevel2Products(category);

        return (
          <div key={category.id}>
            <div className='bagian-judul flex flex-row justify-start items-center gap-3 mb-4 mt-4'>
              <div className='font-semibold sm:text-h-lg mr-2'>{category.name}</div>
              <p className='text-gray_r-10 text-sm'>{countLevel2} Produk tersedia</p>
              <Link href={createSlug('/shop/category/', category?.name, category?.categoryIdI)} className="!text-red-500 font-semibold">Lihat Semua</Link>
            </div>
            <div className='grid grid-cols-3 gap-2'>
              {category.categories.map((subCategory) => {
                const countLevel3 = calculateLevel3Products(subCategory);

                return (
                  <div key={subCategory.id} className='border rounded justify-start items-start'>
                    <div className='p-3'>
                      <div className='flex flex-row border rounded mb-2 justify-start items-center'>
                        <NextImage
                          src={subCategory.image ? subCategory.image : "/images/noimage.jpeg"}
                          alt={subCategory.name}
                          width={90}
                          height={30}
                          className='object-fit'
                        />
                        <div className='bagian-judul flex flex-col justify-center items-start gap-2 ml-2'>
                          <div className='font-semibold text-lg mr-2'>{subCategory.name}</div>
                          <p className='text-gray_r-10 text-sm'>{(subCategory.numFound || 0) + countLevel3} Produk</p>
                          <Link href={createSlug('/shop/category/', subCategory?.name, subCategory?.idLevel2)} className="!text-red-500 font-semibold">Lihat Semua</Link>
                        </div>
                      </div>
                      <div className='grid grid-cols-2 gap-2 overflow-y-auto max-h-[240px]'>
                        {subCategory.childFrontendIdI.map((childCategory) => (
                          <div key={childCategory.id}>
                            <Link href={createSlug('/shop/category/', childCategory?.name, childCategory?.idLevel3)} className="flex flex-row gap-2 border rounded group hover:border-red-500">
                              <NextImage
                                src={childCategory.image ? childCategory.image : "/images/noimage.jpeg"}
                                alt={childCategory.name}
                                width={40}
                                height={40}
                              />
                              <div className='bagian-judul flex flex-col justify-center items-center gap-2 break-words line-clamp-2 group-hover:text-red-500'>
                                <div className='font-semibold line-clamp-2 group-hover:text-red-500 text-sm mr-2'>{childCategory.name}</div>
                              </div>
                            </Link>
                          </div>
                        ))}
                      </div>
                    </div>
                  </div>
                );
              })}
            </div>
          </div>
        );
      })}
    </div>
  );
};

export default CategoryDynamic;