From 3fdf194683abb7c40f3cb7969fd1f8aa6a4f9c57 Mon Sep 17 00:00:00 2001 From: Erik Rasmussen Date: Fri, 21 Aug 2015 15:35:50 +0200 Subject: [PATCH] quack! quack! --- src/actions/actionTypes.js | 21 ------ src/actions/authActions.js | 36 ---------- src/actions/counterActions.js | 9 --- src/actions/infoActions.js | 12 ---- src/actions/widgetActions.js | 35 ---------- src/components/CounterButton.js | 2 +- src/components/InfoBar.js | 2 +- src/components/WidgetForm.js | 2 +- src/ducks/auth.js | 103 +++++++++++++++++++++++++++++ src/ducks/counter.js | 23 +++++++ src/{reducers => ducks}/index.js | 0 src/{reducers => ducks}/info.js | 21 +++--- src/{reducers => ducks}/widgets.js | 61 +++++++++++------ src/reducers/auth.js | 80 ---------------------- src/reducers/counter.js | 19 ------ src/redux/create.js | 6 +- src/views/App.js | 6 +- src/views/Login.js | 5 +- src/views/LoginSuccess.js | 5 +- src/views/Widgets.js | 5 +- 20 files changed, 195 insertions(+), 258 deletions(-) delete mode 100644 src/actions/actionTypes.js delete mode 100644 src/actions/authActions.js delete mode 100644 src/actions/counterActions.js delete mode 100644 src/actions/infoActions.js delete mode 100644 src/actions/widgetActions.js create mode 100644 src/ducks/auth.js create mode 100644 src/ducks/counter.js rename src/{reducers => ducks}/index.js (100%) rename src/{reducers => ducks}/info.js (63%) rename src/{reducers => ducks}/widgets.js (55%) delete mode 100644 src/reducers/auth.js delete mode 100644 src/reducers/counter.js diff --git a/src/actions/actionTypes.js b/src/actions/actionTypes.js deleted file mode 100644 index 77d74966d..000000000 --- a/src/actions/actionTypes.js +++ /dev/null @@ -1,21 +0,0 @@ -export const INFO_LOAD = 'INFO_LOAD'; -export const INFO_LOAD_SUCCESS = 'INFO_LOAD_SUCCESS'; -export const INFO_LOAD_FAIL = 'INFO_LOAD_FAIL'; -export const WIDGET_LOAD = 'WIDGET_LOAD'; -export const WIDGET_LOAD_SUCCESS = 'WIDGET_LOAD_SUCCESS'; -export const WIDGET_LOAD_FAIL = 'WIDGET_LOAD_FAIL'; -export const WIDGET_EDIT_START = 'WIDGET_EDIT_START'; -export const WIDGET_EDIT_STOP = 'WIDGET_EDIT_STOP'; -export const WIDGET_SAVE = 'WIDGET_SAVE'; -export const WIDGET_SAVE_SUCCESS = 'WIDGET_SAVE_SUCCESS'; -export const WIDGET_SAVE_FAIL = 'WIDGET_SAVE_FAIL'; -export const AUTH_LOAD = 'AUTH_LOAD'; -export const AUTH_LOAD_SUCCESS = 'AUTH_LOAD_SUCCESS'; -export const AUTH_LOAD_FAIL = 'AUTH_LOAD_FAIL'; -export const AUTH_LOGIN = 'AUTH_LOGIN'; -export const AUTH_LOGIN_SUCCESS = 'AUTH_LOGIN_SUCCESS'; -export const AUTH_LOGIN_FAIL = 'AUTH_LOGIN_FAIL'; -export const AUTH_LOGOUT = 'AUTH_LOGOUT'; -export const AUTH_LOGOUT_SUCCESS = 'AUTH_LOGOUT_SUCCESS'; -export const AUTH_LOGOUT_FAIL = 'AUTH_LOGOUT_FAIL'; -export const COUNTER_INCREMENT = 'COUNTER_INCREMENT'; diff --git a/src/actions/authActions.js b/src/actions/authActions.js deleted file mode 100644 index 3fb77e378..000000000 --- a/src/actions/authActions.js +++ /dev/null @@ -1,36 +0,0 @@ -import { - AUTH_LOAD, - AUTH_LOAD_SUCCESS, - AUTH_LOAD_FAIL, - AUTH_LOGIN, - AUTH_LOGIN_SUCCESS, - AUTH_LOGIN_FAIL, - AUTH_LOGOUT, - AUTH_LOGOUT_SUCCESS, - AUTH_LOGOUT_FAIL -} from './actionTypes'; - -export function load() { - return { - types: [AUTH_LOAD, AUTH_LOAD_SUCCESS, AUTH_LOAD_FAIL], - promise: (client) => client.get('/loadAuth') - }; -} - -export function login(name) { - return { - types: [AUTH_LOGIN, AUTH_LOGIN_SUCCESS, AUTH_LOGIN_FAIL], - promise: (client) => client.post('/login', { - data: { - name: name - } - }) - }; -} - -export function logout() { - return { - types: [AUTH_LOGOUT, AUTH_LOGOUT_SUCCESS, AUTH_LOGOUT_FAIL], - promise: (client) => client.get('/logout') - }; -} diff --git a/src/actions/counterActions.js b/src/actions/counterActions.js deleted file mode 100644 index e4fb4be46..000000000 --- a/src/actions/counterActions.js +++ /dev/null @@ -1,9 +0,0 @@ -import { - COUNTER_INCREMENT -} from './actionTypes'; - -export function increment() { - return { - type: COUNTER_INCREMENT - }; -} diff --git a/src/actions/infoActions.js b/src/actions/infoActions.js deleted file mode 100644 index a13c33a7f..000000000 --- a/src/actions/infoActions.js +++ /dev/null @@ -1,12 +0,0 @@ -import { - INFO_LOAD, - INFO_LOAD_SUCCESS, - INFO_LOAD_FAIL -} from './actionTypes'; - -export function load() { - return { - types: [INFO_LOAD, INFO_LOAD_SUCCESS, INFO_LOAD_FAIL], - promise: (client) => client.get('/loadInfo') - }; -} diff --git a/src/actions/widgetActions.js b/src/actions/widgetActions.js deleted file mode 100644 index a1a2c73bb..000000000 --- a/src/actions/widgetActions.js +++ /dev/null @@ -1,35 +0,0 @@ -import { - WIDGET_LOAD, - WIDGET_LOAD_SUCCESS, - WIDGET_LOAD_FAIL, - WIDGET_EDIT_START, - WIDGET_EDIT_STOP, - WIDGET_SAVE, - WIDGET_SAVE_SUCCESS, - WIDGET_SAVE_FAIL -} from './actionTypes'; - -export function load() { - return { - types: [WIDGET_LOAD, WIDGET_LOAD_SUCCESS, WIDGET_LOAD_FAIL], - promise: (client) => client.get('/loadWidgets') - }; -} - -export function save(widget) { - return { - types: [WIDGET_SAVE, WIDGET_SAVE_SUCCESS, WIDGET_SAVE_FAIL], - id: widget.id, - promise: (client) => client.post('/updateWidget', { - data: widget - }) - }; -} - -export function editStart(id) { - return { type: WIDGET_EDIT_START, id }; -} - -export function editStop(id) { - return { type: WIDGET_EDIT_STOP, id }; -} diff --git a/src/components/CounterButton.js b/src/components/CounterButton.js index 03cda9afd..59957e383 100755 --- a/src/components/CounterButton.js +++ b/src/components/CounterButton.js @@ -1,7 +1,7 @@ import React, {Component, PropTypes} from 'react'; import {bindActionCreators} from 'redux'; import {connect} from 'react-redux'; -import {increment} from '../actions/counterActions'; +import {increment} from '../ducks/counter'; @connect( state => ({count: state.counter.count}), diff --git a/src/components/InfoBar.js b/src/components/InfoBar.js index 5a0c6141c..8fdc0b764 100755 --- a/src/components/InfoBar.js +++ b/src/components/InfoBar.js @@ -1,7 +1,7 @@ import React, {Component, PropTypes} from 'react'; import {bindActionCreators} from 'redux'; import {connect} from 'react-redux'; -import {load} from '../actions/infoActions'; +import {load} from '../ducks/info'; @connect( state => ({info: state.info.data}), diff --git a/src/components/WidgetForm.js b/src/components/WidgetForm.js index 2b0610ac6..c483a552f 100755 --- a/src/components/WidgetForm.js +++ b/src/components/WidgetForm.js @@ -3,7 +3,7 @@ import {connect} from 'react-redux'; import {bindActionCreators} from 'redux'; import reduxForm from 'redux-form'; import widgetValidation, {colors} from '../validation/widgetValidation'; -import * as widgetActions from '../actions/widgetActions'; +import * as widgetActions from '../ducks/widgets'; @connect( state => ({ diff --git a/src/ducks/auth.js b/src/ducks/auth.js new file mode 100644 index 000000000..51228cbf6 --- /dev/null +++ b/src/ducks/auth.js @@ -0,0 +1,103 @@ +const LOAD = 'redux-example/auth/LOAD'; +const LOAD_SUCCESS = 'redux-example/auth/LOAD_SUCCESS'; +const LOAD_FAIL = 'redux-example/auth/LOAD_FAIL'; +const LOGIN = 'redux-example/auth/LOGIN'; +const LOGIN_SUCCESS = 'redux-example/auth/LOGIN_SUCCESS'; +const LOGIN_FAIL = 'redux-example/auth/LOGIN_FAIL'; +const LOGOUT = 'redux-example/auth/LOGOUT'; +const LOGOUT_SUCCESS = 'redux-example/auth/LOGOUT_SUCCESS'; +const LOGOUT_FAIL = 'redux-example/auth/LOGOUT_FAIL'; + +const initialState = { + loaded: false +}; + +export default function reducer(state = initialState, action = {}) { + switch (action.type) { + case LOAD: + return { + ...state, + loading: true + }; + case LOAD_SUCCESS: + return { + ...state, + loading: false, + loaded: true, + user: action.result + }; + case LOAD_FAIL: + return { + ...state, + loading: false, + loaded: false, + error: action.error + }; + case LOGIN: + return { + ...state, + loggingIn: true + }; + case LOGIN_SUCCESS: + return { + ...state, + loggingIn: false, + user: action.result + }; + case LOGIN_FAIL: + return { + ...state, + loggingIn: false, + user: null, + loginError: action.error + }; + case LOGOUT: + return { + ...state, + loggingOut: true + }; + case LOGOUT_SUCCESS: + return { + ...state, + loggingOut: false, + user: null + }; + case LOGOUT_FAIL: + return { + ...state, + loggingOut: false, + logoutError: action.error + }; + default: + return state; + } +} + +export function isLoaded(globalState) { + return globalState.auth && globalState.auth.loaded; +} + +export function load() { + return { + types: [LOAD, LOAD_SUCCESS, LOAD_FAIL], + promise: (client) => client.get('/loadAuth') + }; +} + +export function login(name) { + return { + types: [LOGIN, LOGIN_SUCCESS, LOGIN_FAIL], + promise: (client) => client.post('/login', { + data: { + name: name + } + }) + }; +} + +export function logout() { + return { + types: [LOGOUT, LOGOUT_SUCCESS, LOGOUT_FAIL], + promise: (client) => client.get('/logout') + }; +} diff --git a/src/ducks/counter.js b/src/ducks/counter.js new file mode 100644 index 000000000..74bb67e40 --- /dev/null +++ b/src/ducks/counter.js @@ -0,0 +1,23 @@ +const INCREMENT = 'redux-example/counter/INCREMENT'; + +const initialState = { + count: 0 +}; + +export default function reducer(state = initialState, action = {}) { + switch (action.type) { + case INCREMENT: + const {count} = state; + return { + count: count + 1 + }; + default: + return state; + } +} + +export function increment() { + return { + type: INCREMENT + }; +} diff --git a/src/reducers/index.js b/src/ducks/index.js similarity index 100% rename from src/reducers/index.js rename to src/ducks/index.js diff --git a/src/reducers/info.js b/src/ducks/info.js similarity index 63% rename from src/reducers/info.js rename to src/ducks/info.js index 5633ae8df..bca72b146 100644 --- a/src/reducers/info.js +++ b/src/ducks/info.js @@ -1,8 +1,6 @@ -import { - INFO_LOAD, - INFO_LOAD_SUCCESS, - INFO_LOAD_FAIL -} from '../actions/actionTypes'; +const LOAD = 'redux-example/LOAD'; +const LOAD_SUCCESS = 'redux-example/LOAD_SUCCESS'; +const LOAD_FAIL = 'redux-example/LOAD_FAIL'; const initialState = { loaded: false @@ -10,19 +8,19 @@ const initialState = { export default function info(state = initialState, action = {}) { switch (action.type) { - case INFO_LOAD: + case LOAD: return { ...state, loading: true }; - case INFO_LOAD_SUCCESS: + case LOAD_SUCCESS: return { ...state, loading: false, loaded: true, data: action.result }; - case INFO_LOAD_FAIL: + case LOAD_FAIL: return { ...state, loading: false, @@ -37,3 +35,10 @@ export default function info(state = initialState, action = {}) { export function isLoaded(globalState) { return globalState.info && globalState.info.loaded; } + +export function load() { + return { + types: [LOAD, LOAD_SUCCESS, LOAD_FAIL], + promise: (client) => client.get('/loadInfo') + }; +} diff --git a/src/reducers/widgets.js b/src/ducks/widgets.js similarity index 55% rename from src/reducers/widgets.js rename to src/ducks/widgets.js index e8e665289..bc2a0d865 100644 --- a/src/reducers/widgets.js +++ b/src/ducks/widgets.js @@ -1,13 +1,11 @@ -import { - WIDGET_LOAD, - WIDGET_LOAD_SUCCESS, - WIDGET_LOAD_FAIL, - WIDGET_EDIT_START, - WIDGET_EDIT_STOP, - WIDGET_SAVE, - WIDGET_SAVE_SUCCESS, - WIDGET_SAVE_FAIL -} from '../actions/actionTypes'; +const LOAD = 'redux-example/widgets/LOAD'; +const LOAD_SUCCESS = 'redux-example/widgets/LOAD_SUCCESS'; +const LOAD_FAIL = 'redux-example/widgets/LOAD_FAIL'; +const EDIT_START = 'redux-example/widgets/EDIT_START'; +const EDIT_STOP = 'redux-example/widgets/EDIT_STOP'; +const SAVE = 'redux-example/widgets/SAVE'; +const SAVE_SUCCESS = 'redux-example/widgets/SAVE_SUCCESS'; +const SAVE_FAIL = 'redux-example/widgets/SAVE_FAIL'; const initialState = { loaded: false, @@ -15,14 +13,14 @@ const initialState = { saveError: {} }; -export default function widgets(state = initialState, action = {}) { +export default function reducer(state = initialState, action = {}) { switch (action.type) { - case WIDGET_LOAD: + case LOAD: return { ...state, loading: true }; - case WIDGET_LOAD_SUCCESS: + case LOAD_SUCCESS: return { ...state, loading: false, @@ -30,7 +28,7 @@ export default function widgets(state = initialState, action = {}) { data: action.result, error: null }; - case WIDGET_LOAD_FAIL: + case LOAD_FAIL: return { ...state, loading: false, @@ -38,7 +36,7 @@ export default function widgets(state = initialState, action = {}) { data: null, error: action.error }; - case WIDGET_EDIT_START: + case EDIT_START: return { ...state, editing: { @@ -46,7 +44,7 @@ export default function widgets(state = initialState, action = {}) { [action.id]: true } }; - case WIDGET_EDIT_STOP: + case EDIT_STOP: return { ...state, editing: { @@ -54,9 +52,9 @@ export default function widgets(state = initialState, action = {}) { [action.id]: false } }; - case WIDGET_SAVE: + case SAVE: return state; // 'saving' flag handled by redux-form - case WIDGET_SAVE_SUCCESS: + case SAVE_SUCCESS: const data = [...state.data]; data[action.result.id - 1] = action.result; return { @@ -71,7 +69,7 @@ export default function widgets(state = initialState, action = {}) { [action.id]: null } }; - case WIDGET_SAVE_FAIL: + case SAVE_FAIL: return { ...state, saveError: { @@ -87,3 +85,28 @@ export default function widgets(state = initialState, action = {}) { export function isLoaded(globalState) { return globalState.widgets && globalState.widgets.loaded; } + +export function load() { + return { + types: [LOAD, LOAD_SUCCESS, LOAD_FAIL], + promise: (client) => client.get('/loadWidgets') + }; +} + +export function save(widget) { + return { + types: [SAVE, SAVE_SUCCESS, SAVE_FAIL], + id: widget.id, + promise: (client) => client.post('/updateWidget', { + data: widget + }) + }; +} + +export function editStart(id) { + return { type: EDIT_START, id }; +} + +export function editStop(id) { + return { type: EDIT_STOP, id }; +} diff --git a/src/reducers/auth.js b/src/reducers/auth.js deleted file mode 100644 index d3b868bbc..000000000 --- a/src/reducers/auth.js +++ /dev/null @@ -1,80 +0,0 @@ -import { - AUTH_LOAD, - AUTH_LOAD_SUCCESS, - AUTH_LOAD_FAIL, - AUTH_LOGIN, - AUTH_LOGIN_SUCCESS, - AUTH_LOGIN_FAIL, - AUTH_LOGOUT, - AUTH_LOGOUT_SUCCESS, - AUTH_LOGOUT_FAIL -} from '../actions/actionTypes'; - -const initialState = { - loaded: false -}; - -export default function info(state = initialState, action = {}) { - switch (action.type) { - case AUTH_LOAD: - return { - ...state, - loading: true - }; - case AUTH_LOAD_SUCCESS: - return { - ...state, - loading: false, - loaded: true, - user: action.result - }; - case AUTH_LOAD_FAIL: - return { - ...state, - loading: false, - loaded: false, - error: action.error - }; - case AUTH_LOGIN: - return { - ...state, - loggingIn: true - }; - case AUTH_LOGIN_SUCCESS: - return { - ...state, - loggingIn: false, - user: action.result - }; - case AUTH_LOGIN_FAIL: - return { - ...state, - loggingIn: false, - user: null, - loginError: action.error - }; - case AUTH_LOGOUT: - return { - ...state, - loggingOut: true - }; - case AUTH_LOGOUT_SUCCESS: - return { - ...state, - loggingOut: false, - user: null - }; - case AUTH_LOGOUT_FAIL: - return { - ...state, - loggingOut: false, - logoutError: action.error - }; - default: - return state; - } -} - -export function isLoaded(globalState) { - return globalState.auth && globalState.auth.loaded; -} diff --git a/src/reducers/counter.js b/src/reducers/counter.js deleted file mode 100644 index 46730b4af..000000000 --- a/src/reducers/counter.js +++ /dev/null @@ -1,19 +0,0 @@ -import { - COUNTER_INCREMENT -} from '../actions/actionTypes'; - -const initialState = { - count: 0 -}; - -export default function counter(state = initialState, action = {}) { - switch (action.type) { - case COUNTER_INCREMENT: - const {count} = state; - return { - count: count + 1 - }; - default: - return state; - } -} diff --git a/src/redux/create.js b/src/redux/create.js index 84e72669c..dc4d96564 100644 --- a/src/redux/create.js +++ b/src/redux/create.js @@ -16,13 +16,13 @@ export default function createApiClientStore(client, data) { finalCreateStore = applyMiddleware(middleware)(createStore); } - const reducer = combineReducers(require('../reducers/index')); + const reducer = combineReducers(require('../ducks/index')); const store = finalCreateStore(reducer, data); store.client = client; if (module.hot) { - module.hot.accept('../reducers/index', () => { - const nextReducer = combineReducers(require('../reducers/index')); + module.hot.accept('../ducks/index', () => { + const nextReducer = combineReducers(require('../ducks/index')); store.replaceReducer(nextReducer); }); } diff --git a/src/views/App.js b/src/views/App.js index af8fa7ab8..fd7be99ca 100755 --- a/src/views/App.js +++ b/src/views/App.js @@ -3,10 +3,8 @@ import {Link} from 'react-router'; import {bindActionCreators} from 'redux'; import {connect} from 'react-redux'; import DocumentMeta from 'react-document-meta'; -import {isLoaded as isInfoLoaded} from '../reducers/info'; -import {isLoaded as isAuthLoaded} from '../reducers/auth'; -import {load as loadInfo} from '../actions/infoActions'; -import {load as loadAuth, logout} from '../actions/authActions'; +import {isLoaded as isInfoLoaded, load as loadInfo} from '../ducks/info'; +import {isLoaded as isAuthLoaded, load as loadAuth, logout} from '../ducks/auth'; import InfoBar from '../components/InfoBar'; import {createTransitionHook} from '../universalRouter'; diff --git a/src/views/Login.js b/src/views/Login.js index a322fdd96..a57fffad3 100755 --- a/src/views/Login.js +++ b/src/views/Login.js @@ -2,9 +2,8 @@ import React, {Component, PropTypes} from 'react'; import {bindActionCreators} from 'redux'; import {connect} from 'react-redux'; import DocumentMeta from 'react-document-meta'; -import {isLoaded as isAuthLoaded} from '../reducers/auth'; -import * as authActions from '../actions/authActions'; -import {load as loadAuth} from '../actions/authActions'; +import * as authActions from '../ducks/auth'; +import {isLoaded as isAuthLoaded, load as loadAuth} from '../ducks/auth'; @connect( state => ({user: state.auth.user}), diff --git a/src/views/LoginSuccess.js b/src/views/LoginSuccess.js index e1a9e1410..91d336c5a 100755 --- a/src/views/LoginSuccess.js +++ b/src/views/LoginSuccess.js @@ -1,9 +1,8 @@ import React, {Component, PropTypes} from 'react'; import {bindActionCreators} from 'redux'; import {connect} from 'react-redux'; -import {isLoaded as isAuthLoaded} from '../reducers/auth'; -import {load as loadAuth} from '../actions/authActions'; -import * as authActions from '../actions/authActions'; +import {isLoaded as isAuthLoaded, load as loadAuth} from '../ducks/auth'; +import * as authActions from '../ducks/auth'; @connect( state => ({user: state.auth.user}), diff --git a/src/views/Widgets.js b/src/views/Widgets.js index 728eaec97..8870e260f 100755 --- a/src/views/Widgets.js +++ b/src/views/Widgets.js @@ -1,10 +1,9 @@ import React, {Component, PropTypes} from 'react'; import {bindActionCreators} from 'redux'; import DocumentMeta from 'react-document-meta'; -import {isLoaded} from '../reducers/widgets'; import {connect} from 'react-redux'; -import * as widgetActions from '../actions/widgetActions'; -import {load as loadWidgets} from '../actions/widgetActions'; +import * as widgetActions from '../ducks/widgets'; +import {isLoaded, load as loadWidgets} from '../ducks/widgets'; import {initializeWithKey} from 'redux-form'; import WidgetForm from '../components/WidgetForm';