Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dirtyHook for dispatching actions in mapStateToProps #7

Open
wants to merge 1 commit into
base: routed-email-app
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -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
}
}
7 changes: 7 additions & 0 deletions actions/ExampleActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const EXAMPLE_ACTION = 'EXAMPLE_ACTION';

export function exampleAction() {
return {
type: EXAMPLE_ACTION
};
}
18 changes: 17 additions & 1 deletion containers/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 ? (
Expand Down
12 changes: 12 additions & 0 deletions utils/dirtyHook.js
Original file line number Diff line number Diff line change
@@ -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;
}