source: frontend/src/components/forms/driver-vehicle-form.js@ 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.4 KB
Line 
1import {Button, Grid, TextField} from "@mui/material";
2import {useLocation, useNavigate} from "react-router-dom";
3import {useState} from "react";
4import {useAuthContext} from "../../configurations/AuthContext";
5import {CreateVehicle, UpdateVehicle} from "../../services/vehicle-service";
6
7const DriverVehicleForm = ({vehicle, onClose}) => {
8 const navigate = useNavigate();
9 const location = useLocation();
10
11 const {loggedUserRole} = useAuthContext();
12 const [formData, setFormData] = useState({
13 tip: vehicle?.tip ?? "",
14 registracija: vehicle?.registracija ?? "",
15 vozacId: vehicle?.vozac?.id ?? loggedUserRole?.roleId
16 });
17
18 const handleChange = name => event => {
19 setFormData({...formData, [name]: event.target.value});
20 };
21
22 const handleSubmit = async event => {
23 event.preventDefault();
24 if (vehicle) {
25 await UpdateVehicle(vehicle.id, formData)
26 navigate(location.pathname);
27 onClose();
28
29 return;
30 }
31
32 await CreateVehicle(formData)
33 navigate(location.pathname);
34 }
35
36 return (<form onSubmit={handleSubmit}>
37 <Grid container spacing={2}>
38 <Grid item xs={12}>
39 <TextField
40 onChange={handleChange("tip")}
41 name="ime"
42 variant="outlined"
43 required
44 fullWidth
45 label="Ime"
46 value={formData.tip}
47 inputProps={{
48 minLength: 2,
49 }}
50 />
51 </Grid>
52 <Grid item xs={12}>
53 <TextField
54 onChange={handleChange("registracija")}
55 variant="outlined"
56 required
57 fullWidth
58 label="Registracija"
59 name="registracija"
60 value={formData.registracija}
61 inputProps={{
62 minLength: 2,
63 }}
64 />
65 </Grid>
66 <Grid item>
67 <Button
68 type="submit"
69 fullWidth
70 variant="contained"
71 >
72 {vehicle ? "Edit" : "Create"}
73 </Button>
74 </Grid>
75 </Grid>
76 </form>)
77}
78
79export default DriverVehicleForm;
Note: See TracBrowser for help on using the repository browser.