verificator
Version:
Client and server-side validation JavaScript library
24 lines (23 loc) • 732 B
JavaScript
import { ADD_ERROR, REMOVE_ERROR, CLEAR_ERRORS } from '../constants/types';
const initialState = {};
const reducer = (state = initialState, action) => {
const { type } = action;
if (type === ADD_ERROR) {
const { key, message } = action.payload;
const errors = state[key] || [];
if (errors.indexOf(message) === -1) {
return Object.assign({}, state, { [key]: [...errors, message] });
}
}
else if (type === REMOVE_ERROR) {
const { key } = action.payload;
if (key in state) {
return Object.assign({}, state, { [key]: [] });
}
}
else if (type === CLEAR_ERRORS) {
return {};
}
return state;
};
export default reducer;