import Link from '../Link/Link' import greeting from '@/core/utils/greeting' import useAuth from '@/core/hooks/useAuth' import { AnimatePresence, motion } from 'framer-motion' import { ChevronDownIcon, ChevronUpIcon, CogIcon } from '@heroicons/react/24/outline' import { Fragment, useEffect, useState } from 'react' import odooApi from '@/core/api/odooApi' const Sidebar = ({ active, close }) => { const auth = useAuth() const SidebarLink = ({ children, ...props }) => ( {children} ) const itemClassName = 'px-4 py-3 block !text-gray_r-12/80 font-normal' const transition = { ease: 'linear', duration: 0.1 } const [isOpenCategory, setOpenCategory] = useState(false) const [categories, setCategories] = useState([]) useEffect(() => { const loadCategories = async () => { if (isOpenCategory && categories.length == 0) { 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() }, [isOpenCategory, categories]) const toggleCategories = (id = 0) => { let newCategories = categories.map((category) => { category.childs = category.childs.map((child1Category) => { return { ...child1Category, isOpen: id == child1Category.id ? !child1Category.isOpen : child1Category.isOpen } }) return { ...category, isOpen: id == category.id ? !category.isOpen : category.isOpen } }) setCategories(newCategories) } return ( <> {active && ( <> {!auth && ( <> Daftar Masuk > )} {auth && ( <> {greeting()}, {auth?.name} > )} Semua Brand Tentang Indoteknik F.A.Q Hubungi Kami setOpenCategory(!isOpenCategory)} > Kategori {!isOpenCategory && } {isOpenCategory && } {isOpenCategory && categories.map((category) => ( {category.name} toggleCategories(category.id)} > {!category.isOpen && } {category.isOpen && } {category.isOpen && category.childs.map((child1Category) => ( {child1Category.name} {child1Category.childs.length > 0 && ( toggleCategories(child1Category.id)} > {!child1Category.isOpen && ( )} {child1Category.isOpen && ( )} )} {child1Category.isOpen && child1Category.childs.map((child2Category) => ( {child2Category.name} ))} ))} ))} > )} > ) } export default Sidebar