Skip to content

Commit

Permalink
Extending the example with async reducer. (first try)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandrenko committed Jun 21, 2016
1 parent 998ac53 commit bfbfcc8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
3 changes: 2 additions & 1 deletion examples/simple-counter/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import Error404 from './pages/Error404';
store.incrementCounter();

const HomePageConnected = connect((store) => ({
counter: store.counter
counter: store.counter,
status: store.status
}), store.mapStoreToProps)(HomePage);

// ROUTES
Expand Down
8 changes: 7 additions & 1 deletion examples/simple-counter/app/pages/Home/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ import React from 'react';

const Component = React.createClass({
propTypes: {
counter: React.PropTypes.number
counter: React.PropTypes.number,
status: React.PropTypes.string
},
buttonClickHandler() {
this.props.store.incrementCounter();
},
buttonSetClickHandler() {
this.props.store.setCounter(10);
},
buttonDelayedSetClickHandler() {
this.props.store.delayedIncrementCounter(10);
},
render() {
return (
<section>
<h1>Home page</h1>
<div>Counter: <b>{ this.props.counter }</b></div>
<div>Action in progress: <b>{ this.props.status }</b></div>
<div><button onClick={ this.buttonClickHandler }>Increment counter</button></div>
<div><button onClick={ this.buttonSetClickHandler }>Set counter to 10</button></div>
<div><button onClick={ this.buttonDelayedSetClickHandler }>Increment counter (Delayed)</button></div>
</section>
);
}
Expand Down
29 changes: 26 additions & 3 deletions examples/simple-counter/app/store.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import sugarStore from '../../../';
import { createStore } from 'redux';

const store = sugarStore(createStore, { counter: 0 }, [
const store = sugarStore(createStore, { counter: 0, status: 'normal' }, [
{
incrementCounter: (state, action) => {
console.log('%cReducer: incrementCounter (green reducer)', 'color: green;');

return {
counter: (state.counter + 1)
counter: (state.counter + 1),
status: 'normal'
};
}
}, {
Expand All @@ -21,7 +22,29 @@ const store = sugarStore(createStore, { counter: 0 }, [
console.log('%cReducer: setCounter', 'color: purple;');

return {
counter: action.payload
counter: action.payload,
status: 'normal'
}
}
},
{
delayedIncrementCounter: (state, action) => {
console.log('%cReducer: delayedIncrementCounter', 'color: red;');

if (action.payload.done) {
return {
counter: (state.counter + 1),
status: 'normal'
};
} else {
setTimeout(function() {
store.delayedIncrementCounter({ done: true });
}, 1000);
}

return {
counter: state.counter,
status: 'in progress'
}
}
}
Expand Down

0 comments on commit bfbfcc8

Please sign in to comment.