diff --git a/actions.js b/actions.js index 6d6e2db..2c134cc 100644 --- a/actions.js +++ b/actions.js @@ -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'; diff --git a/actions/index.js b/actions/index.js index aaddd97..64b0df8 100644 --- a/actions/index.js +++ b/actions/index.js @@ -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'; diff --git a/reducer/location.js b/reducer/location.js index a8b7103..f2be552 100644 --- a/reducer/location.js +++ b/reducer/location.js @@ -1,12 +1,21 @@ 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; @@ -14,6 +23,7 @@ export default function locationReducer(state = { ...state, position, error: null, + fetching: false } } @@ -23,6 +33,7 @@ export default function locationReducer(state = { return { ...state, error: error, + fetching: false } } diff --git a/saga/location.js b/saga/location.js index b086c3a..bbb250c 100644 --- a/saga/location.js +++ b/saga/location.js @@ -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) { @@ -15,6 +16,7 @@ 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}) @@ -22,6 +24,5 @@ export function * getCurrentPosition(options) { (error) => locationChannel.put({type: REDUX_SAGA_LOCATION_ACTION_SET_ERROR, error}), options ); - }