From 5ea3bb9ad1bf7302416bbf7fa31f9ee9c578bb20 Mon Sep 17 00:00:00 2001 From: Nathan Marks Date: Wed, 29 Jun 2016 12:16:56 -0400 Subject: [PATCH] Add react-redux version --- package.json | 2 ++ src/App.js | 10 +++++++++- src/actions.js | 23 +++++++++++++++++++++++ src/constants.js | 6 ++++++ src/pages/Home.js | 11 ++++++++++- src/reducers/app.js | 26 ++++++++++++++++++++++++++ src/reducers/index.js | 8 ++++++++ src/store.js | 14 ++++++++++++++ 8 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/actions.js create mode 100644 src/constants.js create mode 100644 src/reducers/app.js create mode 100644 src/reducers/index.js create mode 100644 src/store.js diff --git a/package.json b/package.json index 798a8d7..052e41e 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,8 @@ "normalize.css": "^4.1.1", "react": "^15.1.0", "react-dom": "^15.1.0", + "react-redux": "^4.4.5", + "redux": "^3.5.2", "stylishly": "^0.5.0", "stylishly-chained": "^0.5.0", "stylishly-descendants": "^0.5.0", diff --git a/src/App.js b/src/App.js index a6971d2..06c789a 100644 --- a/src/App.js +++ b/src/App.js @@ -1,4 +1,6 @@ import React, { Component, PropTypes } from 'react'; +import { Provider } from 'react-redux'; +import { createStore } from './store'; import { createStyleSheet, createStyleManager } from 'stylishly'; import { createPluginRegistry } from 'stylishly/lib/pluginRegistry'; import vendorPrefixer from 'stylishly-vendor-prefixer'; @@ -11,6 +13,8 @@ import mediaQueries from 'stylishly-media-queries'; import Home from './pages/Home'; +const store = createStore(); + export const styleSheet = createStyleSheet('App', () => { return { '@raw body': { @@ -63,6 +67,10 @@ export default class App extends Component { render() { this.styleManager.render(styleSheet); - return ; + return ( + + + + ); } } diff --git a/src/actions.js b/src/actions.js new file mode 100644 index 0000000..20cdbb6 --- /dev/null +++ b/src/actions.js @@ -0,0 +1,23 @@ +import { + OPEN_APP_DRAWER, + CLOSE_APP_DRAWER, + TOGGLE_APP_DRAWER +} from './constants'; + +export function openDrawer() { + return { + type: OPEN_APP_DRAWER + }; +} + +export function closeDrawer() { + return { + type: CLOSE_APP_DRAWER + }; +} + +export function toggleDrawer() { + return { + type: TOGGLE_APP_DRAWER + }; +} diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 0000000..9d4c6d0 --- /dev/null +++ b/src/constants.js @@ -0,0 +1,6 @@ +/** + * Layout + */ +export const OPEN_APP_DRAWER = 'OPEN_APP_DRAWER'; +export const CLOSE_APP_DRAWER = 'CLOSE_APP_DRAWER'; +export const TOGGLE_APP_DRAWER = 'TOGGLE_APP_DRAWER'; diff --git a/src/pages/Home.js b/src/pages/Home.js index 22482fe..3365791 100644 --- a/src/pages/Home.js +++ b/src/pages/Home.js @@ -1,5 +1,6 @@ import React, { Component, PropTypes } from 'react'; import { createStyleSheet } from 'stylishly'; +import { connect } from 'react-redux'; export const styleSheet = createStyleSheet('Home', () => { return { @@ -18,7 +19,7 @@ export const styleSheet = createStyleSheet('Home', () => { }; }); -export default class Home extends Component { +class Home extends Component { static contextTypes = { styleManager: PropTypes.object.isRequired } @@ -33,3 +34,11 @@ export default class Home extends Component { ); } } + +function mapStateToProps(state) { + return { + drawerOpen: state.app.drawerOpen + }; +} + +export default connect(mapStateToProps)(Home); diff --git a/src/reducers/app.js b/src/reducers/app.js new file mode 100644 index 0000000..7bc6e57 --- /dev/null +++ b/src/reducers/app.js @@ -0,0 +1,26 @@ +import { + OPEN_APP_DRAWER, + CLOSE_APP_DRAWER, + TOGGLE_APP_DRAWER +} from '../constants'; + +const initialState = { + drawerOpen: false +}; + +export default function update(state = initialState, action) { + switch (action.type) { + + case OPEN_APP_DRAWER: + return { ...state, drawerOpen: true }; + + case CLOSE_APP_DRAWER: + return { ...state, drawerOpen: false }; + + case TOGGLE_APP_DRAWER: + return { ...state, drawerOpen: !state.drawerOpen }; + + default: + return state; + } +} diff --git a/src/reducers/index.js b/src/reducers/index.js new file mode 100644 index 0000000..27d77f3 --- /dev/null +++ b/src/reducers/index.js @@ -0,0 +1,8 @@ +import { combineReducers } from 'redux'; +import app from './app'; + +const rootReducer = combineReducers({ + app +}); + +export default rootReducer; diff --git a/src/store.js b/src/store.js new file mode 100644 index 0000000..026a947 --- /dev/null +++ b/src/store.js @@ -0,0 +1,14 @@ +import { createStore as _createStore } from 'redux'; +import rootReducer from './reducers'; + +export function createStore(initialState) { + const store = _createStore(rootReducer, initialState); + + if (module.hot) { + module.hot.accept('./reducers', () => + store.replaceReducer(require('./reducers/index')) + ); + } + + return store; +}