[e6c2521] | 1 | import React, {useState} from "react";
|
---|
| 2 | import {Form, Button, Container, Row, Col} from "react-bootstrap";
|
---|
| 3 | import {GiConfirmed} from "react-icons/gi";
|
---|
| 4 | import useFormData from "../Hooks/useFormData";
|
---|
| 5 | import useCreateTrip from "../Hooks/Transport/useCreateTrip";
|
---|
| 6 | import {AiOutlinePlusCircle} from "react-icons/ai";
|
---|
| 7 | import useFormNested from "../useFormNested";
|
---|
| 8 |
|
---|
| 9 | const 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++) {
|
---|
[07f4e8b] | 234 | console.log(wayPoints[i])
|
---|
[e6c2521] | 235 | wayPointNames.push(wayPoints[i].value);
|
---|
| 236 | }
|
---|
| 237 | wayPointNames.push(formData.to);
|
---|
| 238 |
|
---|
| 239 | setFormData({
|
---|
| 240 | ...formData,
|
---|
| 241 | routeWaypointLocation: wayPointNames,
|
---|
| 242 | });
|
---|
| 243 |
|
---|
| 244 | let routesVar = [];
|
---|
| 245 | let routesForm = {};
|
---|
| 246 | let count = 0;
|
---|
| 247 | for (let i = 0; i < wayPointNames.length; i++) {
|
---|
| 248 | for (let j = i + 1; j < wayPointNames.length; j++) {
|
---|
| 249 | routesForm = {
|
---|
| 250 | ...routesForm, [count++]: {
|
---|
| 251 | from: wayPointNames[i],
|
---|
| 252 | to: wayPointNames[j],
|
---|
| 253 | price: 0,
|
---|
| 254 | departure: new Date().getDate(),
|
---|
| 255 | arrival: new Date().getDate(),
|
---|
| 256 | freeSpace: 0,
|
---|
| 257 | order: i
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | routesVar.push({
|
---|
| 262 | from: wayPointNames[i],
|
---|
| 263 | to: wayPointNames[j],
|
---|
| 264 | price: 0,
|
---|
| 265 | departure: Date(),
|
---|
| 266 | arrival: Date(),
|
---|
| 267 | freeSpace: 0,
|
---|
| 268 | order: i
|
---|
| 269 | })
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | for (let i = 0; i < Object.values(routesForm).length; i++) {
|
---|
| 274 | setDependencies(prevState => [...prevState, findDependantRoutes(wayPointNames, routesForm, i)])
|
---|
| 275 | }
|
---|
| 276 | routesSetFormData(routesForm)
|
---|
| 277 | setRoutes(routesVar)
|
---|
| 278 | setRouteCol(true)
|
---|
| 279 | props.setSize();
|
---|
| 280 | }}
|
---|
| 281 | >
|
---|
| 282 | <span className="ikona my-1">
|
---|
| 283 | <GiConfirmed/>
|
---|
| 284 | </span>
|
---|
| 285 | <span className="ikona mx-3">Заврши внес на рути</span>
|
---|
| 286 | </Button>
|
---|
| 287 | </Form.Group>
|
---|
| 288 | <Form.Group className="my-1 justify-content-center">
|
---|
| 289 | <Button
|
---|
| 290 | type="submit"
|
---|
| 291 | style={{backgroundColor: "#159895"}}
|
---|
| 292 | size="md"
|
---|
| 293 | onClick={(e) => {
|
---|
| 294 | e.preventDefault();
|
---|
| 295 | const dataToSend = {
|
---|
| 296 | ...formData,
|
---|
| 297 | routes: Object.values(routesFormData)
|
---|
| 298 | }
|
---|
| 299 | console.log(dataToSend)
|
---|
| 300 | createTrip(dataToSend)
|
---|
| 301 | props.refresh((prev) => {
|
---|
| 302 | return prev + 1;
|
---|
| 303 | })
|
---|
| 304 | }}
|
---|
| 305 | >
|
---|
| 306 | <span className="ikona my-1">
|
---|
| 307 | <GiConfirmed/>
|
---|
| 308 | </span>
|
---|
| 309 | <span className="ikona mx-3">Поднеси апликација</span>
|
---|
| 310 | </Button>
|
---|
| 311 | </Form.Group>
|
---|
| 312 | </Row>
|
---|
| 313 | </Form>
|
---|
| 314 | </Container>
|
---|
| 315 | </>
|
---|
| 316 | );
|
---|
| 317 | };
|
---|
| 318 |
|
---|
| 319 | export default AddTripForm;
|
---|