blob: f4be279e215e40b9bde2a02230f38f659ad09881 (
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
|
import Image from 'next/image';
import { useEffect, useState } from 'react';
import CountDown from '@/core/components/elements/CountDown/CountDown';
import ProductSlider from '@/lib/product/components/ProductSlider';
import { FlashSaleSkeleton } from '../skeleton/FlashSaleSkeleton';
const FlashSale = () => {
const [flashSales, setFlashSales] = useState(null);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const loadFlashSales = async () => {
const res = await fetch('/api/flashsale-header');
const { data } = await res.json();
if (data) {
setFlashSales(data);
}
setIsLoading(false);
};
loadFlashSales();
}, []);
if (isLoading) {
return <FlashSaleSkeleton />;
}
return (
<div className='sm:mt-4'>
{flashSales?.length > 0 && (
<div className='px-4 sm:px-0 grid grid-cols-1 gap-y-8 sm:mt-4'>
{flashSales.map((flashSale, index) => (
<div key={index}>
<div className='flex gap-x-3 mb-4 justify-between sm:justify-start'>
<div className='font-medium sm:text-h-lg mt-1.5'>
{flashSale.name}
</div>
<CountDown initialTime={flashSale.duration} />
</div>
<div className='relative'>
<Image
src={flashSale.banner}
alt={flashSale.name}
width={1080}
height={192}
className='w-full rounded mb-4 hidden sm:block'
loading='eager'
/>
<Image
src={flashSale.bannerMobile}
alt={flashSale.name}
width={256}
height={48}
className='w-full rounded mb-4 block sm:hidden'
loading='eager'
/>
<FlashSaleProduct
flashSaleId={flashSale.pricelistId}
duration={flashSale.duration}
/>
</div>
</div>
))}
</div>
)}
</div>
);
};
const FlashSaleProduct = ({ flashSaleId, duration }) => {
const [products, setProducts] = useState(null);
useEffect(() => {
const data_search = new URLSearchParams({
query: `fq=flashsale_id_i:${flashSaleId}&fq=flashsale_price_f:[1 TO *]&limit=500&orderBy=flashsale-price-asc&source=similar`,
operation: 'AND',
duration: `${duration}`,
});
const loadProducts = async () => {
const res = await fetch(
`/api/search-flashsale?${data_search.toString()}`
);
const { data } = await res.json();
setProducts(data.response);
};
loadProducts();
}, []);
return <ProductSlider products={products} />;
};
export default FlashSale;
|