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

DO NOT MERGE - EXAMPLE Thunk actions#9 #211

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"electron-debug": "^0.6.0",
"electron-json-storage": "^2.0.0",
"electron-positioner": "^3.0.0",
"electron-redux": "^1.2.0",
"electron-redux": "^1.2.1",
"lodash": "^4.13.1",
"material-design-lite": "^1.1.3",
"moment": "^2.13.0",
Expand Down
33 changes: 30 additions & 3 deletions app/renderer/main/components/Home/index.jsx
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;
18 changes: 11 additions & 7 deletions app/renderer/main/containers/HomePage.js
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);
48 changes: 48 additions & 0 deletions app/shared/actions/counter.js
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() {
Copy link
Owner Author

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

return {
type: INCREMENT_COUNTER,
};
}

export function incrementBy(step) {
Copy link
Owner Author

Choose a reason for hiding this comment

The 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() {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redux-thunk action that triggers FSA renderer-side

return dispatch => {
setTimeout(() => {
// Yay! Can invoke sync or async actions with `dispatch`
dispatch(increment());
}, 1000);
};
}

export function incrementByAsync(step) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redux-thunk action that triggers FSA with payload

return dispatch => {
setTimeout(() => {
dispatch(incrementBy(step));
}, 1000);
};
}

export const incrementMain = createAliasedAction(
Copy link
Owner Author

Choose a reason for hiding this comment

The 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(
Copy link
Owner Author

Choose a reason for hiding this comment

The 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(
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, with payload (recommended)

`${INCREMENT_COUNTER}_BY_ASYNC_MAIN`,
incrementByAsync
);
2 changes: 1 addition & 1 deletion app/shared/actions/github.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createAliasedAction } from 'electron-redux';
import githubAuth from '../../main/api/requestGithubToken';
import getRepos from '../../main/api/getRepos';
import importProjects from '../../main/api/importProjects';
import getIssuesForUser from '../../main/api/getIssuesForUser';
import { createAliasedAction } from 'electron-redux';

// Authenticate
export const AUTHENTICATE_GITHUB = 'AUTHENTICATE_GITHUB';
Expand Down
21 changes: 21 additions & 0 deletions app/shared/reducers/counter.js
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;
}
}
2 changes: 2 additions & 0 deletions app/shared/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import job from './job';
import project from './project';
import github from './github';
import settings from './settings';
import counter from './counter';

export default function getRootReducer(scope = 'main') {
let reducers = {
Expand All @@ -14,6 +15,7 @@ export default function getRootReducer(scope = 'main') {
project,
github,
settings,
counter,
};

if (scope === 'renderer') {
Expand Down
26 changes: 24 additions & 2 deletions app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,12 @@ electron-positioner@^3.0.0:
resolved "https://registry.yarnpkg.com/electron-positioner/-/electron-positioner-3.0.0.tgz#1a3b75c9cc1e29dd37c663b23fd876d4ffab9996"

electron-redux:
version "1.2.0"
resolved "https://registry.yarnpkg.com/electron-redux/-/electron-redux-1.2.0.tgz#76c46c8b86d812a4042f1179b572c48fac1caef8"
version "1.2.1"
resolved "https://registry.yarnpkg.com/electron-redux/-/electron-redux-1.2.1.tgz#9e1fde6b201a743909a836e977d42b075381260e"
dependencies:
debug "^2.2.0"
electron "^1.4.3"
flux-standard-action "^1.0.0"
redux "^3.4.0"

electron@^1.4.3:
Expand Down Expand Up @@ -415,6 +417,14 @@ flux-standard-action@^0.6.1:
dependencies:
lodash.isplainobject "^3.2.0"

flux-standard-action@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/flux-standard-action/-/flux-standard-action-1.0.0.tgz#24b84b627386714e33a0f07773931fc4b5fc452e"
dependencies:
lodash.isplainobject "^4.0.6"
lodash.isstring "^4.0.1"
lodash.issymbol "^4.0.1"

forever-agent@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
Expand Down Expand Up @@ -804,6 +814,18 @@ lodash.isplainobject@^3.2.0:
lodash.isarguments "^3.0.0"
lodash.keysin "^3.0.0"

lodash.isplainobject@^4.0.6:
version "4.0.6"
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"

lodash.isstring@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"

lodash.issymbol@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/lodash.issymbol/-/lodash.issymbol-4.0.1.tgz#04ad41d96f3f4f399c37dd4fcf3c1b6901e16116"

lodash.keys@^3.0.0, lodash.keys@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash.keys/-/lodash.keys-3.1.2.tgz#4dbc0472b156be50a0b286855d1bd0b0c656098a"
Expand Down