1 | import React, { useState } from "react";
|
---|
2 | import { useNavigate, useLocation } from 'react-router-dom'
|
---|
3 | import axios from "../../custom-axios/axios";
|
---|
4 | import CenteredContainer from "../UtilComponents/CenteredContainer";
|
---|
5 | import { MapContainer, TileLayer, Marker, Popup, useMapEvents } from 'react-leaflet'
|
---|
6 | import 'leaflet/dist/leaflet.css';
|
---|
7 | import markerIconPng from "leaflet/dist/images/marker-icon.png";
|
---|
8 | import {Icon} from 'leaflet';
|
---|
9 | import '../UtilComponents/App.css';
|
---|
10 |
|
---|
11 |
|
---|
12 | const ConfirmedRequest = (props) => {
|
---|
13 | const location = useLocation();
|
---|
14 | const navigate = useNavigate();
|
---|
15 |
|
---|
16 | const [destinationLatitude, setLatitude] = useState(null)
|
---|
17 | const [destinationLongitude, setLongitude] = useState(null)
|
---|
18 | const [position, setPosition] = useState(null)
|
---|
19 |
|
---|
20 | const startDrive = async () => {
|
---|
21 | const requestId = location.state.confirmedRequest.id;
|
---|
22 | const driverId = localStorage.getItem("driverId");
|
---|
23 | if(!(destinationLatitude && destinationLongitude)){
|
---|
24 | alert("Must choose destination address for passenger on the map.");
|
---|
25 | return;
|
---|
26 | }
|
---|
27 | const response = await axios.post(`/drive/start/${requestId}/${driverId}`, {
|
---|
28 | "destinationLatitude" : destinationLatitude,
|
---|
29 | "destinationLongitude" : destinationLongitude
|
---|
30 | })
|
---|
31 | const startLatitude = location.state.confirmedRequest.latitude;
|
---|
32 | const startLongitude = location.state.confirmedRequest.longitude;
|
---|
33 | props.onRefreshPassengersMadeRequest();
|
---|
34 | navigate('/started-drive', {state: {startedDrive: response.data, startLatitude: startLatitude, startLongitude: startLongitude}})
|
---|
35 | }
|
---|
36 |
|
---|
37 | function LocationMarker() {
|
---|
38 | const map = useMapEvents({
|
---|
39 | dblclick(){
|
---|
40 | map.locate()
|
---|
41 | },
|
---|
42 | click(e) {
|
---|
43 | setLatitude(e.latlng.lat)
|
---|
44 | setLongitude(e.latlng.lng)
|
---|
45 | setPosition(e.latlng)
|
---|
46 | map.flyTo(e.latlng, map.getZoom())
|
---|
47 | },
|
---|
48 | locationfound(e) {
|
---|
49 | setPosition(e.latlng)
|
---|
50 | map.flyTo(e.latlng, map.getZoom())
|
---|
51 | }
|
---|
52 | })
|
---|
53 | return position === null ? null : (
|
---|
54 | <Marker position={position} icon={new Icon({iconUrl: markerIconPng, iconSize: [25, 41], iconAnchor: [12, 41]})}>
|
---|
55 | <Popup><p>This is the passenger's
|
---|
56 | <br></br>
|
---|
57 | destination location.</p></Popup>
|
---|
58 | </Marker>
|
---|
59 | )
|
---|
60 | }
|
---|
61 |
|
---|
62 | return (
|
---|
63 | <CenteredContainer style={{width: 'calc(750px - 50vw)', minWidth:'80%', maxWidth: '100%', margin: 'auto'}}>
|
---|
64 | <div className="card text-center">
|
---|
65 | <div className="card-header">
|
---|
66 | {location.state.confirmedRequest.status}
|
---|
67 | </div>
|
---|
68 | <div className="card-body">
|
---|
69 | <h5 className="card-title">{location.state.confirmedRequest.cityAddress}, {location.state.confirmedRequest.streetAddress}, {location.state.confirmedRequest.numberAddress}</h5>
|
---|
70 | <p className="card-text">Passenger: {location.state.confirmedRequest.passenger.name} {location.state.confirmedRequest.passenger.surname}</p>
|
---|
71 | <br></br>
|
---|
72 | <label htmlFor="MapContainer" style={{color: 'red'}}>After picking your passenger,
|
---|
73 | pick the destination location on the map where the passenger wants to go</label>
|
---|
74 | <MapContainer className="border border-info rounded-2" center={[location.state.confirmedRequest.latitude, location.state.confirmedRequest.longitude]} zoom={14} scrollWheelZoom={true} style={{position: 'relative', zIndex: 0, marginBottom: '20px'}}>
|
---|
75 | <TileLayer
|
---|
76 | attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
---|
77 | url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
---|
78 | />
|
---|
79 | <Marker position={[location.state.confirmedRequest.latitude, location.state.confirmedRequest.longitude]} icon={new Icon({iconUrl: markerIconPng, iconSize: [25, 41], iconAnchor: [12, 41]})}>
|
---|
80 | <Popup position={[location.state.confirmedRequest.latitude, location.state.confirmedRequest.longitude]}>
|
---|
81 | <p>Pick your passenger
|
---|
82 | <br></br>
|
---|
83 | {location.state.confirmedRequest.passenger.name} {location.state.confirmedRequest.passenger.surname} from here.</p>
|
---|
84 | </Popup>
|
---|
85 | <LocationMarker />
|
---|
86 | </Marker>
|
---|
87 | </MapContainer>
|
---|
88 | <button id="submit" title={"Start Drive"} className={"myButton btn btn-danger"} type="submit"
|
---|
89 | style={{backgroundColor: "cyan", borderColor: "black", color: 'black'}} onClick={() => startDrive()}>
|
---|
90 | Start Drive
|
---|
91 | </button>
|
---|
92 | </div>
|
---|
93 | </div>
|
---|
94 | </CenteredContainer>
|
---|
95 | )
|
---|
96 | }
|
---|
97 |
|
---|
98 | export default ConfirmedRequest; |
---|