Skip to content

Commit

Permalink
Merge pull request #1 from luizguilhermefr/master
Browse files Browse the repository at this point in the history
New middleware action
  • Loading branch information
itinance authored May 29, 2017
2 parents 23f4f3c + 10a7e58 commit 2886707
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion actions.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { REDUX_SAGA_LOCATION_ACTION_SET_POSITION
, REDUX_SAGA_LOCATION_ACTION_SET_ERROR } from './actions/index';
, REDUX_SAGA_LOCATION_ACTION_SET_ERROR
, REDUX_SAGA_LOCATION_ACTION_REQUEST } from './actions/index';
1 change: 1 addition & 0 deletions actions/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const REDUX_SAGA_LOCATION_ACTION_SET_POSITION = 'REDUX_SAGA_LOCATION_SET_POSITION';
export const REDUX_SAGA_LOCATION_ACTION_SET_ERROR = 'REDUX_SAGA_LOCATION_SET_ERROR';
export const REDUX_SAGA_LOCATION_ACTION_REQUEST = 'REDUX_SAGA_LOCATION_ACTION_REQUEST';
13 changes: 12 additions & 1 deletion reducer/location.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import {REDUX_SAGA_LOCATION_ACTION_SET_POSITION
, REDUX_SAGA_LOCATION_ACTION_SET_ERROR} from '../actions';
, REDUX_SAGA_LOCATION_ACTION_SET_ERROR
, REDUX_SAGA_LOCATION_ACTION_REQUEST} from '../actions';

export default function locationReducer(state = {
position: null,
error: null,
fetching: false
}, action) {
switch(action.type) {

case REDUX_SAGA_LOCATION_ACTION_REQUEST: {
return {
...state,
fetching: true
}
}

case REDUX_SAGA_LOCATION_ACTION_SET_POSITION: {
const {position} = action;

return {
...state,
position,
error: null,
fetching: false
}
}

Expand All @@ -23,6 +33,7 @@ export default function locationReducer(state = {
return {
...state,
error: error,
fetching: false
}
}

Expand Down
5 changes: 3 additions & 2 deletions saga/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { take, put, call } from 'redux-saga/effects';
export const locationChannel = channel()

import {REDUX_SAGA_LOCATION_ACTION_SET_POSITION
, REDUX_SAGA_LOCATION_ACTION_SET_ERROR} from '../actions';
, REDUX_SAGA_LOCATION_ACTION_SET_ERROR
, REDUX_SAGA_LOCATION_ACTION_REQUEST} from '../actions';

export function * watchLocationChannel() {
while (true) {
Expand All @@ -15,13 +16,13 @@ export function * watchLocationChannel() {
}

export function * getCurrentPosition(options) {
locationChannel.put({type: REDUX_SAGA_LOCATION_ACTION_REQUEST})
navigator.geolocation.getCurrentPosition(
position => {
locationChannel.put({type: REDUX_SAGA_LOCATION_ACTION_SET_POSITION, position})
},
(error) => locationChannel.put({type: REDUX_SAGA_LOCATION_ACTION_SET_ERROR, error}),
options
);

}

0 comments on commit 2886707

Please sign in to comment.