From 61f1cd11638878a57c178811f6609464c211bcc0 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Mon, 2 May 2016 21:07:07 +0100 Subject: [PATCH] Add some success action chaining --- app/actions/index.js | 4 ++-- app/components/routes/login.js | 2 +- app/middleware/actionChain.js | 9 +++++++++ app/store.js | 2 ++ 4 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 app/middleware/actionChain.js diff --git a/app/actions/index.js b/app/actions/index.js index e391e96..7668e75 100644 --- a/app/actions/index.js +++ b/app/actions/index.js @@ -5,12 +5,12 @@ import endpoints from '../settings/endpoints'; export const LOGIN = makeAsyncActionSet('LOGIN'); -export function login(token) { +export function login(token, callback) { return buildAsyncAction( LOGIN, endpoints.get('USER_INFO').query({ 'circle-token': token }).toString(), methods.GET, {}, - { token } + { token, callback } ); } diff --git a/app/components/routes/login.js b/app/components/routes/login.js index 6ef4505..be1e649 100644 --- a/app/components/routes/login.js +++ b/app/components/routes/login.js @@ -121,7 +121,7 @@ export class Login extends React.Component { loaderHandler.hideLoader(); return; } - this.props.login(this.state.token); + this.props.login(this.state.token, this.props.nav.push.bind(this.props.nav, RouteMaster.get('HOME'))); } showAbout() { diff --git a/app/middleware/actionChain.js b/app/middleware/actionChain.js new file mode 100644 index 0000000..47ed8f6 --- /dev/null +++ b/app/middleware/actionChain.js @@ -0,0 +1,9 @@ +export default store => next => action => { + if (action.meta && + action.meta.callback && + action.type.endsWith('SUCCESS') && + typeof action.meta.callback === 'function') { + action.meta.callback(); + } + return next(action); +}; diff --git a/app/store.js b/app/store.js index 830b0cf..e3fb29d 100644 --- a/app/store.js +++ b/app/store.js @@ -7,6 +7,7 @@ import createEngine from 'redux-storage-engine-reactnativeasyncstorage'; import filter from 'redux-storage-decorator-filter'; import { apiMiddleware } from 'redux-api-middleware'; +import actionChain from './middleware/actionChain'; const engine = filter(createEngine(settings.get('STORAGE_KEY'))); @@ -18,6 +19,7 @@ export default function configureStore(initialState) { const createStoreWithMiddleware = applyMiddleware( apiMiddleware, + actionChain, storeMiddleware // Last in list )(createStore);