source: frontend/src/components/forms/menu-item-form.jsx@ badbc79

Last change on this file since badbc79 was badbc79, checked in by Luka Cheshlarov <luka.cheshlarov@…>, 20 months ago

Initial commit

  • Property mode set to 100644
File size: 2.5 KB
Line 
1import {Button, FormControl, Grid, InputAdornment, TextField} from "@mui/material";
2import {useLocation, useNavigate} from "react-router-dom";
3import {useState} from "react";
4import {useAuthContext} from "../../configurations/AuthContext";
5import {CreateMenuItem, UpdateMenuItem} from "../../services/menu-item-service";
6
7const MenuItemForm = ({menuItem, restorantId = undefined, onClose}) => {
8 const navigate = useNavigate();
9 const location = useLocation();
10
11 const [formData, setFormData] = useState({
12 ime: menuItem?.ime ?? "",
13 cena: menuItem?.cena ?? 0,
14 restorantId: restorantId
15 });
16
17 const handleChange = name => event => {
18 setFormData({...formData, [name]: event.target.value});
19 };
20
21 const handleSubmit = async event => {
22 event.preventDefault();
23 if (menuItem) {
24 await UpdateMenuItem(menuItem.id, formData)
25 navigate(location.pathname);
26 onClose();
27
28 return;
29 }
30
31 await CreateMenuItem(formData)
32 onClose();
33 navigate(location.pathname);
34 }
35
36 return (<form onSubmit={handleSubmit} className={"m-2"}>
37 <Grid container spacing={2}>
38 <Grid item xs={12} sm={6}>
39 <TextField
40 onChange={handleChange("ime")}
41 id="ime"
42 name="ime"
43 variant="outlined"
44 required
45 fullWidth
46 label="Ime"
47 value={formData.ime}
48 inputProps={{
49 minLength: 2,
50 }}
51 />
52 </Grid>
53 <Grid item xs={12} sm={6}>
54 <TextField
55 onChange={handleChange("cena")}
56 variant="outlined"
57 required
58 fullWidth
59 label="Cena"
60 type="number"
61 name="cena"
62 value={formData.cena}
63 InputProps={{
64 startAdornment: <InputAdornment position="start">MKD</InputAdornment>,
65 }}
66 />
67 </Grid>
68 <Grid item>
69 <Button
70 type="submit"
71 fullWidth
72 variant="contained"
73 >
74 {menuItem ? "Edit" : "Create"}
75 </Button>
76 </Grid>
77 </Grid>
78 </form>)
79}
80
81export default MenuItemForm;
Note: See TracBrowser for help on using the repository browser.