Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fraser/add directions redux #33

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
/apiKey
6 changes: 6 additions & 0 deletions src/actions/addDirections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const addDirections = directions => {
return {
type: 'ADD_DIRECTIONS',
payload: directions
};
}
5 changes: 5 additions & 0 deletions src/actions/clearDirections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const clearDirections = ()=> {
return {
type: 'CLEAR_DIRECTIONS'
};
}
25 changes: 23 additions & 2 deletions src/components/top-components/RightTopContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, {useRef} from 'react'
import {GoogleMap, useJsApiLoader, DirectionsRenderer} from '@react-google-maps/api';
import Inputs from "./InputDiv";
import {Button} from "@mui/material";
import {useDispatch, useSelector} from "react-redux";
import {addDirections} from "../../actions/addDirections";

const containerStyle = {
display: "inline-flex",
Expand All @@ -16,15 +18,17 @@ const center = {




function MainMapComponent() {
const { isLoaded } = useJsApiLoader({
id: 'google-map-script',
googleMapsApiKey: "",
googleMapsApiKey: "AIzaSyDvwRJY_4ws-EBa5qaIpuH9H_McBjRIK7Q",
libraries: ['places']
})

const [map, setMap] = React.useState(/** @type google.maps.Map */(null));
const [directions, setDirections] = React.useState(null);
const dispatch = useDispatch();

/** @type React.MutableRefObject<HTMLInputElement> */
const originRef = useRef();
Expand All @@ -50,10 +54,27 @@ function MainMapComponent() {
origin: originRef.current.value,
destination: destRef.current.value,
// eslint-disable-next-line no-undef
travelMode: google.maps.TravelMode.DRIVING,
travelMode: google.maps.TravelMode.WALKING,
provideRouteAlternatives: true
}
);
console.log(results.routes);
let directionArray = [];
for (let i = 0; i < results.routes.length; i++) {
let leg = results.routes[i];
console.log(leg);
directionArray.push({
// distance in meters
distance: leg.legs[0].distance.value,
// duration in seconds
duration: leg.legs[0].duration.value,
// addresses are strings
startAddress: leg.legs[0].startAddress,
endAddress: leg.legs[0].endAddress,
})
//console.log(directionArray);
}
//dispatch(addDirections(directionArray));
setDirections(results);
}

Expand Down
15 changes: 15 additions & 0 deletions src/reducers/directionsReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

const directionsReducer = (directionsArray = [], action) => {
switch(action.type) {
case 'ADD_DIRECTIONS':
directionsArray = action.payload;
return directionsArray;
case 'CLEAR_DIRECTIONS':
directionsArray = null;
return directionsArray;
default:
return directionsArray;
}
}

export default directionsReducer;
4 changes: 2 additions & 2 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {combineReducers} from 'redux';
import directionsReducer from './directionsReducer'
import {template,drawerState, loginState} from './reducer'


const rootReducer = combineReducers({
drawerState,loginState,
drawerState,loginState, directionsReducer,
template
});

Expand Down