[364f27d] | 1 | import React from "react";
|
---|
| 2 | import { useLocation, useNavigate } from 'react-router-dom'
|
---|
| 3 | import CenteredContainer from "../UtilComponents/CenteredContainer";
|
---|
| 4 | import axios from "../../custom-axios/axios"
|
---|
| 5 | import { MapContainer, TileLayer, Marker, Popup } 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 RoutingMachine from "./routineMachine";
|
---|
| 10 | import '../UtilComponents/App.css'
|
---|
| 11 |
|
---|
| 12 | const StartedDrive = () => {
|
---|
| 13 | const location = useLocation();
|
---|
| 14 | const navigate = useNavigate();
|
---|
| 15 |
|
---|
| 16 | const distanceToTravel = getDistance([location.state.startLatitude, location.state.startLongitude], [location.state.startedDrive.destinationLatitude, location.state.startedDrive.destinationLongitude])
|
---|
| 17 |
|
---|
| 18 | const finishDrive = () => {
|
---|
| 19 | const driveId = location.state.startedDrive.id
|
---|
| 20 | axios.post(`/drive/finish/${driveId}`, null, { params: {
|
---|
| 21 | kmTravelled: distanceToTravel
|
---|
| 22 | }
|
---|
| 23 | })
|
---|
| 24 | navigate("/home");
|
---|
| 25 | }
|
---|
| 26 | const gradeDrive = () => {
|
---|
| 27 | const driveId = location.state.startedDrive.id
|
---|
| 28 | navigate("/grade-drive", {state: {driveId: driveId}})
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | const payDrive = async () => {
|
---|
| 32 | const driverId = location.state.startedDrive.driver.id
|
---|
| 33 | const requestId = location.state.startedDrive.request.id
|
---|
| 34 | const driverResponse = await axios.get(`/driver/${driverId}`);
|
---|
| 35 | const startedDriveResponse = await axios.get(`/drive/request/${requestId}`)
|
---|
| 36 | const driverPricePerKm = driverResponse.data.pricePerKm
|
---|
| 37 | const kmTravelled = distanceToTravel
|
---|
| 38 | const totalSumToPay = driverPricePerKm * kmTravelled
|
---|
| 39 | navigate("/pay-drive", {state: {driveId: startedDriveResponse.data.id,
|
---|
| 40 | driverPricePerKm: driverPricePerKm,
|
---|
| 41 | totalSumToPay: totalSumToPay,
|
---|
| 42 | kmTravelled: kmTravelled}})
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | let finishDriveButton = <a title={"Finish Drive"} className={"myButton btn btn-primary"}
|
---|
| 46 | style={{backgroundColor: "darkcyan", borderColor: "black", color: "white"}}
|
---|
| 47 | onClick={() => {finishDrive()}}>
|
---|
| 48 | Finish Drive</a>
|
---|
| 49 |
|
---|
| 50 | let gradeButton = <a title={"Grade Drive"} className={"myButton btn btn-primary"}
|
---|
| 51 | style={{backgroundColor: "darkcyan", borderColor: "black", color: "white"}}
|
---|
| 52 | onClick={() => {gradeDrive()}}>
|
---|
| 53 | Grade Driver for Drive</a>
|
---|
| 54 |
|
---|
| 55 | let payForDrive = <a title={"Pay Drive"} className={"myButton btn btn-primary"}
|
---|
| 56 | style={{backgroundColor: "darkcyan", borderColor: "black", color: "white"}}
|
---|
| 57 | onClick={() => {payDrive()}}>
|
---|
| 58 | Pay Drive</a>
|
---|
| 59 | let grade = <h5 className="card-title">Grade: {location.state.startedDrive.grade}</h5>
|
---|
| 60 |
|
---|
| 61 | let buttonOne;
|
---|
| 62 | let buttonTwo;
|
---|
| 63 | let showGrade;
|
---|
| 64 |
|
---|
| 65 | if(localStorage.getItem("driverId")){
|
---|
| 66 | buttonOne = finishDriveButton;
|
---|
| 67 | } else if(localStorage.getItem("passengerId")) {
|
---|
| 68 | buttonOne = payForDrive;
|
---|
| 69 | showGrade = grade;
|
---|
| 70 | if(location.state.startedDrive.grade === 0)
|
---|
| 71 | buttonTwo = gradeButton;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | function getDistance(origin, destination) {
|
---|
| 75 | let lon1 = toRadian(origin[1]),
|
---|
| 76 | lat1 = toRadian(origin[0]),
|
---|
| 77 | lon2 = toRadian(destination[1]),
|
---|
| 78 | lat2 = toRadian(destination[0]);
|
---|
| 79 |
|
---|
| 80 | let deltaLat = lat2 - lat1;
|
---|
| 81 | let deltaLon = lon2 - lon1;
|
---|
| 82 |
|
---|
| 83 | let a = Math.pow(Math.sin(deltaLat/2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(deltaLon/2), 2);
|
---|
| 84 | let c = 2 * Math.asin(Math.sqrt(a));
|
---|
| 85 | let EARTH_RADIUS = 6371;
|
---|
| 86 | let distanceInKm = (c * EARTH_RADIUS);
|
---|
| 87 | return Math.round((distanceInKm + Number.EPSILON) * 100) / 100
|
---|
| 88 | }
|
---|
| 89 | function toRadian(degree) {
|
---|
| 90 | return degree*Math.PI/180;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | return (
|
---|
| 94 | <CenteredContainer style={{width: 'calc(750px - 50vw)', minWidth:'80%', maxWidth: '100%', margin: 'auto'}}>
|
---|
| 95 | <div className="card text-center">
|
---|
| 96 | <div className="card-header">
|
---|
| 97 | {new Intl.DateTimeFormat('en-GB', { dateStyle: 'full', timeStyle: 'medium'})
|
---|
| 98 | .format(new Date(location.state.startedDrive.startTime))}
|
---|
| 99 | <br></br>
|
---|
| 100 | {location.state.startedDrive.status}
|
---|
| 101 | </div>
|
---|
| 102 | <div className="card-body">
|
---|
| 103 | {showGrade}
|
---|
| 104 | <h5> Kilometers to travel: {distanceToTravel} km</h5>
|
---|
| 105 | <MapContainer className="border border-info rounded-2" center={[41.9943, 21.4309]} zoom={12} scrollWheelZoom={true} style={{width: '100%', position: 'relative', zIndex: 0}}>
|
---|
| 106 | <TileLayer
|
---|
| 107 | attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
---|
| 108 | url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
---|
| 109 | />
|
---|
| 110 | <Marker position={[location.state.startedDrive.destinationLatitude, location.state.startedDrive.destinationLongitude]} icon={new Icon({iconUrl: markerIconPng, iconSize: [25, 41], iconAnchor: [12, 41]})}>
|
---|
| 111 | <Popup position={[location.state.startedDrive.destinationLatitude, location.state.startedDrive.destinationLongitude]}>
|
---|
| 112 | <p>Passenger's destination address</p>
|
---|
| 113 | </Popup>
|
---|
| 114 | </Marker>
|
---|
| 115 | <Marker position={[location.state.startLatitude, location.state.startLongitude]} icon={new Icon({iconUrl: markerIconPng, iconSize: [25, 41], iconAnchor: [12, 41]})}>
|
---|
| 116 | <Popup position={[location.state.startLatitude, location.state.startLongitude]}>
|
---|
| 117 | <p>You started your drive here.</p>
|
---|
| 118 | </Popup>
|
---|
| 119 | </Marker>
|
---|
| 120 | <RoutingMachine latitudes={{startLat: location.state.startLatitude, startLng: location.state.startLongitude,
|
---|
| 121 | destLat:location.state.startedDrive.destinationLatitude, destLng:location.state.startedDrive.destinationLongitude }}/>
|
---|
| 122 | </MapContainer>
|
---|
| 123 | <h3 style={{color: "green"}}>{location.state.totalSumToPay}</h3>
|
---|
| 124 | {buttonOne}
|
---|
| 125 | <br></br>
|
---|
| 126 | <hr></hr>
|
---|
| 127 | {buttonTwo}
|
---|
| 128 | </div>
|
---|
| 129 | </div>
|
---|
| 130 | </CenteredContainer>
|
---|
| 131 | )
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | export default StartedDrive; |
---|