summaryrefslogtreecommitdiff
path: root/src/lib/product/components/ProductCard.jsx
blob: 35e2a66570c82631a74f54285b2efaa842bde97c (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import clsx from 'clsx';
import ImageNext from 'next/image';
import { useRouter } from 'next/router';
import { useMemo, useEffect, useState } from 'react';

import Image from '@/core/components/elements/Image/Image';
import Link from '@/core/components/elements/Link/Link';
import currencyFormat from '@/core/utils/currencyFormat';
import { sellingProductFormat } from '@/core/utils/formatValue';
import { createSlug } from '@/core/utils/slug';
import whatsappUrl from '@/core/utils/whatsappUrl';
import useUtmSource from '~/hooks/useUtmSource';

const ProductCard = ({ product, simpleTitle, variant = 'vertical' }) => {
  const router = useRouter();
  const utmSource = useUtmSource();
  const [discount, setDiscount] = useState(0);

  let voucherPastiHemat = 0;
  
  if (product?.voucherPastiHemat ? product?.voucherPastiHemat.length : voucherPastiHemat > 0) {
    const stringVoucher = product?.voucherPastiHemat[0];
    const validJsonString = stringVoucher.replace(/'/g, '"');
    voucherPastiHemat = JSON.parse(validJsonString);
  }

  const callForPriceWhatsapp = whatsappUrl('product', {
    name: product.name,
    manufacture: product.manufacture?.name,
    url: createSlug('/shop/product/', product.name, product.id, true),
  });

  const image = useMemo(() => {
    if (product.image) return product.image + '?ratio=square';
    return '/images/noimage.jpeg';
  }, [product.image]);

  const URL = {
    product:
      createSlug('/shop/product/', product?.name, product?.id) +
      `?utm_source=${utmSource}`,
    manufacture: createSlug(
      '/shop/brands/',
      product?.manufacture?.name,
      product?.manufacture.id
    ),
  };

  const hitungDiscountVoucher = () => {
    let countDiscount = 0;
    if (voucherPastiHemat.discount_type === 'percentage') {
      countDiscount =
        product?.lowestPrice.priceDiscount *
        (voucherPastiHemat.discount_amount / 100);
      if (
        voucherPastiHemat.max_discount > 0 &&
        countDiscount > voucherPastiHemat.max_discount
      ) {
        countDiscount = voucherPastiHemat.max_discount;
      }
    } else {
      countDiscount = voucherPastiHemat.discount_amount;
    }

    setDiscount(countDiscount);
  };

  useEffect(() => {
    hitungDiscountVoucher();
  }, []);

  if (variant == 'vertical') {
    return (
      <div className='rounded shadow-sm border border-gray_r-4 bg-white h-[330px] md:h-[380px]'>
        <Link href={URL.product} className='border-b border-gray_r-4 relative'>
          <div className='relative'>
            <Image
              src={image}
              alt={product?.name}
              className='gambarA w-full object-contain object-center h-36 sm:h-48'
            />
            <div className='absolute top-0 right-0 flex  mt-3'>
              <div className='gambarB '>
                {product?.isSni && (
                  <ImageNext
                    src='/images/sni-logo.png'
                    alt='SNI Logo'
                    className='w-4 h-5 object-contain object-top   sm:h-6'
                    width={50}
                    height={50}
                  />
                )}
              </div>
              <div className='gambarC  '>
                {product?.isTkdn && (
                  <ImageNext
                    src='/images/TKDN.png'
                    alt='TKDN'
                    className='w-11 h-6 object-contain object-top  ml-1 mr-1 sm:h-6'
                    width={50}
                    height={50}
                  />
                )}
              </div>
            </div>
          </div>

          {router.pathname != '/' && product?.flashSale?.id > 0 && (
            <div className='absolute bottom-0 w-full grid'>
              <div className='absolute bottom-0 w-full h-full'>
                <ImageNext
                  src='/images/BG-FLASH-SALE.jpg'
                  className='h-full'
                  width={1000}
                  height={100}
                />
              </div>
              <div className='relative'>
                <div className='flex gap-x-1 items-center p-2 justify-center'>
                  <div className='bg-yellow-400 rounded-lg p-1 h-6 w-19 flex items-center justify-center '>
                    <span className='text-sm font-bold text-black'>
                      {Math.floor(product?.lowestPrice.discountPercentage)}%
                    </span>
                  </div>
                  <div className='bg-red-600 border border-solid border-yellow-400 p-2 rounded-full h-6 flex w-fit items-center justify-center gap-x-2'>
                    <ImageNext
                      src='/images/ICON_FLASH_SALE_WEBSITE_INDOTEKNIK.svg'
                      width={13}
                      height={5}
                    />
                    <span className='text-white text-[9px] md:text-[10px] font-semibold'>
                      {product?.flashSale?.tag != 'false' ||
                      product?.flashSale?.tag
                        ? product?.flashSale?.tag
                        : 'FLASH SALE'}
                    </span>
                  </div>
                </div>
              </div>
            </div>
          )}
          {product.variantTotal > 1 && (
            <div className='absolute badge-gray bottom-1.5 left-1.5'>
              {product.variantTotal} Varian
            </div>
          )}
        </Link>
        <div className='p-2 sm:p-3 pb-3 text-caption-2 sm:text-body-2 leading-5'>
          {product?.manufacture?.name ? (
            <Link href={URL.manufacture} className='mb-1'>
              {product.manufacture.name}
            </Link>
          ) : (
            <div>-</div>
          )}
          <Link
            href={URL.product}
            className={`mb-2 !text-gray_r-12 leading-6 block line-clamp-3 h-[64px]`}
            title={product?.name}
          >
            {product?.name}
          </Link>
          {product?.flashSale?.id > 0 &&
          product?.lowestPrice.discountPercentage > 0 ? (
            <>
              <div className='flex gap-x-1 mb-1 items-center'>
                <div className='text-gray_r-11 line-through text-[11px] sm:text-caption-2'>
                  {currencyFormat(product.lowestPrice.price)}
                </div>
                <div className='badge-solid-red'>
                  {Math.floor(product?.lowestPrice.discountPercentage)}%
                </div>
              </div>
              <div className='text-danger-500 font-semibold mb-2'>
                {product?.lowestPrice.priceDiscount > 0 ? (
                  currencyFormat(product?.lowestPrice.priceDiscount)
                ) : (
                  <a
                    rel='noopener noreferrer'
                    target='_blank'
                    href={callForPriceWhatsapp}
                  >
                    Call for Inquiry
                  </a>
                )}
              </div>
            </>
          ) : (
            <div className='text-danger-500 font-semibold mb-2 min-h-[40px]'>
              {product?.lowestPrice.price > 0 ? (
                <>
                  {currencyFormat(product?.lowestPrice.price)}
                  <div className='text-gray_r-9 text-[10px] font-normal mt-2'>
                    Inc. PPN:{' '}
                    {currencyFormat(
                      product.lowestPrice.price * process.env.NEXT_PUBLIC_PPN
                    )}
                  </div>
                </>
              ) : (
                <a
                  rel='noopener noreferrer'
                  target='_blank'
                  href={callForPriceWhatsapp}
                >
                  Call for Inquiry
                </a>
              )}
            </div>
          )}
          {discount > 0 && product?.flashSale?.id < 1 && (
            <div className='flex gap-x-1 mb-1 text-sm'>
              <div className='inline-flex items-center rounded-md bg-green-50 px-2 py-1 text-xs font-medium text-green-700 ring-1 ring-inset ring-green-600/20'>
                Voucher : {currencyFormat(discount)}
              </div>
            </div>
          )}

          <div className='flex w-full items-center gap-x-1 '>
            {product?.stockTotal > 0 && (
              <div className='badge-solid-red'>Ready Stock</div>
            )}
            {/* <div className='badge-gray'>{product?.stockTotal > 5 ? '> 5' : '< 5'}</div> */}
            {product?.qtySold > 0 && (
              <div className='text-gray_r-9 text-[11px]'>
                {sellingProductFormat(product?.qtySold) + ' Terjual'}
              </div>
            )}
          </div>
        </div>
      </div>
    );
  }

  if (variant == 'horizontal') {
    return (
      <div className='flex bg-white'>
        <div className='w-4/12'>
          <Link href={URL.product} className='relative'>
            <div className='relative'>
              <Image
                src={image}
                alt={product?.name}
                className='gambarA w-full object-contain object-center h-36 sm:h-48'
              />
              <div className='absolute top-0 right-0 flex  mt-3'>
                <div className='gambarB '>
                  {product?.isSni && (
                    <ImageNext
                      src='/images/sni-logo.png'
                      alt='SNI Logo'
                      className='w-4 h-5 object-contain object-top   sm:h-6'
                      width={50}
                      height={50}
                    />
                  )}
                </div>
                <div className='gambarC  '>
                  {product?.isTkdn && (
                    <ImageNext
                      src='/images/TKDN.png'
                      alt='TKDN'
                      className='w-11 h-6 object-contain object-top  ml-1 sm:h-6'
                      width={50}
                      height={50}
                    />
                  )}
                </div>
              </div>
            </div>
            {product.variantTotal > 1 && (
              <div className='absolute badge-gray bottom-1.5 left-1.5'>
                {product.variantTotal} Varian
              </div>
            )}
          </Link>
        </div>
        <div className='w-8/12 p-2'>
          {product.flashSale.id > 0 && (
            <div className='bg-red-600 rounded-full mb-1 p-2 pl-3 pr-3 flex w-fit items-center gap-x-1'>
              <ImageNext
                src='/images/ICON_FLASH_SALE_WEBSITE_INDOTEKNIK.svg'
                width={15}
                height={10}
              />
              <span className='text-white text-xs font-semibold'>
                {' '}
                {product?.flashSale?.tag != 'false' || product?.flashSale?.tag
                  ? product?.flashSale?.tag
                  : 'FLASH SALE'}
              </span>
            </div>
          )}
          {product?.manufacture?.name ? (
            <Link href={URL.manufacture} className='mb-1'>
              {product.manufacture.name}
            </Link>
          ) : (
            <div>-</div>
          )}
          <Link
            href={URL.product}
            className={`mb-3 !text-gray_r-12 leading-6 line-clamp-3`}
          >
            {product?.name}
          </Link>
          {product?.flashSale?.id > 0 &&
          product?.lowestPrice?.discountPercentage > 0 ? (
            <>
              {product?.lowestPrice.discountPercentage > 0 && (
                <div className='flex gap-x-1 mb-1 items-center'>
                  <div className='badge-solid-red'>
                    {Math.floor(product?.lowestPrice?.discountPercentage)}%
                  </div>
                  <div className='text-gray_r-11 line-through text-caption-2'>
                    {currencyFormat(product?.lowestPrice?.price)}
                  </div>
                </div>
              )}

              <div className='text-danger-500 font-semibold mb-2'>
                {product?.lowestPrice?.priceDiscount > 0 ? (
                  currencyFormat(product?.lowestPrice?.priceDiscount)
                ) : (
                  <a
                    rel='noopener noreferrer'
                    target='_blank'
                    href={callForPriceWhatsapp}
                  >
                    Call for Inquiry
                  </a>
                )}
              </div>
            </>
          ) : (
            <div className='text-danger-500 font-semibold mb-2 min-h-[40px]'>
              {product?.lowestPrice.price > 0 ? (
                <>
                  {currencyFormat(product?.lowestPrice.price)}
                  <div className='text-gray_r-9 text-[11px] sm:text-caption-2 font-normal mt-2'>
                    Inc. PPN:{' '}
                    {currencyFormat(
                      product.lowestPrice.price * process.env.NEXT_PUBLIC_PPN
                    )}
                  </div>
                </>
              ) : (
                <a
                  rel='noopener noreferrer'
                  target='_blank'
                  href={callForPriceWhatsapp}
                >
                  Call for Inquiry
                </a>
              )}
            </div>
          )}

          {discount > 0 && product?.flashSale?.id < 1 && (
            <div className='flex gap-x-1 mb-1 text-sm'>
              <div className='inline-flex items-center rounded-md bg-green-50 px-2 py-1 text-xs font-medium text-green-700 ring-1 ring-inset ring-green-600/20'>
                Voucher : {currencyFormat(discount)}
              </div>
            </div>
          )}

          <div className='flex w-full items-center gap-x-1 '>
            {product?.stockTotal > 0 && (
              <div className='badge-solid-red'>Ready Stock</div>
            )}
            {/* <div className='badge-gray'>{product?.stockTotal > 5 ? '> 5' : '< 5'}</div> */}
            {product?.qtySold > 0 && (
              <div className='text-gray_r-9 text-[11px]'>
                {sellingProductFormat(product?.qtySold) + ' Terjual'}
              </div>
            )}
          </div>
        </div>
      </div>
    );
  }
};

export default ProductCard;