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
|
import style from '../styles/price-action.module.css';
import Image from 'next/image';
import Link from 'next/link';
import { useEffect, useState } from 'react';
import formatCurrency from '~/libs/formatCurrency';
import { IProductDetail } from '~/types/product';
import { useProductDetail } from '../stores/useProductDetail';
import AddToCart from './AddToCart';
import AddToQuotation from './AddToQuotation';
import { getAuth } from '~/libs/auth';
import useDevice from '@/core/hooks/useDevice';
import odooApi from '~/libs/odooApi';
import { Button, Skeleton } from '@chakra-ui/react';
type Props = {
product: IProductDetail;
};
const PriceAction = ({ product }: Props) => {
const {
activePrice,
setActive,
activeVariantId,
quantityInput,
setQuantityInput,
askAdminUrl,
isApproval,
setIsApproval,
selectedVariant,
sla,
} = useProductDetail();
const [qtyPickUp, setQtyPickUp] = useState(0);
const { isDesktop, isMobile } = useDevice();
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]);
useEffect(() => {
const fetchData = async () => {
const qty_available = await odooApi(
'GET',
`/api/v1/product_variant/${selectedVariant.id}/qty_available`
);
setQtyPickUp(qty_available?.qty);
};
fetchData();
}, [selectedVariant]);
useEffect(() => {
setQuantityInput('1');
}, [selectedVariant]);
let voucherPastiHemat = 0;
if (
product?.voucher_pasti_hemat
? product?.voucher_pasti_hemat.length
: voucherPastiHemat > 0
) {
const stringVoucher = product?.voucher_pasti_hemat[0];
const validJsonString = stringVoucher.replace(/'/g, '"');
voucherPastiHemat = JSON.parse(validJsonString);
}
return (
<div
className={`block md:sticky md:top-[150px] md:py-6 fixed bottom-0 left-0 right-0 bg-white p-2 z-10 ${
isMobile &&
'pb-6 pt-6 rounded-lg shadow-[rgba(0,0,4,0.1)_0px_-4px_4px_0px] '
}`}
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 className='relative flex items-center'>
<button
type='button'
className='absolute left-0 px-2 py-1 h-full text-gray-500'
onClick={() =>
setQuantityInput(String(Math.max(1, Number(quantityInput) - 1)))
}
>
-
</button>
<input
type='number'
id='quantity'
min={1}
value={quantityInput}
onChange={(e) => setQuantityInput(e.target.value)}
className={style['quantity-input']}
/>
<button
type='button'
className='absolute right-0 px-2 py-1 h-full text-gray-500'
onClick={() => setQuantityInput(String(Number(quantityInput) + 1))}
>
+
</button>
</div>
<div>
<Skeleton
isLoaded={sla}
h='21px'
// w={16}
className={sla?.qty < 10 ? 'text-red-600 font-medium' : ''}
>
Stock : {sla?.qty}{' '}
</Skeleton>
{/* <span className={sla?.qty < 10 ? 'text-red-600 font-medium' : ''}>
{' '}
</span> */}
</div>
<div>
{selectedVariant?.is_in_bu && (
<Link href='/panduan-pick-up-service' className='group'>
<Image
src='/images/PICKUP-NOW.png'
className='group-hover:scale-105 transition-transform duration-200'
alt='pickup now'
width={100}
height={12}
/>
</Link>
)}
</div>
</div>
{qtyPickUp > 0 && (
<div className='text-[12px] mt-1 text-red-500 italic'>
* {qtyPickUp} barang bisa di pickup
</div>
)}
<div className='h-4' />
<div className={`${style['action-wrapper']}`}>
<AddToCart
products={product}
variantId={activeVariantId}
quantity={Number(quantityInput)}
/>
{!isApproval && (
<AddToCart
source='buy'
products={product}
variantId={activeVariantId}
quantity={Number(quantityInput)}
/>
)}
</div>
<div className='mt-4'>
<AddToQuotation
source='buy'
products={product}
variantId={activeVariantId}
quantity={Number(quantityInput)}
/>
</div>
</div>
);
};
export default PriceAction;
|