summaryrefslogtreecommitdiff
path: root/src/lib/variant/components/VariantCard.jsx
blob: 6b06bc31c9040237df86f0044488cfce67915073 (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
import { useRouter } from 'next/router'
import { toast } from 'react-hot-toast'

import Image from '@/core/components/elements/Image/Image'
import Link from '@/core/components/elements/Link/Link'
import { createSlug } from '@/core/utils/slug'
import currencyFormat from '@/core/utils/currencyFormat'
import { updateItemCart } from '@/core/utils/cart'
import whatsappUrl from '@/core/utils/whatsappUrl'

const VariantCard = ({ product, openOnClick = true, buyMore = false }) => {
  const router = useRouter()

  const addItemToCart = () => {
    toast.success('Berhasil menambahkan ke keranjang', { duration: 1500 })
    updateItemCart({
      productId: product.id,
      quantity: 1
    })
    return
  }

  const checkoutItem = () => {
    router.push(`/shop/checkout?product_id=${product.id}&qty=${product.quantity}`)
  }

  const Card = () => (
    <div className='flex gap-x-3'>
      <div className='w-4/12 flex items-center gap-x-2'>
        <Image
          src={product.parent.image}
          alt={product.parent.name}
          className='object-contain object-center border border-gray_r-6 h-32 w-full rounded-md'
        />
      </div>
      <div className='w-8/12 flex flex-col'>
        <p className='product-card__title wrap-line-ellipsis-2'>{product.parent.name}</p>
        <p className='text-caption-2 text-gray_r-11 mt-1'>
          {product.code || '-'}
          {product.attributes.length > 0 ? ` ・ ${product.attributes.join(', ')}` : ''}
        </p>
        <p className='text-caption-2 text-gray_r-11 mt-1'>
          Berat Item : {product?.weight} Kg
        </p>
        <div className='flex flex-wrap gap-x-1 items-center mt-auto'>
          {product.hasFlashsale && (
            <>
              <p className='text-caption-2 text-gray_r-11 line-through'>
                {currencyFormat(product.price.price)}
              </p>
              <span className='badge-red'>{product.price.discountPercentage}%</span>
            </>
          )}
        </div>
        <p className='text-caption-2 text-gray_r-11 mt-1'>
          {product.price.priceDiscount > 0
            ? currencyFormat(product.price.priceDiscount) + ' × ' + product.quantity + ' Barang'
            : ''}
        </p>
        <p className='text-caption-2 text-gray_r-12 font-bold mt-2'>
          {product.price.priceDiscount > 0 ? (
            currencyFormat(product.quantity * product.price.priceDiscount)
          ) : (
            <a
              href={whatsappUrl('product', {
                name: product.name,
                url: createSlug('/shop/product/', product.name, product.id, true)
              })}
              className='underline text-danger-500'
            >
              Call for Inquiry{' '}
            </a>
          )}
        </p>
      </div>
    </div>
  )

  if (openOnClick) {
    return (
      <>
        <Link href={createSlug('/shop/product/', product.parent.name, product.parent.id)}>
          <Card />
        </Link>
        {buyMore && (
          <div className='flex justify-end gap-x-2 mb-2'>
            <button
              type='button'
              onClick={addItemToCart}
              className='btn-yellow text-gray_r-12 py-2 px-3 text-caption-1'
            >
              Tambah Keranjang
            </button>
            <button
              type='button'
              onClick={checkoutItem}
              className='btn-solid-red py-2 px-3 text-caption-1'
            >
              Beli Lagi
            </button>
          </div>
        )}
      </>
    )
  }

  return <Card />
}

export default VariantCard