From 69d03ae0026b1cef8ec81cbacbf1e2b86054a035 Mon Sep 17 00:00:00 2001 From: jgautsch Date: Fri, 4 Dec 2015 10:02:16 -0600 Subject: [PATCH] Add dirtyHook for dispatching actions in mapStateToProps Conflicts: index.js --- .jshintrc | 27 +++++++++++++++++++++++++++ actions/ExampleActions.js | 7 +++++++ containers/App.js | 18 +++++++++++++++++- index.js | 3 +++ utils/dirtyHook.js | 12 ++++++++++++ 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 .jshintrc create mode 100644 actions/ExampleActions.js create mode 100644 utils/dirtyHook.js diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..1c5e457 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,27 @@ +{ + "node": true, + "browser": true, + "esnext": true, + "bitwise": true, + "camelcase": false, + "curly": true, + "eqeqeq": true, + "immed": true, + "indent": 2, + "latedef": true, + "newcap": true, + "noarg": true, + "quotmark": "false", + "regexp": true, + "undef": true, + "unused": false, + "trailing": true, + "smarttabs": true, + "white": true, + "newcap": false, + "esnext": true, + "globals": { + "React": true, + "__DEV__": true + } +} diff --git a/actions/ExampleActions.js b/actions/ExampleActions.js new file mode 100644 index 0000000..f984034 --- /dev/null +++ b/actions/ExampleActions.js @@ -0,0 +1,7 @@ +export const EXAMPLE_ACTION = 'EXAMPLE_ACTION'; + +export function exampleAction() { + return { + type: EXAMPLE_ACTION + }; +} diff --git a/containers/App.js b/containers/App.js index 5f31e0e..c418e5c 100644 --- a/containers/App.js +++ b/containers/App.js @@ -3,6 +3,8 @@ import { connect } from 'react-redux' import Folders from '../components/Folders' import OpenEmails from '../components/OpenEmails.js'; import { Link } from 'react-router' +import { addDirtyHook } from '../utils/dirtyHook'; +import { exampleAction } from '../actions/ExampleActions'; class App extends Component { @@ -42,5 +44,19 @@ class App extends Component { } } +function mapStateToProps(state) { + return { + folders: state.emailApp.folders.folders + }; +} + +function dirtyHook(dispatch, state) { + if (true || state.emails.dirty) { + dispatch(exampleAction()); + } +} + +const hookedMapStateToProps = addDirtyHook(dirtyHook, mapStateToProps); + // Wrap the component to inject dispatch and state into it -export default connect((state) => { return { folders: state.emailApp.folders.folders } })(App) +export default connect(hookedMapStateToProps)(App); diff --git a/index.js b/index.js index 42af046..eba2805 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ import App from './containers/App' import configureStore, { USE_DEV_TOOLS } from './store/configureStore' import { Route, Router as RealRouter } from 'react-router' import * as actions from './actions/'; +import { configureDirtyHook } from './utils/dirtyHook'; import emailApp from './emailApp' import Folders from './components/Folders' import Folder from './components/Folder' @@ -31,6 +32,8 @@ class Router extends RealRouter { window.emailApp = emailApp; const store = configureStore(); +configureDirtyHook(store); + const pageLoaders = generatePageLoaders(store.dispatch); const debugPanel = USE_DEV_TOOLS ? ( diff --git a/utils/dirtyHook.js b/utils/dirtyHook.js new file mode 100644 index 0000000..3bfda73 --- /dev/null +++ b/utils/dirtyHook.js @@ -0,0 +1,12 @@ +let _dispatch; + +export function addDirtyHook(hook, mapStateToProps) { + return function(state, props) { + hook(_dispatch, state); + return mapStateToProps.apply(this, [state, props]); + }; +} + +export function configureDirtyHook(store) { + _dispatch = store.dispatch; +}