import React from "react"; import { useLocation, useNavigate } from 'react-router-dom' import CenteredContainer from "../UtilComponents/CenteredContainer"; import axios from "../../custom-axios/axios" import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet' import 'leaflet/dist/leaflet.css'; import markerIconPng from "leaflet/dist/images/marker-icon.png" import {Icon} from 'leaflet' import RoutingMachine from "./routineMachine"; import '../UtilComponents/App.css' const StartedDrive = () => { const location = useLocation(); const navigate = useNavigate(); const distanceToTravel = getDistance([location.state.startLatitude, location.state.startLongitude], [location.state.startedDrive.destinationLatitude, location.state.startedDrive.destinationLongitude]) const finishDrive = () => { const driveId = location.state.startedDrive.id axios.post(`/drive/finish/${driveId}`, null, { params: { kmTravelled: distanceToTravel } }) navigate("/home"); } const gradeDrive = () => { const driveId = location.state.startedDrive.id navigate("/grade-drive", {state: {driveId: driveId}}) } const payDrive = async () => { const driverId = location.state.startedDrive.driver.id const requestId = location.state.startedDrive.request.id const driverResponse = await axios.get(`/driver/${driverId}`); const startedDriveResponse = await axios.get(`/drive/request/${requestId}`) const driverPricePerKm = driverResponse.data.pricePerKm const kmTravelled = distanceToTravel const totalSumToPay = driverPricePerKm * kmTravelled navigate("/pay-drive", {state: {driveId: startedDriveResponse.data.id, driverPricePerKm: driverPricePerKm, totalSumToPay: totalSumToPay, kmTravelled: kmTravelled}}) } let finishDriveButton = {finishDrive()}}> Finish Drive let gradeButton = {gradeDrive()}}> Grade Driver for Drive let payForDrive = {payDrive()}}> Pay Drive let grade =
Grade: {location.state.startedDrive.grade}
let buttonOne; let buttonTwo; let showGrade; if(localStorage.getItem("driverId")){ buttonOne = finishDriveButton; } else if(localStorage.getItem("passengerId")) { buttonOne = payForDrive; showGrade = grade; if(location.state.startedDrive.grade === 0) buttonTwo = gradeButton; } function getDistance(origin, destination) { let lon1 = toRadian(origin[1]), lat1 = toRadian(origin[0]), lon2 = toRadian(destination[1]), lat2 = toRadian(destination[0]); let deltaLat = lat2 - lat1; let deltaLon = lon2 - lon1; let a = Math.pow(Math.sin(deltaLat/2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(deltaLon/2), 2); let c = 2 * Math.asin(Math.sqrt(a)); let EARTH_RADIUS = 6371; let distanceInKm = (c * EARTH_RADIUS); return Math.round((distanceInKm + Number.EPSILON) * 100) / 100 } function toRadian(degree) { return degree*Math.PI/180; } return (
{new Intl.DateTimeFormat('en-GB', { dateStyle: 'full', timeStyle: 'medium'}) .format(new Date(location.state.startedDrive.startTime))}

{location.state.startedDrive.status}
{showGrade}
Kilometers to travel: {distanceToTravel} km

Passenger's destination address

You started your drive here.

{location.state.totalSumToPay}

{buttonOne}


{buttonTwo}
) } export default StartedDrive;