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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
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 }) => (
<Link
{...props}
onClick={close}
>
{children}
</Link>
)
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 (
<>
<AnimatePresence>
{active && (
<>
<motion.div
className='overlay z-50'
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={transition}
onClick={close}
/>
<motion.div
className='fixed z-[55] top-0 h-full w-[80%] bg-white'
initial={{ left: '-80%' }}
animate={{ left: 0 }}
exit={{ left: '-80%' }}
transition={transition}
>
<div className='divide-y divide-gray_r-6'>
<div className='p-4 flex gap-x-3'>
{!auth && (
<>
<Link
onClick={close}
href='/register'
className='btn-yellow !text-gray_r-12 py-2 flex-1'
>
Daftar
</Link>
<Link
onClick={close}
href='/login'
className='btn-solid-red !text-gray_r-1 py-2 flex-1'
>
Masuk
</Link>
</>
)}
{auth && (
<>
<div className='text-caption-2 text-gray_r-11'>
{greeting()},
<span className='text-body-2 text-gray_r-12 block mt-1 font-medium'>
{auth?.name}
</span>
</div>
<Link
onClick={close}
href='/my/menu'
className='!text-gray_r-11 ml-auto my-auto'
>
<CogIcon className='w-6' />
</Link>
</>
)}
</div>
<SidebarLink
className={itemClassName}
href='/shop/brands'
>
Semua Brand
</SidebarLink>
<SidebarLink
className={itemClassName}
href='/'
>
Tentang Indoteknik
</SidebarLink>
<SidebarLink
className={itemClassName}
href='/'
>
Pusat Bantuan
</SidebarLink>
<button
className={`${itemClassName} w-full text-left flex`}
onClick={() => setOpenCategory(!isOpenCategory)}
>
Kategori
<div className='ml-auto'>
{!isOpenCategory && <ChevronDownIcon className='text-gray_r-12 w-5' />}
{isOpenCategory && <ChevronUpIcon className='text-gray_r-12 w-5' />}
</div>
</button>
{isOpenCategory &&
categories.map((category) => (
<Fragment key={category.id}>
<div className='flex w-full text-gray_r-11 border-b border-gray_r-6 px-4 pl-8 items-center'>
<Link
href={`/shop/search?category=${category.name}`}
className='flex-1 font-normal !text-gray_r-11 py-4'
>
{category.name}
</Link>
<div
className='ml-4 h-full py-4'
onClick={() => toggleCategories(category.id)}
>
{!category.isOpen && <ChevronDownIcon className='text-gray_r-11 w-5' />}
{category.isOpen && <ChevronUpIcon className='text-gray_r-11 w-5' />}
</div>
</div>
{category.isOpen &&
category.childs.map((child1Category) => (
<Fragment key={child1Category.id}>
<div
className={`flex w-full !text-gray_r-11 border-b border-gray_r-6 p-4 pl-12 ${
category.isOpen ? 'bg-gray_r-2' : ''
}`}
>
<Link
href={`/shop/search?category=${child1Category.name}`}
className='flex-1 font-normal !text-gray_r-11'
>
{child1Category.name}
</Link>
{child1Category.childs.length > 0 && (
<div
className='ml-4 h-full'
onClick={() => toggleCategories(child1Category.id)}
>
{!child1Category.isOpen && (
<ChevronDownIcon className='text-gray_r-11 w-5' />
)}
{child1Category.isOpen && (
<ChevronUpIcon className='text-gray_r-11 w-5' />
)}
</div>
)}
</div>
{child1Category.isOpen &&
child1Category.childs.map((child2Category) => (
<Link
key={child2Category.id}
href={`/shop/search?category=${child2Category.name}`}
className='flex w-full font-normal !text-gray_r-11 border-b border-gray_r-6 p-4 pl-16'
>
{child2Category.name}
</Link>
))}
</Fragment>
))}
</Fragment>
))}
</div>
</motion.div>
</>
)}
</AnimatePresence>
</>
)
}
export default Sidebar
|