Skip to content

Commit

Permalink
cleaned up code everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
payaaam committed Mar 20, 2017
1 parent d2629c8 commit 698363a
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 86 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
},
"dependencies": {
"antd": "^2.5.0",
"event-emitter": "^0.3.5",
"humps": "^1.1.0",
"lodash": "^4.16.1",
"normalizr": "^2.2.1",
Expand Down
85 changes: 46 additions & 39 deletions src/lib/actions/LocationActions.js
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))
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import React, { Component } from 'react'
import { connect } from 'react-redux'
import { fetchUserLocation } from '../actions/LocationActions'
import '../../stylesheets/components/location-selector.scss'
const google = window.google;

class LocationSelect extends Component {
class LocationSearch extends Component {

state = {
currentInputValue: '',
Expand All @@ -13,18 +12,13 @@ class LocationSelect extends Component {

componentWillMount() {}

componentDidMount() {
this.geocoder = new google.maps.Geocoder;
}
componentDidMount() {}

onSubmit(evt) {
evt.preventDefault();
let currentSearchText = this.state.currentInputValue;
this.state.recentSearches.push(currentSearchText);
this.props.dispatch(fetchUserLocation(currentSearchText))
// Validation
// Save location to recent searches
// Search for new location
}

handleInputChange(evt) {
Expand All @@ -34,7 +28,6 @@ class LocationSelect extends Component {
}

render() {
const children = this.props.children;
return (
<div className="location-box">
<div className="site-title">
Expand All @@ -49,4 +42,4 @@ class LocationSelect extends Component {
}
}

export default connect()(LocationSelect)
export default connect()(LocationSearch)
7 changes: 4 additions & 3 deletions src/lib/containers/MapContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { connect } from 'react-redux'
import ChipotleMap from '../components/ChipotleMap'
import { fetchGeolocation } from '../actions/LocationActions'
import Loading from '../components/Loading'
import LocationSelect from '../components/LocationSelect'
import LocationSearch from '../components/LocationSearch'

import '../../stylesheets/components/chipotle-map.scss'


class MapContainer extends Component {

componentWillMount() {
this.props.dispatch(fetchGeolocation())
this.props.dispatch(fetchGeolocation());
}

componentDidMount() {}
Expand All @@ -29,7 +30,7 @@ class MapContainer extends Component {
<div style={{ height: `100%` }} />
}
/>
<LocationSelect />
<LocationSearch />
</div>
)
}
Expand Down
29 changes: 29 additions & 0 deletions src/lib/reducers/LocationReducer.js
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
33 changes: 1 addition & 32 deletions src/lib/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
import { routerReducer as routing } from 'react-router-redux'
import { combineReducers } from 'redux'
import { REQUEST_GEOLOCATION, RECEIVE_GEOLOCATION } from '../actions/LocationActions'

const defaultReducer = (state={}, action) => {
return state
}

const defaultLocation = {
center: {
lat: 40.748,
lng: -73.985
}
}

const location = (state=defaultLocation, action) => {
switch (action.type) {
case REQUEST_GEOLOCATION:
return {
...state
}
case RECEIVE_GEOLOCATION:
return {
...state,
center: {
lat: action.coordinates.latitude,
lng: action.coordinates.longitude
}
}
default:
return state
}
}
import location from './LocationReducer'

const rootReducer = combineReducers({
defaultReducer,
routing,
location
})
Expand Down
1 change: 0 additions & 1 deletion src/lib/utils/GoogleService.js
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 {
Expand Down

0 comments on commit 698363a

Please sign in to comment.