From c6b0402fb0ba7d56b1e060d36e98d934661d43d4 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 24 Feb 2023 08:54:32 +0700 Subject: fix --- src/core/components/elements/Sidebar/Sidebar.jsx | 116 ++++++++++++++++++++++- 1 file changed, 114 insertions(+), 2 deletions(-) (limited to 'src/core/components/elements') diff --git a/src/core/components/elements/Sidebar/Sidebar.jsx b/src/core/components/elements/Sidebar/Sidebar.jsx index 22fda06b..c39b5e34 100644 --- a/src/core/components/elements/Sidebar/Sidebar.jsx +++ b/src/core/components/elements/Sidebar/Sidebar.jsx @@ -2,7 +2,9 @@ import Link from '../Link/Link' import greeting from '@/core/utils/greeting' import useAuth from '@/core/hooks/useAuth' import { AnimatePresence, motion } from 'framer-motion' -import { CogIcon } from '@heroicons/react/24/outline' +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() @@ -19,6 +21,47 @@ const Sidebar = ({ active, close }) => { const itemClassName = 'px-4 py-3 block !text-gray_r-12/80 font-normal' const transition = { ease: 'linear', duration: 0.2 } + 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 ( <> @@ -95,7 +138,76 @@ const Sidebar = ({ active, close }) => { > Pusat Bantuan - + + {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} + + ))} +
+ ))} +
+ ))} -- cgit v1.2.3