summaryrefslogtreecommitdiff
path: root/src/core/components/elements/Sidebar/Sidebar.jsx
blob: ddae3e2095b6d807edde5cce33358c81e7da7a55 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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, UserIcon } from '@heroicons/react/24/outline'
import { Fragment, useEffect, useState } from 'react'
import odooApi from '@/core/api/odooApi'
import { createSlug } from '@/core/utils/slug'
import Image from 'next/image'

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.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 (
    <>
      <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 h-full flex flex-col'>
                <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'
                      > 
                        <UserIcon className='h-6 w-6 text-gray-500' />
                      </Link>
                    </>
                  )}
                </div>
                <div className='overflow-y-auto flex-1'>
                  <SidebarLink className={itemClassName} href='/shop/promo'>
                    Semua Promo
                  </SidebarLink>
                  <SidebarLink className={itemClassName} href='/shop/brands'>
                    Semua Brand
                  </SidebarLink>
                  <SidebarLink
                    className={itemClassName}
                    href='https://blog.indoteknik.com/'
                    target='_blank'
                    rel='noreferrer noopener'
                  >
                    Blog Indoteknik
                  </SidebarLink>
                  {/* <SidebarLink className={itemClassName} href='/video'>
                    Indoteknik TV
                  </SidebarLink> */}
                  <SidebarLink className={itemClassName} href='/tentang-kami'>
                    Tentang Indoteknik
                  </SidebarLink>
                  <SidebarLink className={itemClassName} href='/contact-us'>
                    Hubungi Kami
                  </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={createSlug('/shop/category/', category.name, category.id)}
                            className='flex-1 font-normal !text-gray_r-11 py-4 flex items-center flex-row'
                          >
                            <div className='mr-2  flex justify-center items-center'>
                              <Image src={category.image} alt='' width={25} height={25} />
                            </div>
                            {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={createSlug('/shop/category/', child1Category.name, child1Category.id)}
                                  className='flex-1 font-normal !text-gray_r-11 flex flex-row items-center'
                                >
                                  <div className='mr-2  flex justify-center items-center'>
                                    <Image src={`https://erp.indoteknik.com/api/image/product.public.category/image_1920/${child1Category.id}`} alt='' width={25} height={25} />
                                  </div>
                                  {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={createSlug('/shop/category/', child2Category.name, child2Category.id)}
                                    className='flex w-full font-normal !text-gray_r-11 border-b border-gray_r-6 p-4 pl-16 flex-row'
                                  >
                                    <div className='mr-2  flex justify-center items-center'>
                                      <Image src={`https://erp.indoteknik.com/api/image/product.public.category/image_1920/${child2Category.id}`} alt='' width={25} height={25} />
                                    </div>
                                    {child2Category.name}
                                  </Link>
                                ))}
                            </Fragment>
                          ))}
                      </Fragment>
                    ))}
                </div>
              </div>
            </motion.div>
          </>
        )}
      </AnimatePresence>
    </>
  )
}

export default Sidebar