diff --git a/app/package.json b/app/package.json index a39d24a..1390849 100644 --- a/app/package.json +++ b/app/package.json @@ -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", @@ -46,6 +46,7 @@ "redux-form": "^6.0.0-alpha.14", "redux-logger": "^2.6.1", "redux-promise": "^0.5.3", + "redux-promise-middleware": "^4.1.0", "redux-thunk": "^2.0.1" } } diff --git a/app/renderer/main/components/Home/index.jsx b/app/renderer/main/components/Home/index.jsx index ea14e33..edd7432 100644 --- a/app/renderer/main/components/Home/index.jsx +++ b/app/renderer/main/components/Home/index.jsx @@ -1,13 +1,51 @@ -import React from 'react'; -import { Grid, Cell } from 'react-mdl'; +import React, { PropTypes } from 'react'; +import { Grid, Cell, Button } from 'react-mdl'; import pkg from '../../../../package.json'; -export default function Home() { +function Home({ + increment, + incrementAsync, + incrementBy, + incrementMain, + incrementAsyncMain, + incrementByAsyncMain, + incrementPromise, + incrementPromiseMain, + incrementPromiseLocal, + counter, +}) { return (

Timesheets v{pkg.version}

+

Count: {counter.loading ? 'Loading...' : counter.count}

+

redux-thunk

+
+
+
+
+
+
+

redux-promise-middleware

+
+
+
); } + +Home.propTypes = { + increment: PropTypes.func.isRequired, + incrementAsync: PropTypes.func.isRequired, + incrementBy: PropTypes.func.isRequired, + incrementMain: PropTypes.func.isRequired, + incrementAsyncMain: PropTypes.func.isRequired, + incrementByAsyncMain: PropTypes.func.isRequired, + incrementPromise: PropTypes.func.isRequired, + incrementPromiseMain: PropTypes.func.isRequired, + incrementPromiseLocal: PropTypes.func.isRequired, + counter: PropTypes.object.isRequired, +}; + +export default Home; diff --git a/app/renderer/main/containers/HomePage.js b/app/renderer/main/containers/HomePage.js index b938e37..d31d1ba 100644 --- a/app/renderer/main/containers/HomePage.js +++ b/app/renderer/main/containers/HomePage.js @@ -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 ( - - ); - } +function mapStateToProps({ counter }) { + return { counter }; } + +function mapDispatchToProps(dispatch) { + return bindActionCreators(CounterActions, dispatch); +} + +export default connect(mapStateToProps, mapDispatchToProps)(Home); diff --git a/app/shared/actions/counter.js b/app/shared/actions/counter.js new file mode 100644 index 0000000..233a514 --- /dev/null +++ b/app/shared/actions/counter.js @@ -0,0 +1,82 @@ +import { createAliasedAction } from 'electron-redux'; + +export const INCREMENT_COUNTER = 'INCREMENT_COUNTER'; + +export function increment() { + return { + type: INCREMENT_COUNTER, + }; +} + +export function incrementBy(step) { + return { + type: INCREMENT_COUNTER, + payload: step, + }; +} + +export function incrementAsync() { + return dispatch => { + setTimeout(() => { + // Yay! Can invoke sync or async actions with `dispatch` + dispatch(increment()); + }, 1000); + }; +} + +export function incrementByAsync(step) { + return dispatch => { + setTimeout(() => { + dispatch(incrementBy(step)); + }, 1000); + }; +} + +export const incrementMain = createAliasedAction( + INCREMENT_COUNTER, + increment +); + +export const incrementAsyncMain = createAliasedAction( + `${INCREMENT_COUNTER}_MAIN`, + incrementAsync +); + +export const incrementByAsyncMain = createAliasedAction( + `${INCREMENT_COUNTER}_BY_ASYNC_MAIN`, + incrementByAsync +); + +export function incrementPromise() { + return { + type: INCREMENT_COUNTER, + payload: { + promise: new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 1000); + }), + }, + }; +} + +export const incrementPromiseMain = createAliasedAction( + `${INCREMENT_COUNTER}_PROMISE_MAIN`, + incrementPromise +); + +export function incrementPromiseLocal() { + return { + type: INCREMENT_COUNTER, + payload: { + promise: new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 1000); + }), + }, + meta: { + scope: 'local', + }, + }; +} diff --git a/app/shared/actions/github.js b/app/shared/actions/github.js index 7d61429..eb9180a 100644 --- a/app/shared/actions/github.js +++ b/app/shared/actions/github.js @@ -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'; diff --git a/app/shared/reducers/counter.js b/app/shared/reducers/counter.js new file mode 100644 index 0000000..df71e54 --- /dev/null +++ b/app/shared/reducers/counter.js @@ -0,0 +1,30 @@ +/* eslint-disable no-param-reassign */ +import { INCREMENT_COUNTER } from '../actions/counter'; + +const initialState = { + count: 0, + loading: false, +}; + +export default function counter(state = initialState, action) { + switch (action.type) { + case `${INCREMENT_COUNTER}_PENDING`: { + return { + ...state, + loading: true, + }; + } + case `${INCREMENT_COUNTER}_FULFILLED`: + case INCREMENT_COUNTER: { + const step = action.payload || 1; + return { + ...state, + loading: false, + count: parseInt(state.count + step, 10) || initialState.count, + }; + } + + default: + return state; + } +} diff --git a/app/shared/reducers/index.js b/app/shared/reducers/index.js index c9a4a60..37b0a31 100644 --- a/app/shared/reducers/index.js +++ b/app/shared/reducers/index.js @@ -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 = { @@ -14,6 +15,7 @@ export default function getRootReducer(scope = 'main') { project, github, settings, + counter, }; if (scope === 'renderer') { diff --git a/app/shared/store/configureStore.js b/app/shared/store/configureStore.js index adf4bb3..69d010f 100644 --- a/app/shared/store/configureStore.js +++ b/app/shared/store/configureStore.js @@ -1,7 +1,8 @@ import { createStore, applyMiddleware, compose } from 'redux'; import { persistState } from 'redux-devtools'; import thunk from 'redux-thunk'; -import promise from 'redux-promise'; +// import promise from 'redux-promise'; +import promise from 'redux-promise-middleware'; import createLogger from 'redux-logger'; import { hashHistory } from 'react-router'; import { routerMiddleware } from 'react-router-redux'; @@ -29,7 +30,7 @@ export default function configureStore(initialState, scope = 'main') { let middleware = [ thunk, - promise, + promise(), ]; if (!process.env.NODE_ENV) { diff --git a/app/yarn.lock b/app/yarn.lock index e3dcb11..df61512 100644 --- a/app/yarn.lock +++ b/app/yarn.lock @@ -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: @@ -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" @@ -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" @@ -1318,6 +1340,10 @@ redux-logger@^2.6.1: dependencies: deep-diff "0.3.4" +redux-promise-middleware: + version "4.1.0" + resolved "https://registry.yarnpkg.com/redux-promise-middleware/-/redux-promise-middleware-4.1.0.tgz#8477866fa09837c1f08f5869c473747577f5446a" + redux-promise@^0.5.3: version "0.5.3" resolved "https://registry.yarnpkg.com/redux-promise/-/redux-promise-0.5.3.tgz#e97e6c9d3bf376eacb79babe6d906da20112d6d8"