-
Notifications
You must be signed in to change notification settings - Fork 22
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
DO NOT MERGE - EXAMPLE Thunk actions#9 #211
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,40 @@ | ||
import React from 'react'; | ||
import { Grid, Cell } from 'react-mdl'; | ||
import React, { PropTypes } from 'react'; | ||
import { Grid, Cell, Button } from 'react-mdl'; | ||
import { createAliasedAction } from 'electron-redux'; | ||
import pkg from '../../../../package.json'; | ||
|
||
export default function Home() { | ||
function Home({ | ||
increment, | ||
incrementAsync, | ||
incrementBy, | ||
incrementMain, | ||
incrementAsyncMain, | ||
incrementByAsyncMain, | ||
counter, | ||
}) { | ||
return ( | ||
<Grid> | ||
<Cell col={12}> | ||
<h2>Timesheets <small>v{pkg.version}</small></h2> | ||
<p>Count: <strong>{counter.count}</strong></p> | ||
<Button raised accent ripple onClick={() => increment()}>incr</Button><br /> | ||
<Button raised accent ripple onClick={() => incrementAsync()}>incr async</Button><br /> | ||
<Button raised accent ripple onClick={() => incrementBy(2)}>incr by 2</Button><br /> | ||
<Button raised accent ripple onClick={() => incrementMain()}>incr main process</Button><br /> | ||
<Button raised accent ripple onClick={() => incrementAsyncMain()}>incr async main</Button><br /> | ||
<Button raised accent ripple onClick={() => incrementByAsyncMain(2)}>incr by 2 async main</Button><br /> | ||
</Cell> | ||
</Grid> | ||
); | ||
} | ||
|
||
Home.propTypes = { | ||
increment: PropTypes.func.isRequired, | ||
incrementAsync: PropTypes.func.isRequired, | ||
incrementBy: PropTypes.func.isRequired, | ||
incrementAsyncMain: PropTypes.func.isRequired, | ||
incrementByAsyncMain: PropTypes.func.isRequired, | ||
counter: PropTypes.object.isRequired, | ||
}; | ||
|
||
export default Home; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import React, { Component } from 'react'; | ||
import { bindActionCreators } from 'redux'; | ||
import { connect } from 'react-redux'; | ||
import Home from '../components/Home'; | ||
import * as CounterActions from '../../../shared/actions/counter'; | ||
|
||
export default class HomePage extends Component { | ||
render() { | ||
return ( | ||
<Home /> | ||
); | ||
} | ||
function mapStateToProps({ counter }) { | ||
return { counter }; | ||
} | ||
|
||
function mapDispatchToProps(dispatch) { | ||
return bindActionCreators(CounterActions, dispatch); | ||
} | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(Home); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { createAliasedAction } from 'electron-redux'; | ||
|
||
export const INCREMENT_COUNTER = 'INCREMENT_COUNTER'; | ||
|
||
export function increment() { | ||
return { | ||
type: INCREMENT_COUNTER, | ||
}; | ||
} | ||
|
||
export function incrementBy(step) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FSA with a payload |
||
return { | ||
type: INCREMENT_COUNTER, | ||
payload: step, | ||
}; | ||
} | ||
|
||
export function incrementAsync() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return dispatch => { | ||
setTimeout(() => { | ||
// Yay! Can invoke sync or async actions with `dispatch` | ||
dispatch(increment()); | ||
}, 1000); | ||
}; | ||
} | ||
|
||
export function incrementByAsync(step) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return dispatch => { | ||
setTimeout(() => { | ||
dispatch(incrementBy(step)); | ||
}, 1000); | ||
}; | ||
} | ||
|
||
export const incrementMain = createAliasedAction( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aliased action that triggers FSA main-side |
||
INCREMENT_COUNTER, | ||
increment | ||
); | ||
|
||
export const incrementAsyncMain = createAliasedAction( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aliased action that triggers thunk action (which produces FSA) main-side (recommended) |
||
`${INCREMENT_COUNTER}_MAIN`, | ||
incrementAsync | ||
); | ||
|
||
export const incrementByAsyncMain = createAliasedAction( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above, with payload (recommended) |
||
`${INCREMENT_COUNTER}_BY_ASYNC_MAIN`, | ||
incrementByAsync | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* eslint-disable no-param-reassign */ | ||
import { INCREMENT_COUNTER } from '../actions/counter'; | ||
|
||
const initialState = { | ||
count: 0, | ||
}; | ||
|
||
export default function counter(state = initialState, action) { | ||
switch (action.type) { | ||
case INCREMENT_COUNTER: { | ||
const step = action.payload || 1; | ||
return { | ||
...state, | ||
count: state.count + step, | ||
}; | ||
} | ||
|
||
default: | ||
return state; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A standard FSA-complaint action