import { useState, useEffect } from 'react'; import { Modal, Button, Container, Row, Col, Tab, Tabs, Form, InputGroup } from 'react-bootstrap'; import MenuItem from '../../molecules/MenuItem'; import BasicTemplate from '../../templates/BasicTemplate'; import DataStorage from '../../organisms/DataStorage'; import CacheStorage from '../../organisms/CacheStorage'; import axios from 'axios'; const categoryMapper = [ { itemType: 1, category: 'Món chính' }, { itemType: 2, category: 'Đồ uống' }, { itemType: 3, category: 'Tráng miệng' }, ]; function MenuPage() { const [key, setKey] = useState(0); const [selectedDish, setSelectedDish] = useState(null); const [show, setShow] = useState(false); const [cartAmount, setCartAmount] = useState(0); const [branches, setBranches] = useState([]); const defaultSelectedStore = CacheStorage.get('selectedStore') || ""; const [selectedStore, setSelectedStore] = useState(defaultSelectedStore); const [loadingBranches, setLoadingBranches] = useState(true); const [loading, setLoading] = useState(true); const [menuItems, setMenuItems] = useState({}); function handleClose() { const cart = JSON.parse(DataStorage.get('cart')) || {}; // Đảm bảo cart không null cart[selectedDish.id] = { 'name': selectedDish.item_name, 'amount': cartAmount, 'imageSrc': selectedDish.image_url, 'price': selectedDish.price }; for (let id in cart) { if (cart[id].amount === 0) { delete cart[id]; } DataStorage.set('cart', JSON.stringify(cart)); // console.log(JSON.stringify(filteredCart)); setShow(false); } } function notLoggedInClose() { setShow(false); } function handleShow(dish) { if (DataStorage.get('isLoggedIn') !== 'true' || !DataStorage.get('cart')) { setShow(true); return; } else { setSelectedDish(dish); // Đặt selectedDish ngay lập tức // Sau khi cập nhật selectedDish, lấy dữ liệu từ cart theo tên món ăn mới const cart = JSON.parse(DataStorage.get('cart')) || {}; // Kiểm tra số lượng món ăn trong cart const amount = cart[dish.id] !== undefined ? cart[dish.id].amount : 0; setCartAmount(amount); // Cập nhật số lượng setShow(true); // Hiển thị modal } } function setIncrease() { setCartAmount(cartAmount + 1); } function setDecrease() { if (cartAmount > 0) { setCartAmount(cartAmount - 1); } } useEffect(() => { const fetchBranches = async () => { try { const response = await axios.get(process.env.REACT_APP_API_URL + '/branchs'); // Thay 'API_ENDPOINT' bằng URL của API setBranches(response.data); // Lưu dữ liệu vào state setLoadingBranches(false); // Đặt loading thành false khi hoàn tất CacheStorage.set('stores', JSON.stringify(Object(response.data))); } catch (error) { console.error('Error fetching branches:', error); setLoadingBranches(false); // Đặt loading thành false nếu lỗi } }; if (CacheStorage.get('stores')) { setBranches(JSON.parse(CacheStorage.get('stores'))); setLoadingBranches(false); } else { fetchBranches(); } }, []) // useEffect(() => { // const fetchMenuItems = async () => { // try { // const menuItemsByType = {}; // for (const item of categoryMapper) { // // Gọi API để lấy món theo itemType // const response = await axios.get(process.env.REACT_APP_API_URL + `/menu-items?filter.item_type=${item.itemType}`); // // Lưu danh sách món theo itemType // menuItemsByType[item.itemType] = response.data.data; // } // console.log(menuItemsByType); // setMenuItems(menuItemsByType); // setLoading(false); // CacheStorage.set('menuItems',JSON.stringify(menuItemsByType)); // } catch (error) { // console.error('Error fetching menu items:', error); // setLoading(false); // } // }; // if (CacheStorage.get('menuItems')) { // setMenuItems(JSON.parse(CacheStorage.get('menuItems'))); // setLoading(false); // } else { // fetchMenuItems(); // } // }, []); function organizeMenuItemsByType(menuData) { // Lấy ra tất cả menu_item từ array ban đầu const menuItems = menuData.map(item => item.menu_item); // Tạo một object để nhóm menu_item theo item_type, khởi tạo các mảng rỗng cho mỗi item_type từ categoryMapper const organizedItems = categoryMapper.reduce((acc, { itemType }) => { acc[itemType] = []; return acc; }, {}); // Thêm menu_item vào organizedItems theo item_type menuItems.forEach(menuItem => { const type = menuItem.item_type; // Đưa menu_item vào array của item_type tương ứng if (organizedItems[type]) { organizedItems[type].push(menuItem); } }); return organizedItems; } useEffect(() => { const fetchStoreData = async (storeId) => { if (storeId!=="") { try { let menuItemsByType = {}; // Gọi API để lấy món theo itemType const response = await axios.get(process.env.REACT_APP_API_URL + `/branchs/${storeId}/menus?limit=100`); // Lưu danh sách món theo itemType console.log('Response', response.data.data); menuItemsByType = organizeMenuItemsByType(response.data.data); console.log(menuItemsByType); setMenuItems(menuItemsByType); setLoading(false); CacheStorage.set(`store_menu_${storeId}`, JSON.stringify(menuItemsByType)); } catch (error) { console.error('Error fetching menu items:', error); setLoading(false); } } } // Kiểm tra cache thông tin chi nhánh trong sessionStorage const cachedStoreData = CacheStorage.get(`store_menu_${selectedStore}`); if (cachedStoreData) { setMenuItems(JSON.parse(cachedStoreData)); setLoading(false); } else { fetchStoreData(selectedStore); } }, [selectedStore]); const handleSelectStore = (e) => { setSelectedStore(e.target.value); CacheStorage.set('selectedStore',e.target.value); setLoading(true); }; let selectboxContent; if (loadingBranches) { selectboxContent = (
Đang tải dữ liệu...
) } else { selectboxContent = ({selectedDish?.description}
{/* Dish description */}Đang tải thực đơn...
); } else { menuContent = (