| 1 | import React, {useState} from "react";
|
|---|
| 2 | import {Link, useLocation, useNavigate} from "react-router-dom";
|
|---|
| 3 | import {Button, Card, CardActions, CardContent, Grid, Typography} from "@mui/material";
|
|---|
| 4 | import {truncate} from "../functions";
|
|---|
| 5 | import CreateEditMenuItemModal from "../modals/menu-item-modal";
|
|---|
| 6 | import {DeleteMenuItem} from "../../services/menu-item-service";
|
|---|
| 7 | import {useAuthContext} from "../../configurations/AuthContext";
|
|---|
| 8 | import {UserRole} from "../../services/user-service";
|
|---|
| 9 | import {AddCircle, RemoveCircle, Restaurant} from "@mui/icons-material";
|
|---|
| 10 | import MenuItem from "../../assets/images/menuItem.jpg";
|
|---|
| 11 |
|
|---|
| 12 | const MenuItemCard = ({item, itemchange, quantitynb, detectAdminAction, skipChanges = false}) => {
|
|---|
| 13 | const {loggedUserRole, isAuthorized} = useAuthContext();
|
|---|
| 14 | const navigate = useNavigate();
|
|---|
| 15 | const location = useLocation();
|
|---|
| 16 | const [openUpdateModal, setOpenUpdateModal] = useState(false);
|
|---|
| 17 | const [quantity, setQuantity] = useState(quantitynb ?? 0);
|
|---|
| 18 |
|
|---|
| 19 | const handleMenuOrderQuantityChange = (number) => event => {
|
|---|
| 20 | if (number < 0) {
|
|---|
| 21 | return;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | setQuantity(number)
|
|---|
| 25 | const orderMenuItem = {menuItemId: item?.id, quantity: number};
|
|---|
| 26 | itemchange(orderMenuItem);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | const handleDelete = async (id) => {
|
|---|
| 30 | await DeleteMenuItem(id);
|
|---|
| 31 | navigate(location.pathname);
|
|---|
| 32 |
|
|---|
| 33 | if (detectAdminAction) {
|
|---|
| 34 | detectAdminAction(true)
|
|---|
| 35 | }
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | const handleEdit = () => {
|
|---|
| 39 | setOpenUpdateModal(false);
|
|---|
| 40 | navigate(location.pathname);
|
|---|
| 41 |
|
|---|
| 42 | if (detectAdminAction) {
|
|---|
| 43 | detectAdminAction(true)
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | return (
|
|---|
| 48 | item != null &&
|
|---|
| 49 | <Card
|
|---|
| 50 | className={`card-zoom m-3 `}>
|
|---|
| 51 | <CardContent className={"cursor-pointer"}>
|
|---|
| 52 | <Grid container direction={"row"} alignItems={"center"}>
|
|---|
| 53 | <Grid item md={3}>
|
|---|
| 54 | <img src={MenuItem} height={'100'} alt={"food"}/>
|
|---|
| 55 | </Grid>
|
|---|
| 56 | <Grid item md={6}>
|
|---|
| 57 | <Typography variant={"h5"}
|
|---|
| 58 | className={"font-weight-bold p-1"}>
|
|---|
| 59 | {truncate(item.ime, 15, 30)}
|
|---|
| 60 | </Typography>
|
|---|
| 61 | <Typography className={"card-description p-1"}>
|
|---|
| 62 | {item.cena} MKD
|
|---|
| 63 | </Typography>
|
|---|
| 64 | </Grid>
|
|---|
| 65 | {itemchange && <Grid item md={3} className={"d-flex flex-column"}>
|
|---|
| 66 | <Typography className={"card-description p-1"} align={"center"}>
|
|---|
| 67 | <b>{quantity} </b> items
|
|---|
| 68 | </Typography>
|
|---|
| 69 |
|
|---|
| 70 | {loggedUserRole?.role === UserRole.Potrosuvac && !skipChanges &&
|
|---|
| 71 | <Grid container item justifyContent={"space-around"}>
|
|---|
| 72 | <Button color="error" onClick={handleMenuOrderQuantityChange(quantity - 1)}>
|
|---|
| 73 | <RemoveCircle/>
|
|---|
| 74 | </Button>
|
|---|
| 75 | <Button onClick={handleMenuOrderQuantityChange(quantity + 1)}>
|
|---|
| 76 | <AddCircle/></Button></Grid>
|
|---|
| 77 | }
|
|---|
| 78 | <Typography className={"card-description mt-1 p-1"} align={"center"}>
|
|---|
| 79 | Price: <b>{quantity * item.cena}</b> MKD
|
|---|
| 80 | </Typography>
|
|---|
| 81 | </Grid>}
|
|---|
| 82 |
|
|---|
| 83 | <Grid item md={12} className={"mt-2"}>
|
|---|
| 84 | <Link to={`/restorants/${item.restoran.id}`}>
|
|---|
| 85 | <Restaurant/> {truncate(item.restoran.ime)}
|
|---|
| 86 | </Link>
|
|---|
| 87 | </Grid>
|
|---|
| 88 | </Grid>
|
|---|
| 89 |
|
|---|
| 90 | </CardContent>
|
|---|
| 91 | <CardActions>
|
|---|
| 92 | {(isAuthorized(item.restoran?.manager?.id) || loggedUserRole?.role === UserRole.Admin) &&
|
|---|
| 93 | <>
|
|---|
| 94 | <Button onClick={() => setOpenUpdateModal(true)}>
|
|---|
| 95 | Edit
|
|---|
| 96 | </Button>
|
|---|
| 97 | <Button onClick={() => handleDelete(item?.id)}>Delete</Button>
|
|---|
| 98 | </>}
|
|---|
| 99 | </CardActions>
|
|---|
| 100 |
|
|---|
| 101 | {openUpdateModal &&
|
|---|
| 102 | <CreateEditMenuItemModal menuItem={item} restorantId={item.restoran.id} open={openUpdateModal}
|
|---|
| 103 | onClose={handleEdit}/>}
|
|---|
| 104 | </Card>
|
|---|
| 105 | )
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | export default MenuItemCard; |
|---|