[c44c5ed] | 1 | import React, { useState, useEffect } from 'react';
|
---|
| 2 | import axios from 'axios';
|
---|
| 3 | import 'bootstrap/dist/css/bootstrap.min.css';
|
---|
| 4 |
|
---|
| 5 | const ITEMS_PER_PAGE = 6;
|
---|
| 6 |
|
---|
| 7 | const ReadOnlyMenuList = ({ restaurantId }) => {
|
---|
| 8 | const [menuItems, setMenuItems] = useState([]);
|
---|
| 9 | const [currentPage, setCurrentPage] = useState(1);
|
---|
| 10 |
|
---|
| 11 | useEffect(() => {
|
---|
| 12 | const fetchMenu = async () => {
|
---|
| 13 | try {
|
---|
| 14 | const response = await axios.get(`http://localhost:8081/api/restaurant-menu/${restaurantId}`);
|
---|
| 15 | setMenuItems(response.data);
|
---|
| 16 | setCurrentPage(1); // Reset to first page on restaurant change
|
---|
| 17 | } catch (err) {
|
---|
| 18 | console.error('Failed to fetch menu:', err);
|
---|
| 19 | }
|
---|
| 20 | };
|
---|
| 21 |
|
---|
| 22 | if (restaurantId) {
|
---|
| 23 | fetchMenu();
|
---|
| 24 | }
|
---|
| 25 | }, [restaurantId]);
|
---|
| 26 |
|
---|
| 27 | const indexOfLastItem = currentPage * ITEMS_PER_PAGE;
|
---|
| 28 | const indexOfFirstItem = indexOfLastItem - ITEMS_PER_PAGE;
|
---|
| 29 | const currentItems = menuItems.slice(indexOfFirstItem, indexOfLastItem);
|
---|
| 30 | const totalPages = Math.ceil(menuItems.length / ITEMS_PER_PAGE);
|
---|
| 31 |
|
---|
| 32 | const handlePageChange = (pageNumber) => {
|
---|
| 33 | setCurrentPage(pageNumber);
|
---|
| 34 | };
|
---|
| 35 |
|
---|
| 36 | return (
|
---|
| 37 | <div className="container mt-4">
|
---|
| 38 | {menuItems.length > 0 && <h3 className="text-center">Menu</h3>}
|
---|
| 39 | <div className="row">
|
---|
| 40 | {currentItems.map((item) => (
|
---|
| 41 | <div key={item.menuID} className="col-md-6 mb-4">
|
---|
| 42 | <div className="card h-100 shadow-sm">
|
---|
| 43 | <div className="card-body d-flex flex-column">
|
---|
| 44 | <h5 className="card-title">{item.itemName}</h5>
|
---|
| 45 | <p className="card-text">{item.description}</p>
|
---|
| 46 | <h6 className="card-subtitle mb-2 text-muted">${item.price.toFixed(2)}</h6>
|
---|
[142c0f8] | 47 |
|
---|
| 48 | {item.tags && item.tags.length > 0 && (
|
---|
| 49 | <div className="mb-2">
|
---|
| 50 | {item.tags.map((tag) => (
|
---|
| 51 | <span key={tag.id} className="badge bg-secondary me-1">
|
---|
| 52 | {tag.tagName}: {tag.tagValue}
|
---|
| 53 | </span>
|
---|
| 54 | ))}
|
---|
| 55 | </div>
|
---|
| 56 | )}
|
---|
[c44c5ed] | 57 | </div>
|
---|
| 58 | </div>
|
---|
| 59 | </div>
|
---|
| 60 | ))}
|
---|
| 61 | </div>
|
---|
| 62 |
|
---|
| 63 | {totalPages > 1 && (
|
---|
| 64 | <nav className="d-flex justify-content-center">
|
---|
| 65 | <ul className="pagination">
|
---|
| 66 | {Array.from({ length: totalPages }, (_, index) => (
|
---|
| 67 | <li
|
---|
| 68 | key={index + 1}
|
---|
| 69 | className={`page-item ${currentPage === index + 1 ? 'active' : ''}`}
|
---|
| 70 | >
|
---|
| 71 | <button
|
---|
| 72 | type="button"
|
---|
| 73 | className="page-link"
|
---|
| 74 | onClick={() => handlePageChange(index + 1)}
|
---|
| 75 | >
|
---|
| 76 | {index + 1}
|
---|
| 77 | </button>
|
---|
| 78 | </li>
|
---|
| 79 | ))}
|
---|
| 80 | </ul>
|
---|
| 81 | </nav>
|
---|
| 82 | )}
|
---|
| 83 | </div>
|
---|
| 84 | );
|
---|
| 85 | };
|
---|
| 86 |
|
---|
| 87 | export default ReadOnlyMenuList;
|
---|