Spaces:
Sleeping
Sleeping
File size: 5,888 Bytes
fdbdf19 708809b 22c310d fdbdf19 22c310d fdbdf19 22c310d |
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 |
import { useState, useEffect } from 'react';
import { Container, Carousel, Row, Col, Button } from 'react-bootstrap';
import MenuItem from '../molecules/MenuItem';
import CacheStorage from './CacheStorage';
import axios from 'axios';
const carouselImages = [
{ itemType: 1, imageUrl: '/dishes.jpg' },
{ itemType: 2, imageUrl: '/drinks.jpg' },
{ itemType: 3, imageUrl: '/desserts.jpg' },
];
function MenuSection() {
const [carouselMaxHeight, setHeight] = useState('700px');
const [index, setIndex] = useState(0);
const [menuItems, setMenuItems] = useState({}); // Đối tượng lưu danh sách món theo từng itemType
const [loading, setLoading] = useState(true);
const handleSelect = (selectedIndex) => {
setIndex(selectedIndex);
};
// const menuItems = [
// { name: 'Món 1', description: 'Mô tả món 1', imageSrc: '/placeholder3.jpg' },
// { name: 'Món 2', description: 'Mô tả món 2', imageSrc: '/placeholder3.jpg' },
// { name: 'Món 3', description: 'Mô tả món 3', imageSrc: '/placeholder3.jpg' }
// ];
const categoryMapper = {
1: 'Món chính',
2: 'Đồ uống',
3: 'Tráng miệng',
};
useEffect(() => {
const fetchMenuItems = async () => {
try {
const menuItemsByType = {};
for (const carousel of carouselImages) {
// Gọi API để lấy món theo itemType
const response = await axios.get(process.env.REACT_APP_API_URL + `/menu-items?limit=3&filter.item_type=${carousel.itemType}`);
// Lưu danh sách món theo itemType
menuItemsByType[carousel.itemType] = response.data.data;
}
console.log(menuItemsByType);
setMenuItems(menuItemsByType);
setLoading(false);
CacheStorage.set('homeMenuItems',JSON.stringify(menuItemsByType));
console.log('fetched from API');
} catch (error) {
console.error('Error fetching menu items:', error);
setLoading(false);
}
};
if (CacheStorage.get('homeMenuItems')) {
setMenuItems(JSON.parse(CacheStorage.get('homeMenuItems')));
setLoading(false);
console.log('fetched from cache');
} else {
fetchMenuItems();
}
}, []);
useEffect(() => {
const handleResize = () => {
if (window.innerWidth < 992) { // md
setHeight('1500px'); // Không giới hạn chiều cao
} else {
setHeight('700px'); // Giới hạn chiều cao cho md trở lên
}
};
// Đăng ký event resize
window.addEventListener('resize', handleResize);
// Gọi lần đầu để xác định chiều cao ban đầu
handleResize();
// Cleanup
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);
let menuContent;
if (loading) {
menuContent = (<p>Đang tải dữ liệu</p>);
} else {
menuContent = (<Carousel activeIndex={index} onSelect={handleSelect} data-bs-theme="dark">
{carouselImages.map((carousel, index) => (
<Carousel.Item key={index}>
<div className="d-flex justify-content-center align-items-center" style={{ width: '100%' }}>
<img
className="img-fluid"
src={carousel.imageUrl}
alt={`Carousel ${carousel.itemType}`}
style={{
height: carouselMaxHeight,
width: '100vw',
objectPosition: 'center',
objectFit: 'cover',
}}
/>
<div
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
backgroundColor: 'rgba(0, 0, 0, 0.5)', // Màu tối với độ trong suốt
}}
></div>
</div>
<Carousel.Caption>
<div className='my-5'>
<h1 style={{ color: "var(--text-color)" }} className='mb-5'>{categoryMapper[carousel.itemType]}</h1>
<Row className="g-4">
{Array.from(menuItems[carousel.itemType]).map((item, idx) => (
<Col sm={12} lg={4} key={idx}>
<MenuItem
dishName={item.item_name}
description={item.description}
imageSrc={item.image_url}
/>
</Col>
))}
</Row>
<Button as='a' href='/menu' className='mt-5'> Xem thêm các món </Button>
</div>
</Carousel.Caption>
</Carousel.Item>
))}
</Carousel>);
}
return (
<Container id="menu" className="text-center justify-content-center align-items-center my-5" style={{ maxWidth: "100%" }}>
<h1 className='mb-5'>Menu</h1>
{menuContent}
</Container>
);
}
export default MenuSection;
|