summaryrefslogtreecommitdiff
path: root/src/lib/promotinProgram/components/PromotionType.jsx
blob: 51f2622a1515dcad914b15d97ab71d00ea7d029a (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import React, { useEffect, useState } from 'react'
import Image from '@/core/components/elements/Image/Image'
import BottomPopup from '@/core/components/elements/Popup/BottomPopup'
import CountDown2 from '@/core/components/elements/CountDown/CountDown2'
import currencyFormat from '@/core/utils/currencyFormat'
import { getPromotionProgram } from '../api/homepageApi'

const PromotionType = ({
  isModal = false,
  variantId,
  setPromotionActiveId,
  promotionActiveId,
  quantity,
  product = null,
  setProducts = null
}) => {
  const [selectedPromo, setSelectedPromo] = useState(null)
  const [promotionType, setPromotionType] = useState(false)
  const [promos, setPromotionList] = useState(null)
  const [activeTitle, setActiveTitle] = useState(null)
  const [quantitySet, setQuantity] = useState(null)

  useEffect(() => {
    const id = variantId
    const listProgram = async () => {
      const programs = await getPromotionProgram({ id })
      if (programs?.length > 0) {
        setPromotionList(programs)
        setActiveTitle(programs?.[0].type.value)
      }
    }
    listProgram()
    setSelectedPromo(promotionActiveId)
    if (product) {
      const variant = product.variants.find((variant) => variant.id === variantId)
      setQuantity(variant.quantity)
    }else{
      setQuantity(quantity)
    }
  }, [])

  const groupingData = promos?.reduce((groups, item) => {
    const promoType = item.type.value
    if (!groups[promoType]) {
      groups[promoType] = []
    }
    groups[promoType].push(item)

    return groups
  }, {})

  const handlePromoClick = (promoId, minQty) => {
    if (quantitySet >= minQty) {
      if (promoId == selectedPromo) {
        setSelectedPromo(null)
        setPromotionActiveId(null)
        if (product) {
          const updateProdcuts = () => {
            let variantIndex = product.variants.findIndex((varian) => varian.id == variantId)
            product.variants[variantIndex].programActive = null

            setProducts(product)
          }
          updateProdcuts()
        }
      } else {
        setSelectedPromo(promoId)
        setPromotionActiveId(promoId)
        if (product) {
          const updateProdcuts = () => {
            let variantIndex = product.variants.findIndex((varian) => varian.id == variantId)
            product.variants[variantIndex].programActive = promoId

            setProducts(product)
          }
          updateProdcuts()
        }
      }
    }
  }

  const handlePopUp = () => {
    if (isModal == false) {
      setPromotionType(true)
    }
  }

  return (
    promos && (
      <>
        <div className='h-[50%] relative'>
          <div className='relative rounded-lg border border-solid border-gray-300 mb-2 w-full'>
            <Image src='https://placehold.co/537x50.png' alt='' layout='fill' objectFit='cover' />
            <div className='h-full absolute top-0 left-0 w-full flex items-center justify-between p-2'>
              <span className='font-semibold text-lg text-white'>Promo Tersedia</span>
              <button type='button' onClick={() => handlePopUp()} className='py-2 btn-yellow'>
                Lihat Semua
              </button>
            </div>
          </div>
          <div
            className={`w-full ${
              isModal == true ? '' : 'grid grid-cols-3 gap-1 bg-gray-200 '
            }  p-2 rounded-lg`}
          >
            {isModal === true ? (
              <div>
                <div className='flex gap-2 mb-3'>
                  {Object.keys(groupingData).map((index) => {
                    return (
                      <>
                        <button
                          onClick={() => setActiveTitle(index)}
                          className={`py-1 px-2 rounded-lg flex justify-center items-center text-sm ${
                            activeTitle === index ? 'badge-yellow  text-black' : ''
                          } `}
                        >
                          {groupingData[index][0].type.label}
                        </button>
                      </>
                    )
                  })}
                </div>
                {activeTitle &&
                  groupingData[activeTitle].map((item, i) => (
                    <div
                      key={i}
                      onClick={() => handlePromoClick(item.id, item.minimumPurchaseQty)}
                      className={`border border-solid mb-5 w-full hover:cursor-pointer ${
                        selectedPromo
                          ? selectedPromo === item.id
                            ? 'opacity-100 border-red-500 bg-red-100'
                            : 'opacity-50  pointer-events-none'
                          : 'opacity-100'
                      } ${
                        quantitySet >= item.minimumPurchaseQty
                          ? ''
                          : 'opacity-50  pointer-events-none'
                      } `}
                    >
                      <div className={`flex`}>
                        <div className=''>
                          <Image
                            src={item.Image}
                            alt={item.name}
                            className={`flex-1 w-[170px] object-cover`}
                          />
                        </div>
                        <div className='p-2 w-full'>
                          <div className='flex justify-between mb-1'>
                            <div className='text-danger-500 font-semibold mb-1 mt-1'>
                              Waktu Tersisa
                            </div>
                            <div>
                              <CountDown2 initialTime={item.remainingTime}></CountDown2>
                            </div>
                          </div>
                          <p className='text-justify text-gray-500 line-clamp-3'>{item.name}</p>
                          <div className='mt-4'>
                            {/* {item.type.value === 'bundling' && (
                              <> */}
                                <div className='flex gap-x-2 mt-3   justify-between items-center'>
                                  <div className='flex gap-x-2 items-center '>
                                    <div className='text-gray_r-11 line-through text-caption-1 mt-1'>
                                      {currencyFormat(item.totalSavings)}
                                    </div>
                                    <div className='text-danger-500 font-semibold '>Gratis</div>
                                  </div>
                                  <div className='text-danger-500 font-semibold '>
                                    {quantitySet < item.minimumPurchaseQty
                                      ? 'Tambah ' +
                                        (parseInt(item.minimumPurchaseQty) -
                                          parseInt(quantitySet)) +
                                        ' lagi'
                                      : ''}
                                  </div>
                                </div>
                              {/* </>
                            )} */}
                            {/* {item.type.value === 'special_price' && (
                              <>
                                <div className='flex gap-x-2 mt-3 items-center'>
                                  <div className='text-danger-500 font-semibold '> {currencyFormat(item.totalSavings)}</div>
                                </div>
                              </>
                            )}
                            {item.type.value === 'discount_loading' && (
                              <>
                                <div className='flex justify-between'>
                                  <div className='text-danger-500 font-semibold '>
                                    {currencyFormat(item.totalSavings)}
                                  </div>
                                  <div className='text-danger-500 font-semibold '>
                                    {quantitySet < item.minimumPurchaseQty
                                      ? 'Tambah ' +
                                        (parseInt(item.minimumPurchaseQty) -
                                          parseInt(quantitySet)) +
                                        ' lagi'
                                      : ''}
                                  </div>
                                </div>
                              </>
                            )} */}
                          </div>
                        </div>
                      </div>
                    </div>
                  ))}
              </div>
            ) : (
              promos.map((promo, index) => {
                if (index > 2) {
                  return null
                } else {
                  if (index === 2 && promos.length > 2) {
                    return (
                      <>
                        <div
                          onClick={() => setPromotionType(true)}
                          className={` border border-solid bg-white mb-5 w-full hover:cursor-pointer opacity-100 flex flex-col justify-center items-center`}
                        >
                          <div className='flex justify-center items-center'>
                            <div className='rounded-full shadow-lg w-10 h-10 flex justify-center items-center'>
                              <svg
                                aria-hidden='true'
                                fill='none'
                                stroke='currentColor'
                                stroke-width='1.5'
                                viewBox='0 0 24 24'
                                className='text-red-500  w-20 h-20'
                              >
                                <path
                                  d='M12 6v12m6-6H6'
                                  stroke-linecap='round'
                                  stroke-linejoin='round'
                                ></path>
                              </svg>
                            </div>
                          </div>

                          <span className='mt-2 text-sm'>Lihat Promo Lainya</span>
                        </div>
                      </>
                    )
                  }
                  return (
                    <>
                      <div
                        key={promo.id}
                        onClick={() => setPromotionType(true)}
                        className={`border border-solid bg-white  mb-5 w-full hover:cursor-pointer`}
                      >
                        <div className={`items-center`}>
                          <div className=''>
                            <Image
                              src={promo.Image}
                              alt={promo.name}
                              className={`flex-1 w-full object-cover`}
                            />
                          </div>
                          <div className='p-2'>
                            <div className='badge-yellow  text-black mb-1'>{promo.type.label}</div>
                            <p className='text-justify line-clamp-2'>{promo.name}</p>
                            <div className='text-danger-500 font-semibold mb-1 mt-1'>
                              {/* {currencyFormat(promo.totalSavings)} */}
                            </div>
                            {/* <div className='w-full bg-yellow-200 rounded-full h-1.5 mb-2'>
                              <div className='bg-yellow-500 h-1.5 rounded-full w-[45%]'></div>
                            </div> */}
                            <div>
                              <CountDown2 initialTime={promo.remainingTime}></CountDown2>
                            </div>
                          </div>
                        </div>
                      </div>
                    </>
                  )
                }
              })
            )}
            <BottomPopup
              className=' !h-[75%]'
              title='Pakai Promo'
              active={promotionType}
              close={() => setPromotionType(false)}
            >
              <div className='flex mt-4'>
                <PromotionType
                  isModal={true}
                  variantId={variantId}
                  setPromotionActiveId={setPromotionActiveId}
                  promotionActiveId={promotionActiveId}
                  quantity={quantitySet}
                ></PromotionType>
              </div>
            </BottomPopup>
          </div>
        </div>
      </>
    )
  )
}

export default PromotionType