-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
83 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,81 @@ | ||
import GoogleService from '../utils/GoogleService'; | ||
const googleService = new GoogleService(); | ||
const browserGeolocation = navigator.geolocation | ||
|
||
export const REQUEST_GEOLOCATION = 'REQUEST_GEOLOCATION' | ||
export const RECEIVE_GEOLOCATION = 'RECEIVE_GEOLOCATION' | ||
export const REQUEST_USER_LOCATION = 'RECEIVE_USER_LOCATION' | ||
export const RECEIVE_USER_LOCATION = 'RECEIVE_USER_LOCATION' | ||
export const REQUEST_LOCATION = 'REQUEST_LOCATION' | ||
export const RECEIVE_LOCATION = 'RECEIVE_LOCATION' | ||
export const RECEIVE_LOCATION_ERROR = 'RECEIVE_LOCATION_ERROR' | ||
|
||
export function requestGeolocation() { | ||
export function requestLocation() { | ||
return { | ||
type: REQUEST_GEOLOCATION | ||
type: REQUEST_LOCATION | ||
} | ||
} | ||
|
||
export function receiveGeolocation(coordinates) { | ||
export function receiveLocation(coordinates) { | ||
return { | ||
type: RECEIVE_GEOLOCATION, | ||
type: RECEIVE_LOCATION, | ||
coordinates | ||
} | ||
} | ||
|
||
// This makes a request to the geoli | ||
export function receiveLocationError(err) { | ||
return { | ||
type: RECEIVE_LOCATION, | ||
err: err | ||
} | ||
} | ||
|
||
/** | ||
* Uses browser to find current coordinates (lat, lng) | ||
*/ | ||
export function fetchGeolocation() { | ||
return (dispatch) => { | ||
dispatch(requestGeolocation()) | ||
navigator.geolocation.getCurrentPosition( | ||
dispatch(requestLocation()) | ||
|
||
// Check localStorage | ||
let cachedLocation = localStorage.getItem('canilivehere-geolocation') | ||
if (cachedLocation) { | ||
cachedLocation = JSON.parse(cachedLocation); | ||
return dispatch(receiveLocation(cachedLocation)); | ||
} | ||
|
||
browserGeolocation.getCurrentPosition( | ||
(position) => { | ||
// Successful geolocation lookup | ||
let coords = position.coords | ||
dispatch(receiveGeolocation(coords)); | ||
}, | ||
(error) => { | ||
// Failed geolocation lookup | ||
/* | ||
let defaultCoordinates = { | ||
latitude: 40.748, | ||
longitude: -73.985 | ||
let coords = { | ||
latitude: position.coords.latitude, | ||
longitude: position.coords.longitude | ||
} | ||
dispatch(receiveGeolocation(defaultCoordinates)); | ||
*/ | ||
|
||
localStorage.setItem('canilivehere-geolocation', JSON.stringify(coords)); | ||
dispatch(receiveLocation(coords)); | ||
}, | ||
(err) => { | ||
dispatch(receiveLocationError(err)) | ||
} | ||
); | ||
} | ||
} | ||
|
||
export function requestUserLocation() { | ||
return { | ||
type: REQUEST_USER_LOCATION | ||
} | ||
} | ||
|
||
export function receiveUserLocation(coordinates) { | ||
return { | ||
type: RECEIVE_USER_LOCATION, | ||
coordinates | ||
} | ||
} | ||
|
||
/** | ||
* This function takes address string and gets its coordinates (lat, lng) | ||
* from Google. | ||
* | ||
* @param {String} addressText The string to search for coordinates | ||
*/ | ||
export function fetchUserLocation(addressText) { | ||
return (dispatch) => { | ||
dispatch(requestUserLocation()) | ||
dispatch(requestLocation()) | ||
return googleService.getCoordinatesFromAddress(addressText) | ||
.then((response) => { | ||
let coordinates = { | ||
latitude: response[0].geometry.location.lat(), | ||
longitude: response[0].geometry.location.lng() | ||
} | ||
return dispatch(receiveGeolocation(coordinates)) | ||
return dispatch(receiveLocation(coordinates)) | ||
}) | ||
.catch((err) => { | ||
|
||
dispatch(receiveLocationError(err)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { REQUEST_LOCATION, RECEIVE_LOCATION } from '../actions/LocationActions' | ||
|
||
const defaultLocation = { | ||
center: { | ||
lat: 40.748, | ||
lng: -73.985 | ||
} | ||
} | ||
|
||
const location = (state=defaultLocation, action) => { | ||
switch (action.type) { | ||
case REQUEST_LOCATION: | ||
return { | ||
...state | ||
} | ||
case RECEIVE_LOCATION: | ||
return { | ||
...state, | ||
center: { | ||
lat: action.coordinates.latitude, | ||
lng: action.coordinates.longitude | ||
} | ||
} | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
export default location |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
//const emitter = require('event-emitter'); | ||
const google = window.google; | ||
|
||
class GoogleService { | ||
|