source: frontend/src/Components/Forms/AddTripForm.js@ e6c2521

Last change on this file since e6c2521 was e6c2521, checked in by darsov2 <62809499+darsov2@…>, 6 months ago

images upload/download impl, other fixes

  • Property mode set to 100644
File size: 16.6 KB
Line 
1import React, {useState} from "react";
2import {Form, Button, Container, Row, Col} from "react-bootstrap";
3import {GiConfirmed} from "react-icons/gi";
4import useFormData from "../Hooks/useFormData";
5import useCreateTrip from "../Hooks/Transport/useCreateTrip";
6import {AiOutlinePlusCircle} from "react-icons/ai";
7import useFormNested from "../useFormNested";
8
9const AddTripForm = (props) => {
10 const {createTrip} = useCreateTrip(props.transportId);
11 console.log(props.transportId)
12 const [wayPoints, setWayPoints] = useState([]);
13 const [dependencies, setDependencies] = useState([]);
14 const [routeCount, setRouteCount] = useState(0);
15 const [showRouteCol, setRouteCol] = useState(false);
16 let wayPointNames
17 const findDependantRoutes = (routes, waypoints, i) => {
18
19 const dependency = []
20 console.log('OD fja')
21 console.log(Object.values(waypoints))
22 const objVal = Object.values(waypoints)
23 const searchingRoute = Object.values(waypoints)[i]
24 const fromIndex = routes.findIndex(x => searchingRoute.from === x)
25 const toIndex = routes.findIndex(x => searchingRoute.to === x)
26 for(let j = 0; j < objVal.length; j++)
27 {
28 const start = routes.findIndex(x => objVal[j].from === x)
29 const end = routes.findIndex(x => objVal[j].to === x)
30 if(start >= fromIndex && start < toIndex)
31 {
32 dependency.push(j)
33 }
34 else if(end >= toIndex && end < toIndex)
35 {
36 dependency.push(j)
37 }
38 else if(fromIndex >= start && fromIndex < end)
39 {
40 dependency.push(j)
41 }
42 }
43 return dependency;
44 }
45
46 const {formData, onFormChange, onCheckBoxChange, setFormData} = useFormData(
47 {
48 transport: "",
49 from: "",
50 to: "",
51 freeSpace: "",
52 date: new Date().getDate(),
53 time: new Date().getDate(),
54 routeWaypointLocation: [],
55 }
56 );
57
58 const {
59 formData: routesFormData,
60 onFormChange: routesFormChange,
61 setFormData: routesSetFormData
62 } = useFormNested({});
63
64 const [routes, setRoutes] = useState([]);
65
66 const newWaypoint = (
67 <>
68 <Form.Group className="mb-3" controlId="tripFrom">
69 <Form.Label>Попатна дестинација</Form.Label>
70 <Form.Control
71 type="text"
72 placeholder="Внесете го градот на поаѓање"
73 // value={formData.departureLocation}
74 // onChange={onFormChange}
75 name="routeWaypointLocation"
76 />
77 </Form.Group>
78 </>
79 );
80
81 return (
82 <>
83 <Container
84 className="rounded-5 m-5 my-auto mx-auto py-2 px-5"
85 style={{backgroundColor: "#ffffff"}}
86 >
87 <Form>
88 <Row>
89 <Row>
90 <Form.Group className="mb-3" controlId="tripFrom">
91 <Form.Label>Дестинација на поаѓање</Form.Label>
92 <Form.Control
93 type="text"
94 placeholder="Внесете го градот на поаѓање"
95 value={formData.from}
96 onChange={onFormChange}
97 name="from"
98 />
99 </Form.Group>
100
101 <Form.Group className="mb-3" controlId="tripTo">
102 <Form.Label>Дестинација на пристигање</Form.Label>
103 <Form.Control
104 type="text"
105 placeholder="Внесете го градот на пристигање"
106 value={formData.to}
107 onChange={onFormChange}
108 name="to"
109 />
110 </Form.Group>
111
112 <Form.Group className="mb-3" controlId="tripDate">
113 <Form.Label>Датум и време на поаѓање</Form.Label>
114 <Form.Control
115 type="datetime-local"
116 placeholder="Внесете го датумот на поаѓање"
117 value={formData.date}
118 onChange={onFormChange}
119 name="date"
120 />
121 </Form.Group>
122
123 <Form.Group className="mb-3" controlId="tripRoute">
124 <Form.Label className="d-flex justify-content-between align-items-center">
125 Попатни дестинации
126 <AiOutlinePlusCircle
127 onClick={() => {
128 const wayPoints = document.getElementsByName(
129 "routeWaypointLocation"
130 );
131 if (wayPoints.length == 0) {
132 setFormData({
133 ...formData,
134 routeWaypointLocation: [formData.from],
135 });
136 setRouteCount((prev) => {
137 return prev + 1;
138 });
139 console.log(formData);
140 }
141 if (
142 wayPoints.length == 0 ||
143 wayPoints[wayPoints.length - 1].value.length > 0
144 ) {
145 setRouteCount((prevState) => {
146 return prevState + 1;
147 });
148 console.log(routeCount);
149 setWayPoints((prevState) => {
150 return [...prevState, newWaypoint];
151 });
152 }
153 }}
154 id="add-route"
155 size={30}
156 color="#159895"
157 ></AiOutlinePlusCircle>
158 </Form.Label>
159 </Form.Group>
160 {!showRouteCol && wayPoints}
161 </Row>
162 {showRouteCol && console.log(routes)}
163 {showRouteCol && console.log(routesFormData)}
164 {dependencies !== [] && console.log(dependencies)}
165 {(showRouteCol && dependencies !== []) && (
166 routes.map((route, i) => {
167 return (
168 <Row>
169 <Form.Group className="mb-3" controlId="tripTo" key={i}>
170 <Row>
171 {/*{console.log(findDependantRoutes(routesFormData, wayPointNames, i))}*/}
172 <Col routeId={i}>
173 <Form.Label>Рута</Form.Label>
174 <Form.Control
175 type="text"
176 placeholder="Внесете го градот на пристигање"
177 value={route.from + " - " + route.to}
178 disabled
179 onChange={routesFormChange}
180 name="arrivalLocation"
181 />
182 </Col>
183 <Col routeId={i} dependantRoutes={dependencies[i].join(',')}>
184 <Form.Label>Слободни места</Form.Label>
185 <Form.Control
186 type="text"
187 placeholder="Внесете го бројот на слободни места"
188 value={routesFormData[i].freeSpace}
189 onChange={routesFormChange}
190 name="freeSpace"
191 />
192 </Col>
193 <Col routeId={i}>
194 <Form.Label>Време на пристигнување</Form.Label>
195 <Form.Control
196 type="datetime-local"
197 placeholder="Внесете го времето на пристигање"
198 value={routesFormData[i].arrival}
199 onChange={routesFormChange}
200 name="arrival"
201 />
202 </Col>
203 <Col routeId={i}>
204 <Form.Label>Цена</Form.Label>
205 <Form.Control
206 type="text"
207 placeholder="Внесете ја цената за рутата"
208 value={routesFormData[i].price}
209 onChange={routesFormChange}
210 name="price"
211 />
212 </Col>
213 </Row>
214 </Form.Group>
215 </Row>)
216 })
217 )}
218 </Row>
219 <Row>
220 <Form.Group className="my-1 justify-content-center">
221 <Button
222 type="button"
223 style={{backgroundColor: "#159895"}}
224 size="md"
225 onClick={() => {
226 document.getElementById("add-route").style.display = "none";
227
228 const wayPoints = document.getElementsByName(
229 "routeWaypointLocation"
230 );
231 wayPointNames = formData["routeWaypointLocation"];
232 console.log(wayPointNames);
233 for (let i = 0; i < routeCount - 1; i++) {
234 wayPointNames.push(wayPoints[i].value);
235 }
236 wayPointNames.push(formData.to);
237
238 setFormData({
239 ...formData,
240 routeWaypointLocation: wayPointNames,
241 });
242
243 let routesVar = [];
244 let routesForm = {};
245 let count = 0;
246 for (let i = 0; i < wayPointNames.length; i++) {
247 for (let j = i + 1; j < wayPointNames.length; j++) {
248 routesForm = {
249 ...routesForm, [count++]: {
250 from: wayPointNames[i],
251 to: wayPointNames[j],
252 price: 0,
253 departure: new Date().getDate(),
254 arrival: new Date().getDate(),
255 freeSpace: 0,
256 order: i
257 }
258 }
259
260 routesVar.push({
261 from: wayPointNames[i],
262 to: wayPointNames[j],
263 price: 0,
264 departure: Date(),
265 arrival: Date(),
266 freeSpace: 0,
267 order: i
268 })
269 }
270 }
271
272 for (let i = 0; i < Object.values(routesForm).length; i++) {
273 console.log(findDependantRoutes(wayPointNames, routesForm, i))
274 setDependencies(prevState => [...prevState, findDependantRoutes(wayPointNames, routesForm, i)])
275 }
276 console.log('VO STATEOOOO')
277 console.log(dependencies)
278 routesSetFormData(routesForm)
279 setRoutes(routesVar)
280 setRouteCol(true)
281 props.setSize();
282 }}
283 >
284 <span className="ikona my-1">
285 <GiConfirmed/>
286 </span>
287 <span className="ikona mx-3">Заврши внес на рути</span>
288 </Button>
289 </Form.Group>
290 <Form.Group className="my-1 justify-content-center">
291 <Button
292 type="submit"
293 style={{backgroundColor: "#159895"}}
294 size="md"
295 onClick={(e) => {
296 e.preventDefault();
297 const dataToSend = {
298 ...formData,
299 routes: Object.values(routesFormData)
300 }
301 console.log(dataToSend)
302 createTrip(dataToSend)
303 props.refresh((prev) => {
304 return prev + 1;
305 })
306 }}
307 >
308 <span className="ikona my-1">
309 <GiConfirmed/>
310 </span>
311 <span className="ikona mx-3">Поднеси апликација</span>
312 </Button>
313 </Form.Group>
314 </Row>
315 </Form>
316 </Container>
317 </>
318 );
319};
320
321export default AddTripForm;
Note: See TracBrowser for help on using the repository browser.