2018-10-16 22:31:33 +00:00
|
|
|
/* @flow */
|
|
|
|
import type { Action } from 'flowtype';
|
2018-11-22 13:20:37 +00:00
|
|
|
|
2018-11-22 14:35:25 +00:00
|
|
|
import * as ACCOUNT from 'actions/constants/account';
|
2018-11-21 18:32:28 +00:00
|
|
|
import * as ACTION from 'actions/constants/signVerify';
|
2018-10-17 15:57:21 +00:00
|
|
|
|
2018-11-27 16:11:46 +00:00
|
|
|
export type Error = {
|
|
|
|
inputName: string,
|
|
|
|
message: ?string,
|
|
|
|
};
|
|
|
|
|
2018-10-16 22:31:33 +00:00
|
|
|
export type State = {
|
2018-11-21 18:19:14 +00:00
|
|
|
signAddress: string,
|
|
|
|
signMessage: string,
|
|
|
|
signSignature: string,
|
|
|
|
verifyAddress: string,
|
|
|
|
verifyMessage: string,
|
|
|
|
verifySignature: string,
|
2018-11-27 16:11:46 +00:00
|
|
|
touched: Array<string>,
|
2019-03-04 12:33:02 +00:00
|
|
|
errors: Array<Error>,
|
|
|
|
};
|
2018-10-16 22:31:33 +00:00
|
|
|
|
|
|
|
export const initialState: State = {
|
2018-11-21 18:19:14 +00:00
|
|
|
signAddress: '',
|
|
|
|
signMessage: '',
|
|
|
|
signSignature: '',
|
|
|
|
verifyAddress: '',
|
|
|
|
verifyMessage: '',
|
|
|
|
verifySignature: '',
|
|
|
|
touched: [],
|
2018-11-27 16:11:46 +00:00
|
|
|
errors: [],
|
2018-10-16 22:31:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default (state: State = initialState, action: Action): State => {
|
|
|
|
switch (action.type) {
|
2018-11-21 18:32:28 +00:00
|
|
|
case ACTION.SIGN_SUCCESS:
|
2018-10-17 15:57:21 +00:00
|
|
|
return {
|
2018-11-22 13:20:37 +00:00
|
|
|
...state,
|
|
|
|
signSignature: action.signSignature,
|
2018-10-18 12:18:48 +00:00
|
|
|
};
|
|
|
|
|
2018-11-27 16:11:46 +00:00
|
|
|
case ACTION.ERROR: {
|
|
|
|
const { inputName } = action;
|
|
|
|
if (!state.errors.some(e => e.inputName === inputName)) {
|
|
|
|
const error = { inputName, message: action.message };
|
2018-11-21 18:19:14 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2018-11-27 16:11:46 +00:00
|
|
|
errors: [...state.errors, error],
|
2018-11-21 18:19:14 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2018-11-27 16:11:46 +00:00
|
|
|
case ACTION.TOUCH: {
|
|
|
|
const { inputName } = action;
|
|
|
|
if (!state.touched.includes(inputName)) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
touched: [...state.touched, action.inputName],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
errors: state.errors.filter(error => error.inputName !== inputName),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-21 18:32:28 +00:00
|
|
|
case ACTION.INPUT_CHANGE: {
|
2018-11-27 16:11:46 +00:00
|
|
|
const change = { [action.inputName]: action.value };
|
2018-11-22 13:20:37 +00:00
|
|
|
return { ...state, ...change };
|
2018-11-21 18:19:14 +00:00
|
|
|
}
|
|
|
|
|
2018-11-22 14:35:25 +00:00
|
|
|
case ACCOUNT.DISPOSE:
|
|
|
|
return initialState;
|
|
|
|
|
2018-11-21 18:32:28 +00:00
|
|
|
case ACTION.CLEAR_SIGN:
|
2018-10-18 22:55:16 +00:00
|
|
|
return {
|
2018-11-21 18:32:28 +00:00
|
|
|
...state,
|
|
|
|
signAddress: '',
|
|
|
|
signMessage: '',
|
|
|
|
signSignature: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
case ACTION.CLEAR_VERIFY:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
verifyAddress: '',
|
|
|
|
verifyMessage: '',
|
|
|
|
verifySignature: '',
|
2018-10-18 22:55:16 +00:00
|
|
|
};
|
|
|
|
|
2018-10-16 22:31:33 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2019-03-04 12:33:02 +00:00
|
|
|
};
|