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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
import React, { useEffect, useState, useCallback } from 'react';
import NextImage from 'next/image';
import Link from 'next/link';
import { createSlug } from '@/core/utils/slug';
import { Swiper, SwiperSlide } from 'swiper/react';
import 'swiper/css';
import {
fetchCategoryManagementSolr,
fetchCategoryManagementVersion,
} from '../api/categoryManagementApi';
const CategoryDynamicMobile = () => {
const [selectedCategory, setSelectedCategory] = useState({});
const [categoryManagement, setCategoryManagement] = useState([]);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
const fetchCategoryData = async () => {
setIsLoading(true);
const res = await fetch('/api/category-management');
const { data } = await res.json();
if (data) {
setCategoryManagement(data);
}
setIsLoading(false);
};
fetchCategoryData();
}, []);
useEffect(() => {
if (categoryManagement?.length > 0) {
const initialSelections = categoryManagement.reduce((acc, category) => {
if (category.categories.length > 0) {
acc[category.id] = category.categories[0].id_level_2;
}
return acc;
}, {});
setSelectedCategory(initialSelections);
}
}, [categoryManagement]);
const handleCategoryLevel2Click = (categoryId, idLevel2) => {
setSelectedCategory((prev) => ({
...prev,
[categoryId]: idLevel2,
}));
};
return (
<div className='p-4'>
{categoryManagement &&
categoryManagement?.map((category) => (
<div key={category.id}>
<div className='bagian-judul flex flex-row justify-between items-center gap-3 mb-4 mt-4'>
<h1 className='font-semibold text-[14px] sm:text-h-lg mr-2'>
{category.name}
</h1>
<Link
href={createSlug(
'/shop/category/',
category?.name,
category?.category_id
)}
className='!text-red-500 font-semibold text-sm'
>
Lihat Semua
</Link>
</div>
<Swiper slidesPerView={2.3} spaceBetween={10}>
{category.categories.map((index) => (
<SwiperSlide key={index.id}>
<div
onClick={() =>
handleCategoryLevel2Click(category.id, index?.id_level_2)
}
className={`border flex justify-start items-center max-w-48 max-h-16 rounded ${
selectedCategory[category.id] === index?.id_level_2
? 'bg-red-50 border-red-500 text-red-500'
: 'border-gray-200 text-gray-900'
}`}
>
<div className='p-1 flex justify-start items-center'>
<div className='flex flex-row justify-center items-center'>
<NextImage
src={
index.image ? index.image : '/images/noimage.jpeg'
}
alt={index.name}
width={30}
height={30}
className=''
loading='eager'
/>
<div className='bagian-judul flex flex-col justify-center items-start gap-1 ml-2'>
<h2 className='font-semibold text-[10px] line-clamp-1'>
{index?.name}
</h2>
</div>
</div>
</div>
</div>
</SwiperSlide>
))}
</Swiper>
<div className='p-3 mt-2 border'>
<div className='grid grid-cols-2 gap-2 overflow-y-auto max-h-[240px]'>
{category.categories.map(
(index) =>
selectedCategory[category.id] === index?.id_level_2 &&
index?.child_frontend_id_i.map((x) => (
<div key={x.id}>
<Link
href={createSlug(
'/shop/category/',
x?.name,
x?.id_level_3
)}
className='flex flex-row gap-1 border rounded group hover:border-red-500'
>
<NextImage
src={x.image ? x.image : '/images/noimage.jpeg'}
alt={x.name}
width={40}
height={40}
className='p-2'
loading='eager'
/>
<div className='bagian-judul flex flex-col justify-center items-start gap-1 break-words line-clamp-2 group-hover:text-red-500'>
<h3 className='font-semibold line-clamp-2 group-hover:text-red-500 text-[10px]'>
{x?.name}
</h3>
</div>
</Link>
</div>
))
)}
</div>
</div>
</div>
))}
</div>
);
};
export default CategoryDynamicMobile;
|