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
|
import {
AutoComplete,
AutoCompleteInput,
AutoCompleteItem,
AutoCompleteList,
} from '@choc-ui/chakra-autocomplete';
import style from '../styles/information.module.css';
import dynamic from 'next/dynamic';
import Link from 'next/link';
import { useEffect, useRef, useState } from 'react';
import axios from 'axios'; // <--- 1. TAMBAHAN IMPORT AXIOS
import currencyFormat from '@/core/utils/currencyFormat';
import { InputGroup, InputRightElement, SimpleGrid, Flex, Text, Box } from '@chakra-ui/react';
import { ChevronDownIcon } from '@heroicons/react/24/outline';
import Image from 'next/image';
import { formatToShortText } from '~/libs/formatNumber';
import { createSlug } from '~/libs/slug';
import { IProductDetail } from '~/types/product';
import { useProductDetail } from '../stores/useProductDetail';
import useVariant from '../hook/useVariant';
const Skeleton = dynamic(() =>
import('@chakra-ui/react').then((mod) => mod.Skeleton)
);
type Props = {
product: IProductDetail;
};
const Information = ({ product }: Props) => {
const { selectedVariant, setSelectedVariant, setSla, sla } =
useProductDetail();
const [inputValue, setInputValue] = useState<string | null>(
selectedVariant?.code + ' - ' + selectedVariant?.attributes[0]
);
const [disableFilter, setDisableFilter] = useState<boolean>(false);
const inputRef = useRef<HTMLInputElement>(null);
const [variantOptions, setVariantOptions] = useState<any[]>(
product?.variants
);
const variantId = selectedVariant?.id;
const { slaVariant, isLoading } = useVariant({ variantId });
const [warranties, setWarranties] = useState<Record<string, string>>({});
const [loadingWarranty, setLoadingWarranty] = useState(false);
useEffect(() => {
const fetchWarrantyDirectly = async () => {
if (!product?.variants || product.variants.length === 0) return;
setLoadingWarranty(true);
try {
// Ambil semua SKU untuk dikirim ke API
const skus = product.variants.map((v) => v.id).join(',');
const mainSku = product.variants[0].id;
console.log("Fetching warranties for SKUs:", skus);
console.log("Main SKU:", mainSku);
// Panggil API magento-product
const res = await axios.get('/api/magento-product', {
params: { skus, main_sku: mainSku }
});
// Simpan hasil ke state lokal
if (res.data && res.data.warranties) {
setWarranties(res.data.warranties);
}
console.log("Warranties API Response:", res);
console.log("Warranties fetched:", res.data.warranties);
} catch (error) {
console.error("Gagal ambil garansi:", error);
} finally {
setLoadingWarranty(false);
}
};
fetchWarrantyDirectly();
}, [product]);
// ======================================================
useEffect(() => {
if (selectedVariant) {
setInputValue(
selectedVariant?.code +
(selectedVariant?.attributes[0]
? ' - ' + selectedVariant?.attributes[0]
: '')
);
}
}, [selectedVariant]);
useEffect(() => {
if (isLoading) {
setSla(null);
}
if (slaVariant) {
setSla(slaVariant);
}
}, [slaVariant, isLoading, setSla]);
const handleOnChange = (vals: any) => {
setDisableFilter(true);
let code = vals.replace(/\s-\s.*$/, '').trim();
let variant = product?.variants.find((item) => item.code === code);
if (variant) {
setSelectedVariant(variant);
setInputValue(
variant?.code +
(variant?.attributes[0] ? ' - ' + variant?.attributes[0] : '')
);
setVariantOptions(product?.variants);
}
};
const handleOnKeyUp = (e: any) => {
setDisableFilter(false);
setInputValue(e.target.value);
};
// STYLE CUSTOM UNTUK BARIS (Item Code, dll)
const rowStyle = {
backgroundColor: '#ffffff',
fontSize: '13px',
borderBottom: '1px dashed #e2e8f0',
padding: '8px 0',
marginBottom: '0px'
};
return (
<div className={style['wrapper']}>
<div className='realtive mb-5'>
<label className='form-label mb-2 text-lg text-red-600'>
Pilih Variant * :{' '}
<span className='text-gray_r-9 text-sm'>
{product?.variants?.length} Variants
</span>{' '}
</label>
<AutoComplete
disableFilter={disableFilter}
openOnFocus
className='form-input'
onChange={(vals) => handleOnChange(vals)}
>
<InputGroup>
<AutoCompleteInput
ref={inputRef}
value={inputValue as string}
onChange={(e) => handleOnKeyUp(e)}
onFocus={() => setDisableFilter(true)}
/>
<InputRightElement className='mr-4'>
<ChevronDownIcon
className='h-6 w-6 text-gray-500'
onClick={() => inputRef?.current?.focus()}
/>
</InputRightElement>
</InputGroup>
<AutoCompleteList>
{variantOptions
.sort((a: any, b: any) => {
return a.code.localeCompare(b.code, undefined, { numeric: true, sensitivity: 'base' });
})
.map((option, cid) => (
<AutoCompleteItem
key={`option-${cid}`}
value={
option.code +
(option?.attributes[0] ? ' - ' + option?.attributes[0] : '')
}
_selected={
option.id === selectedVariant?.id
? {
bg: 'gray.300',
}
: undefined
}
textTransform='capitalize'
>
<div
key={cid}
className='flex gap-x-2 w-full justify-between px-3 items-center p-2'
>
<div className='text-small'>
{option.code +
(option?.attributes[0]
? ' - ' + option?.attributes[0]
: '')}
</div>
<div
className={
option?.price?.discount_percentage
? 'flex gap-x-4 items-center justify-between'
: ''
}
>
{option?.price?.discount_percentage > 0 && (
<>
<div className='badge-solid-red text-xs'>
{Math.floor(option?.price?.discount_percentage)}%
</div>
<div className='min-w-16 sm:min-w-24 text-gray_r-11 line-through text-[11px] sm:text-caption-2'>
{currencyFormat(option?.price?.price)}
</div>
</>
)}
<div className='min-w-20 sm:min-w-28 text-danger-500 font-semibold'>
{currencyFormat(option?.price?.price_discount)}
</div>
</div>
</div>
</AutoCompleteItem>
))}
</AutoCompleteList>
</AutoComplete>
</div>
{/* ITEM CODE */}
<div className={style['row']} style={rowStyle}>
<div className={style['label']} style={{ color: '#6b7280' }}>Item Code</div>
<div className={style['value']}>{selectedVariant?.code}</div>
</div>
{/* MANUFACTURE */}
<div className={style['row']} style={rowStyle}>
<div className={style['label']} style={{ color: '#6b7280' }}>Manufacture</div>
<div className={style['value']}>
{!!product.manufacture.name ? (
<Link
href={createSlug(
'/shop/brands/',
product.manufacture.name,
product.manufacture.id.toString()
)}
>
{product?.manufacture.logo ? (
<Image
height={50}
width={100}
src={product.manufacture.logo}
alt={product.manufacture.name}
className='h-8 object-fit'
/>
) : (
<p className='font-bold text-red-500'>
{product.manufacture.name}
</p>
)}
</Link>
) : (
'-'
)}
</div>
</div>
{/* BERAT BARANG */}
<div className={style['row']} style={rowStyle}>
<div className={style['label']} style={{ color: '#6b7280' }}>Berat Barang</div>
<div className={style['value']}>
{selectedVariant?.weight > 0 ? `${selectedVariant?.weight} Kg` : '-'}
</div>
</div>
{/* TERJUAL */}
<div className={style['row']} style={{ ...rowStyle, borderBottom: 'none' }}>
<div className={style['label']} style={{ color: '#6b7280' }}>Terjual</div>
<div className={style['value']}>
{product.qty_sold > 0 ? formatToShortText(product.qty_sold) : '-'}
</div>
</div>
{/* === DETAIL INFORMASI PRODUK (Updated Layout) === */}
<div className="mt-6 border-t pt-4">
<h2 className="font-bold text-gray-800 text-sm mb-4">Detail Informasi Produk</h2>
<SimpleGrid columns={{ base: 1, md: 3 }} spacing={10}>
{/* 1. Distributor Resmi */}
<Flex align="center" className="gap-3">
<img
src="/images/produk_asli.svg"
alt="Distributor Resmi"
className="w-10 h-10 shrink-0"
/>
<Box>
<Text fontSize="11px" color="gray.500" lineHeight="short" mb="1px">Distributor Resmi</Text>
<Text fontSize="12px" fontWeight="bold" color="gray.800" lineHeight="short">Jaminan Produk Asli</Text>
</Box>
</Flex>
{/* 2. Estimasi Penyiapan */}
<Flex align="center" className="gap-3">
<img
src="/images/estimasi.svg"
alt="Estimasi Penyiapan"
className="w-9 h-9 shrink-0"
/>
<Box>
<Text fontSize="11px" color="gray.500" lineHeight="short" mb="1px">Estimasi Penyiapan</Text>
{isLoading ? (
<Skeleton height="12px" width="60px" mt="2px" />
) : (
<Text fontSize="12px" fontWeight="bold" color="gray.800" lineHeight="short">
{sla?.sla_date || '3 - 7 Hari'}
</Text>
)}
</Box>
</Flex>
{/* 3. Garansi Produk */}
<Flex align="center" className="gap-3">
<img
src="/images/garansi.svg"
alt="Garansi Produk"
className="w-10 h-10 shrink-0"
/>
<Box>
<Text fontSize="11px" color="gray.500" lineHeight="short" mb="1px">Garansi Produk</Text>
{/* Menggunakan Loading State & Data dari 'warranties' */}
{loadingWarranty ? (
<Skeleton height="12px" width="80px" mt="2px" />
) : (
<Text fontSize="12px" fontWeight="bold" color="gray.800" lineHeight="short">
{selectedVariant && warranties[selectedVariant.id]
? warranties[selectedVariant.id]
: '-'}
</Text>
)}
</Box>
</Flex>
</SimpleGrid>
</div>
</div>
);
};
export default Information;
|