summaryrefslogtreecommitdiff
path: root/src-migrate/modules/product-detail/components/PriceAction.tsx
blob: 405eb12bcf4f3e5430c25e4e7a7c356606841180 (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
import style from '../styles/price-action.module.css';

import React, { useEffect } from 'react';
import formatCurrency from '~/libs/formatCurrency';
import { IProductDetail } from '~/types/product';
import { useProductDetail } from '../stores/useProductDetail';
import AddToCart from './AddToCart';
import Link from 'next/link';
import { getAuth } from '~/libs/auth';

type Props = {
  product: IProductDetail;
};

const PriceAction = ({ product }: Props) => {
  const {
    activePrice,
    setActive,
    activeVariantId,
    quantityInput,
    setQuantityInput,
    askAdminUrl,
    isApproval,
    setIsApproval,
    selectedVariant,
    sla,
  } = useProductDetail();

  useEffect(() => {
    setActive(selectedVariant);
    if (product.variants.length > 2 && product.variants[0].price.price === 0) {
      const variants = product.variants;
      for (let i = 0; i < variants.length; i++) {
        if (variants[i].price.price > 0) {
          setActive(variants[i]);
          break;
        }
      }
    }
  }, [product, setActive, selectedVariant]);

  return (
    <div
      className='block md:sticky top-[150px] bg-white py-0 md:py-6 z-10'
      id='price-section'
    >
      {!!activePrice && activePrice.price > 0 && (
        <>
          <div className='flex items-end gap-x-2'>
            {activePrice.discount_percentage > 0 && (
              <>
                <div className={style['disc-badge']}>
                  {Math.floor(activePrice.discount_percentage)}%
                </div>
                <div className={style['disc-price']}>
                  Rp {formatCurrency(activePrice.price || 0)}
                </div>
              </>
            )}
            <div className={style['main-price']}>
              Rp {formatCurrency(activePrice.price_discount || 0)}
            </div>
          </div>
          <div className='h-1' />
          <div className={style['secondary-text']}>
            Termasuk PPN: Rp{' '}
            {formatCurrency(Math.round(activePrice.price_discount * 1.11))}
          </div>
        </>
      )}

      {!!activePrice && activePrice.price === 0 && (
        <span>
          Hubungi kami untuk dapatkan harga terbaik,{' '}
          <Link
            href={askAdminUrl}
            target='_blank'
            className={style['contact-us']}
          >
            klik disini
          </Link>
        </span>
      )}

      <div className='h-4' />
      <div className='flex gap-x-5 items-center'>
        <div>
          <label htmlFor='quantity' className='hidden'>
            Quantity
          </label>
          <input
            type='number'
            id='quantity'
            value={quantityInput}
            onChange={(e) => setQuantityInput(e.target.value)}
            className={style['quantity-input']}
          />
        </div>
        <div>
          <span className={sla?.qty < 10 ? 'text-red-600 font-medium' : ''} > Stock : {sla?.qty} </span>
        </div>
      </div>
      <div className='h-4' />

      <div className={style['action-wrapper']}>
        <AddToCart
          variantId={activeVariantId}
          quantity={Number(quantityInput)}
        />
        {!isApproval && (
          <AddToCart
            source='buy'
            variantId={activeVariantId}
            quantity={Number(quantityInput)}
          />
        )}
      </div>
    </div>
  );
};

export default PriceAction;