2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
16 import {actionTypes} from './PlainDataReducerConstants.js';
17 import Validator from 'nfvo-utils/Validator.js';
18 import forOwn from 'lodash/forOwn.js';
20 function updateDataAndValidateReducer(state = {}, action) {
21 let genericFieldInfoCopy;
22 switch (action.type) {
23 case actionTypes.DATA_CHANGED:
24 let changed = action.deltaData;
25 if (!action.formName || (state.formName !== action.formName)) {return {...state};}
26 genericFieldInfoCopy = {...state.genericFieldInfo};
27 forOwn(changed,(value, key) => {
28 if (state.genericFieldInfo[key]) {
29 let result = Validator.validate(key, value, state.genericFieldInfo[key].validations, state, action.customValidations);
30 genericFieldInfoCopy[key] = {...genericFieldInfoCopy[key], isValid: result.isValid, errorText: result.errorText};
40 genericFieldInfo: genericFieldInfoCopy
42 case actionTypes.VALIDATE_FORM:
43 if (!action.formName || (state.formName !== action.formName)) {return {...state};}
44 genericFieldInfoCopy = {...state.genericFieldInfo};
46 forOwn(state.genericFieldInfo,(value, key) => {
47 let val = state.data && state.data[key] ? state.data[key] : '';
48 let result = Validator.validate(key, val, state.genericFieldInfo[key].validations, state, {});
49 genericFieldInfoCopy[key] = {...genericFieldInfoCopy[key], isValid: result.isValid, errorText: result.errorText};
50 if (!result.isValid) {
57 genericFieldInfo: genericFieldInfoCopy
59 case actionTypes.VALIDATE_DATA:
60 let specificFields = action.data;
61 if (!action.formName || (state.formName !== action.formName)) {return {...state};}
62 genericFieldInfoCopy = {...state.genericFieldInfo};
63 forOwn(specificFields,(value, key) => {
64 let result = Validator.validate(key, value, state.genericFieldInfo[key].validations, state, action.customValidations);
65 genericFieldInfoCopy[key] = {...genericFieldInfoCopy[key], isValid: result.isValid, errorText: result.errorText};
70 genericFieldInfo: genericFieldInfoCopy
77 export function createPlainDataReducer(loadReducer) {
78 return (state = {}, action) => {
79 if(action.type === actionTypes.VALIDATE_DATA ||
80 action.type === actionTypes.VALIDATE_FORM ||
81 action.type === actionTypes.DATA_CHANGED
83 return updateDataAndValidateReducer(state, action);
85 return loadReducer(state, action);