From 2932a2185a02c8ee716119c38845be6e1a514e79 Mon Sep 17 00:00:00 2001 From: Matheus Rezende Date: Wed, 28 Feb 2018 23:55:09 +0700 Subject: [PATCH] added redux-thunk support --- index.js | 1 + thunks/location.js | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 thunks/location.js diff --git a/index.js b/index.js index 23d0790..882904d 100644 --- a/index.js +++ b/index.js @@ -7,3 +7,4 @@ export { watchLocationChannel, getCurrentPosition, watchCurrentPosition } from './saga/location'; export { default as locationReducer } from './reducer/location'; +export {getCurrentPositionThunk, watchCurrentPositionThunk} from './thunks/location' \ No newline at end of file diff --git a/thunks/location.js b/thunks/location.js new file mode 100644 index 0000000..e924dca --- /dev/null +++ b/thunks/location.js @@ -0,0 +1,27 @@ +import { + REDUX_SAGA_LOCATION_ACTION_REQUEST, + REDUX_SAGA_LOCATION_ACTION_SET_POSITION, + REDUX_SAGA_LOCATION_ACTION_SET_ERROR, +} from '../actions'; + +export const getCurrentPositionThunk = (options) => dispatch => { + dispatch({type: REDUX_SAGA_LOCATION_ACTION_REQUEST}) + navigator.geolocation.getCurrentPosition( + position => { + dispatch({type: REDUX_SAGA_LOCATION_ACTION_SET_POSITION, position}) + }, + (error) => dispatch({type: REDUX_SAGA_LOCATION_ACTION_SET_ERROR, error}), + options + ); +} + +export const watchCurrentPositionThunk = (options) => (dispatch) =>{ + dispatch({type: REDUX_SAGA_LOCATION_ACTION_REQUEST}) + navigator.geolocation.watchPosition( + position => { + dispatch({type: REDUX_SAGA_LOCATION_ACTION_SET_POSITION, position}) + }, + (error) => dispatch({type: REDUX_SAGA_LOCATION_ACTION_SET_ERROR, error}), + options + ); +}