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} from '../api/categoryManagementApi' const CategoryDynamicMobile = () => { const [selectedCategory, setSelectedCategory] = useState({}); const [categoryManagement, setCategoryManagement] = useState([]) const [isLoading, setIsLoading] = useState(false) const loadBrand = useCallback(async () => { setIsLoading(true) const items = await fetchCategoryManagementSolr(); setIsLoading(false) setCategoryManagement(items) }, []) useEffect(() => { loadBrand() }, [loadBrand]) useEffect(() => { const loadPromo = async () => { try { 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); } } catch (loadError) { // console.error("Error loading promo items:", loadError); } }; loadPromo(); }, [categoryManagement]); const handleCategoryLevel2Click = (categoryIdI, idLevel2) => { setSelectedCategory(prev => ({ ...prev, [categoryIdI]: idLevel2 })); }; return (
{categoryManagement && categoryManagement?.map((category) => (
{category?.name}
Lihat Semua
{category.categories.map((index) => (
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'}`} >
{index?.name}
))}
{category.categories.map((index) => ( selectedCategory[category.id] === index?.id_level_2 && index?.child_frontend_id_i.map((x) => (
{x?.name}
)) ))}
))}
); }; export default CategoryDynamicMobile;