Skip to content

Commit

Permalink
Add react-redux version
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanmarks committed Jun 29, 2016
1 parent c237234 commit 5ea3bb9
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 9 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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': {
Expand Down Expand Up @@ -63,6 +67,10 @@ export default class App extends Component {

render() {
this.styleManager.render(styleSheet);
return <Home />;
return (
<Provider store={store}>
<Home />
</Provider>
);
}
}
23 changes: 23 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -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
};
}
6 changes: 6 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -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';
11 changes: 10 additions & 1 deletion src/pages/Home.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
Expand All @@ -33,3 +34,11 @@ export default class Home extends Component {
);
}
}

function mapStateToProps(state) {
return {
drawerOpen: state.app.drawerOpen
};
}

export default connect(mapStateToProps)(Home);
26 changes: 26 additions & 0 deletions src/reducers/app.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 8 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { combineReducers } from 'redux';
import app from './app';

const rootReducer = combineReducers({
app
});

export default rootReducer;
14 changes: 14 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 5ea3bb9

Please sign in to comment.