Refactored reducers

This commit is contained in:
Andrew Revinsky (DART)
2016-03-22 19:51:00 +03:00
parent 9967ad9c52
commit bdb4d3db26
15 changed files with 217 additions and 294 deletions

View File

@@ -19,7 +19,7 @@ export function configureError({errors, ...props} = {}) {
return {
...props,
type: T.AUTH.CONFIGURE_ERROR,
errors
error: errors
};
}

View File

@@ -74,7 +74,7 @@ class EmailSignInForm extends React.Component {
name="email"
disabled={disabled}
value={read(this.props.auth, 'signIn.form.email', '')}
errors={read(this.props.auth, 'signIn.errors.email', {})}
errors={read(this.props.auth, 'signIn.errors.email', [])}
onChange={this.handleInput.bind(this, "email")}
{...this.props.inputProps.email} />

View File

@@ -3,20 +3,35 @@
*/
import T from '../../constants/ACTION_TYPES';
const authInitialState = {
const initialState = {
loading: false,
valid: false,
errors: null
};
export const authReducer = (state = {...authInitialState}, action) => {
export const authReducer = (state = {...initialState}, action) => {
switch(action.type) {
case T.AUTH.AUTHENTICATE_START: return {...state, loading: true };
case T.AUTH.AUTHENTICATE_COMPLETE: return {...state, loading: false,
errors: null,
valid: true};
case T.AUTH.AUTHENTICATE_ERROR: return {...state, loading: false,
errors: "Invalid token",
valid: false};
case T.AUTH.AUTHENTICATE_START:
return {
...state,
loading: true
};
case T.AUTH.AUTHENTICATE_COMPLETE:
return {
...state,
loading: false,
errors: null,
valid: true
};
case T.AUTH.AUTHENTICATE_ERROR:
return {
...state,
loading: false,
errors: "Invalid token",
valid: false
};
default: return state;
}

View File

@@ -2,36 +2,55 @@
* Created by andrew on 25/02/16.
*/
import T from '../../constants/ACTION_TYPES';
import createDataReducer from '../createDataReducer';
const configInitialState = {
loading: true,
errors: null,
config: null
};
export const configReducer = (state = {...configInitialState}, action) => {
switch(action.type) {
case T.AUTH.CONFIGURE_START:
return {
...state,
loading: true
};
case T.AUTH.CONFIGURE_COMPLETE:
const { config } = action;
return {
...state,
loading: false,
errors: null,
config
};
case T.AUTH.CONFIGURE_ERROR:
const { errors } = action;
return {
...state,
loading: false,
errors
};
default:
return state;
}
};
export const configReducer = createDataReducer([
T.AUTH.CONFIGURE_START,
T.AUTH.CONFIGURE_COMPLETE,
T.AUTH.CONFIGURE_ERROR
],
'config',
'config',
(c = {}) => ({ ...c })
);
//
// const configInitialState = {
// loading: true,
// errors: null,
// config: null
// };
//
// export const configReducer = (state = {...configInitialState}, action) => {
// switch(action.type) {
// case T.AUTH.CONFIGURE_START:
// {
// return {
// ...state,
// loading: true
// };
// }
//
// case T.AUTH.CONFIGURE_COMPLETE: {
// const { config } = action;
// return {
// ...state,
// loading: false,
// errors: null,
// config
// };
// }
//
// case T.AUTH.CONFIGURE_ERROR:
// {
// const { errors } = action;
// return {
// ...state,
// loading: false,
// errors
// };
// }
//
// default:
// return state;
// }
// };

View File

@@ -2,41 +2,11 @@
* Created by andrew on 25/02/16.
*/
import T from '../../constants/ACTION_TYPES';
import createFormReducer from '../createFormReducer';
const signInInitialState = {
loading: false,
errors: null,
form: {}
};
export const signInReducer = (state = { ...signInInitialState, form: {...signInInitialState.form }}, action) => {
switch(action.type) {
case T.AUTH.SIGN_IN_START:
return {
...state,
loading: true
};
case T.AUTH.SIGN_IN_COMPLETE:
return {
...signInInitialState,
form: { ...signInInitialState.form }
};
case T.AUTH.SIGN_IN_ERROR:
const { errors } = action;
return {
...state,
loading: false,
errors
};
case T.AUTH.SIGN_IN_FORM_UPDATE:
const { key, value } = action;
return {
...state,
form: {
...state.form,
[key]: value
}
};
default: return state;
}
};
export const signInReducer = createFormReducer([
T.AUTH.SIGN_IN_START,
T.AUTH.SIGN_IN_COMPLETE,
T.AUTH.SIGN_IN_ERROR,
T.AUTH.SIGN_IN_FORM_UPDATE
]);

View File

@@ -2,46 +2,11 @@
* Created by andrew on 25/02/16.
*/
import T from '../../constants/ACTION_TYPES';
import createFormReducer from '../createFormReducer';
const signUpInitialState = {
loading: false,
errors: null,
form: {}
};
export const signUpReducer = (state = { ...signUpInitialState, form: {...signUpInitialState.form }}, action) => {
switch(action.type) {
case T.AUTH.SIGN_UP_START:
return {
...state,
loading: true
};
case T.AUTH.SIGN_UP_COMPLETE:
return {
...signUpInitialState,
form: { ...signUpInitialState.form }
};
case T.AUTH.SIGN_UP_ERROR:
const { errors } = action;
return {
...state,
loading: false,
errors
};
case T.AUTH.SIGN_UP_FORM_UPDATE:
const { key, value } = action;
return {
...state,
errors: {
...(state.errors || {}),
[key]: []
},
form: {
...state.form,
[key]: value
}
};
default: return state;
}
};
export const signUpReducer = createFormReducer([
T.AUTH.SIGN_UP_START,
T.AUTH.SIGN_UP_COMPLETE,
T.AUTH.SIGN_UP_ERROR,
T.AUTH.SIGN_UP_FORM_UPDATE
]);

View File

@@ -3,12 +3,12 @@
*/
import T from '../../constants/ACTION_TYPES';
const userInitalState = {
const initalState = {
attributes: null,
isSignedIn: false
};
export const userReducer = (state = {...userInitalState}, action) => {
export const userReducer = (state = {...initalState}, action) => {
switch(action.type) {
case T.AUTH.AUTHENTICATE_COMPLETE:
case T.AUTH.SIGN_IN_COMPLETE: {
@@ -21,7 +21,7 @@ export const userReducer = (state = {...userInitalState}, action) => {
case T.AUTH.SIGN_OUT_COMPLETE:
case T.AUTH.AUTHENTICATE_ERROR:
return {
...userInitalState
...initalState
};
default: return state;
}

View File

@@ -0,0 +1,43 @@
/**
* Created by andrew on 3/22/16.
*/
const createDataReducer = ([KEY_REQUEST, KEY_SUCCESS, KEY_ERROR], payloadActionNameProp = 'payload', payloadStateNameProp = 'data', payloadAssignFn = (k = []) => [...k]) => {
const initialState = {
loading: false,
errors: {},
[payloadStateNameProp]: payloadAssignFn()
};
return function formReducer(state = {...initialState}, action) {
switch(action.type) {
case KEY_REQUEST: {
return {
...state,
loading: true
}
}
case KEY_SUCCESS: {
const payload = action[payloadActionNameProp];
return {
...initialState,
[payloadStateNameProp]: payloadAssignFn(payload)
};
}
case KEY_ERROR:
{
const {error} = action;
return {
...state,
loading: false,
errors: Object.isSealed(error) ? {aggregate: error} : {...error}
}
}
default:
return state;
}
};
};
export default createDataReducer;

View File

@@ -0,0 +1,56 @@
/**
* Created by andrew on 3/22/16.
*/
const createFormReducer = ([KEY_REQUEST, KEY_SUCCESS, KEY_ERROR, KEY_UPDATE]) => {
const initialState = {
loading: false,
form: {},
errors: {}
};
return function formReducer(state = {...initialState}, action) {
switch(action.type) {
case KEY_REQUEST: {
return {
...state,
loading: true
}
}
case KEY_ERROR: {
const { error } = action;
return {
...state,
loading: false,
errors: Object.isSealed(error) ? { aggregate: error } : { ...error }
}
}
case KEY_SUCCESS: {
return {
...initialState
}
}
case KEY_UPDATE: {
const { key, value } = action;
return {
...state,
form: {
...state.form,
[key]: value
},
errors: {
...state.errors,
aggregate: null,
[key]: null
}
}
}
default:
return state;
}
};
};
export default createFormReducer;

View File

@@ -6,6 +6,7 @@
*/
import T from '../../constants/ACTION_TYPES';
import { combineReducers } from 'redux';
import createFormReducer from '../createFormReducer';
const ownAccountsReducer = (state = [], action ) => {
switch (action.type) {
@@ -44,53 +45,13 @@ const otherAccountsReducer = (state = [], action ) => {
}
};
const createAccountInitialState = {
form: {},
loading: false,
errors: {}
};
const createAccountReducer = (state = { ...createAccountInitialState }, action ) => {
switch(action.type) {
case T.ACCOUNTS.CREATE_START: {
return {
...state,
loading: true
};
}
case T.ACCOUNTS.CREATE_COMPLETE: {
return {
...createAccountInitialState
};
}
case T.ACCOUNTS.CREATE_ERROR: {
const { error = {} } = action;
return {
...state,
loading: false,
errors: {
...error
}
};
}
case T.ACCOUNTS.CREATE_FORM_UPDATE: {
const { key, value } = action;
return {
...state,
form: {
...state.form,
[key]: value
},
errors: {
...state.errors,
[key]: []
}
};
}
default:
return state;
}
};
const createAccountReducer = createFormReducer([
T.ACCOUNTS.CREATE_START,
T.ACCOUNTS.CREATE_COMPLETE,
T.ACCOUNTS.CREATE_ERROR,
T.ACCOUNTS.CREATE_FORM_UPDATE
]);
export const accounts = combineReducers({
own: ownAccountsReducer,
@@ -98,42 +59,3 @@ export const accounts = combineReducers({
create: createAccountReducer
});
//export const accounts = (state = {...initialState}, action) => {
// switch(action.type) {
// case T.ACCOUNTS.LIST_START:
// case T.ACCOUNTS.LIST_COMPLETE:
// case T.ACCOUNTS.LIST_ERROR:
//
// //case T.ACCOUNTS.LIST_REF_START:
// case T.AUTH.AUTHENTICATE_COMPLETE:
// case T.AUTH.SIGN_IN_COMPLETE: {
// const { user } = action;
// const { toAccounts = [] } = user;
// return accounts(state, {
// type: T.ACCOUNTS.LIST_REF_COMPLETE, data: toAccounts
// });
// }
//
// case T.ACCOUNTS.LIST_REF_COMPLETE: {
// const { data = [] } = action;
// return {
// ...state,
// other: [
// ...data
// ]
// };
// }
// //case T.ACCOUNTS.LIST_REF_ERROR:
//
//
//
// case T.ACCOUNTS.CREATE_REF_START:
// case T.ACCOUNTS.CREATE_REF_COMPLETE:
// case T.ACCOUNTS.CREATE_REF_ERROR:
// case T.ACCOUNTS.CREATE_REF_FORM_UPDATE:
//
// default:
// return state;
// }
//};

View File

@@ -5,39 +5,10 @@
* Created by andrew on 15/03/16.
*/
import T from '../../constants/ACTION_TYPES';
import createListReducer from '../createDataReducer';
const initialState = {
loading: false,
errors: {},
data: []
};
export const transfers = (state = {...initialState}, action) => {
switch(action.type) {
case T.TRANSFERS.LIST_START: {
return {
...state,
loading: true
};
}
case T.TRANSFERS.LIST_COMPLETE: {
const { payload } = action;
return {
...initialState,
data: [...payload]
};
}
case T.TRANSFERS.LIST_ERROR: {
const { error } = action;
return {
...state,
loading: false,
errors: Object.isSealed(error) ? { message: error } : { ...error }
};
}
default:
return state;
}
};
export const transfers = createListReducer([
T.TRANSFERS.LIST_START,
T.TRANSFERS.LIST_COMPLETE,
T.TRANSFERS.LIST_ERROR
]);

View File

@@ -11,6 +11,7 @@ export const error = (state = null, action ) => {
}
case T.ERROR.START:
return action.payload;
default:
return state;
}

View File

@@ -5,50 +5,11 @@
* Created by andrew on 15/03/16.
*/
import T from '../../constants/ACTION_TYPES';
import createFormReducer from '../createFormReducer';
const initialState = {
loading: false,
form: {},
errors: {}
};
export const transfersMake = (state = {...initialState}, action) => {
switch(action.type) {
case T.TRANSFERS.MAKE_START: {
return {
...state,
loading: true
}
}
case T.TRANSFERS.MAKE_ERROR: {
const { error } = action;
return {
...state,
loading: false,
errors: Object.isSealed(error) ? { message: error } : { ...error }
};
}
case T.TRANSFERS.MAKE_COMPLETE: {
return {
...initialState
}
}
case T.TRANSFERS.MAKE_FORM_UPDATE: {
const { key, value } = action;
return {
...state,
form: {
...state.form,
[key]: value
},
errors: {
...state.errors,
[key]: null
}
}
}
default:
return state;
}
};
export const transfersMake = createFormReducer([
T.TRANSFERS.MAKE_START,
T.TRANSFERS.MAKE_COMPLETE,
T.TRANSFERS.MAKE_ERROR,
T.TRANSFERS.MAKE_FORM_UPDATE
]);

View File

@@ -18,7 +18,7 @@ export function parseResponse (response) {
if (!message) {
return rest;
}
const jvmPattern = /\[Field error in object 'customerInfo' on field '(\w+)'/gm;
const jvmPattern = /\[Field error in object '\w+' on field '(\w+)'/gm;
let errors = {};
message.replace(jvmPattern, (m, name) => {
errors[name] = ['Required'];

View File

@@ -5,10 +5,10 @@ export default function read(src, path = '', defaultVal = null) {
const [pathItem = null, ...rest] = path.split('.');
if (pathItem === null ) {
return src;
return src || defaultVal;
} else if (rest.length === 0) {
if (!src) { return defaultVal; }
return src[pathItem];
return src[pathItem] || defaultVal;
}
if (!src) { return defaultVal; }