source: RebuMKReact/src/Components/Requests/makeRequest.js

Last change on this file was 364f27d, checked in by MetodiMladenovski <meto.18@…>, 14 months ago

added projects and db scripts

  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[364f27d]1import React, { useState } from "react";
2import { useNavigate, useLocation} from "react-router-dom";
3import CenteredContainer from "../UtilComponents/CenteredContainer";
4import axios from "../../custom-axios/axios";
5import { MapContainer, TileLayer, Marker, Popup, useMapEvents } from 'react-leaflet'
6import 'leaflet/dist/leaflet.css';
7import markerIconPng from "leaflet/dist/images/marker-icon.png"
8import {Icon} from 'leaflet'
9import '../UtilComponents/App.css'
10
11const MakeRequest = () => {
12
13 const location = useLocation();
14 // eslint-disable-next-line react-hooks/rules-of-hooks
15 const navigate = useNavigate();
16 // eslint-disable-next-line react-hooks/rules-of-hooks
17 const [formData, updateFormData] = React.useState({
18 cityAddress : "",
19 streetAddress : "",
20 numberAddress : ""
21 })
22 const [latitude, setLatitude] = useState(null)
23 const [longitude, setLongitude] = useState(null)
24 const [position, setPosition] = useState(null)
25
26 const handleChange = (e) => {
27 updateFormData({
28 ...formData,
29 [e.target.name]: e.target.value.trim()
30 })
31 }
32
33 const onFormSubmit = async (e) => {
34 e.preventDefault();
35 const cityAddress = formData.cityAddress;
36 const streetAddress = formData.streetAddress;
37 const numberAddress = formData.numberAddress;
38 const passengerId = localStorage.getItem("passengerId");
39 let chosenDriverId = null
40 if(location.state){
41 chosenDriverId = location.state.driverId
42 }
43 if(!(latitude && longitude)){
44 alert("You must pick a location on the map")
45 }
46 const response = await axios.post(`/request/make/${passengerId}`, {
47 "cityAddress" : cityAddress,
48 "streetAddress" : streetAddress,
49 "numberAddress" : numberAddress,
50 "latitude" : latitude,
51 "longitude" : longitude,
52 "chosenDriverId" : chosenDriverId
53 })
54 localStorage.setItem("madeRequestId", response.data.id)
55 navigate("/made-request", {state: {madeRequest: response.data}})
56 }
57
58 let showChosenDriver
59 if(location.state){
60 showChosenDriver = <p style={{color: "green", fontStyle: "bold"}}>You picked the driver with name {location.state.driverName}.
61 <br></br>
62 Add your address to complete the request.</p>
63 }
64
65 function LocationMarker() {
66 const map = useMapEvents({
67 dblclick(){
68 map.locate()
69 },
70 click(e) {
71 setLatitude(e.latlng.lat)
72 setLongitude(e.latlng.lng)
73 setPosition(e.latlng)
74 map.flyTo(e.latlng, map.getZoom())
75 },
76 locationfound(e) {
77 setPosition(e.latlng)
78 map.flyTo(e.latlng, map.getZoom())
79 }
80 })
81 return position === null ? null : (
82 <Marker position={position} icon={new Icon({iconUrl: markerIconPng, iconSize: [25, 41], iconAnchor: [12, 41]})}>
83 <Popup>You are here</Popup>
84 </Marker>
85 )
86 }
87
88 return(
89 <CenteredContainer>
90 <h2 style={{textAlign: "center", color: "darkcyan"}}>Make Request</h2>
91 <hr></hr>
92 {showChosenDriver}
93 <br></br>
94 <h6 style={{textAlign: "center"}}>Your current(pick up) address:</h6>
95 <br></br>
96 <form onSubmit={onFormSubmit}>
97 <div className="form-group">
98 <label htmlFor="cityAddress">City</label>
99 <input type="text"
100 className="form-control"
101 id="cityAddress"
102 name="cityAddress"
103 required
104 placeholder="E.g: Skopje"
105 style={{height: 100 + "%"}}
106 onChange={handleChange}
107 />
108 </div>
109 <br></br>
110 <div className="form-group">
111 <label htmlFor="streetAddress">Street</label>
112 <input type="text"
113 className="form-control"
114 id="streetAddress"
115 name="streetAddress"
116 required
117 placeholder="E.g: Jordan Mijalkov"
118 style={{height: 100 + "%"}}
119 onChange={handleChange}
120 />
121 </div>
122 <br></br>
123 <div className="form-group">
124 <label htmlFor="numberAddress">Number</label>
125 <input type="number"
126 className="form-control"
127 id="numberAddress"
128 name="numberAddress"
129 placeholder="E.g: 2"
130 required
131 onChange={handleChange}
132 />
133 </div>
134 <br></br>
135 <label htmlFor="MapContainer" style={{color: 'red'}}>Choose your location on the map</label>
136 <label htmlFor="MapContainer" style={{color: 'darkcyan'}}>You can double click for the app to try and locate you on it's own,
137 or click once to mark your precise location</label>
138 <MapContainer
139 className="border border-info rounded-2" center={[41.9943, 21.4309]}
140 style={{width: '100%', position:'relative', zIndex: 0}}
141 zoom={13}
142 scrollWheelZoom={true}>
143 <TileLayer
144 attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
145 url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
146 />
147 <LocationMarker />
148 </MapContainer>
149 <br></br>
150 <p style={{textAlign: 'center'}}>
151 <button id="submit" type="submit" className="myButton btn btn-primary" style={{backgroundColor: "cyan", borderColor: "black", color: 'black'}}>Submit</button>
152 </p>
153 </form>
154 </CenteredContainer>
155 )
156
157}
158
159export default MakeRequest;
Note: See TracBrowser for help on using the repository browser.