blob: 2276984f7a9d2abdbae6dcbf7027f6cb90c671ee [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16import {actionTypes} from './PlainDataReducerConstants.js';
17import Validator from 'nfvo-utils/Validator.js';
18import forOwn from 'lodash/forOwn.js';
Avi Ziv61070c92017-07-26 17:37:57 +030019import {other as optionInputOther} from 'nfvo-components/input/inputOptions/InputOptions.jsx';
AviZi280f8012017-06-09 02:39:56 +030020
21function updateDataAndValidateReducer(state = {}, action) {
22 let genericFieldInfoCopy;
23 switch (action.type) {
24 case actionTypes.DATA_CHANGED:
25 let changed = action.deltaData;
26 if (!action.formName || (state.formName !== action.formName)) {return {...state};}
27 genericFieldInfoCopy = {...state.genericFieldInfo};
28 forOwn(changed,(value, key) => {
29 if (state.genericFieldInfo[key]) {
30 let result = Validator.validate(key, value, state.genericFieldInfo[key].validations, state, action.customValidations);
31 genericFieldInfoCopy[key] = {...genericFieldInfoCopy[key], isValid: result.isValid, errorText: result.errorText};
32 }
33 });
34 return {
35 ...state,
36 formReady: null,
37 data: {
38 ...state.data,
39 ...action.deltaData
40 },
41 genericFieldInfo: genericFieldInfoCopy
42 };
43 case actionTypes.VALIDATE_FORM:
44 if (!action.formName || (state.formName !== action.formName)) {return {...state};}
45 genericFieldInfoCopy = {...state.genericFieldInfo};
46 let formReady = true;
47 forOwn(state.genericFieldInfo,(value, key) => {
48 let val = state.data && state.data[key] ? state.data[key] : '';
49 let result = Validator.validate(key, val, state.genericFieldInfo[key].validations, state, {});
Avi Ziv61070c92017-07-26 17:37:57 +030050 if(val.choice !== undefined) {
51 result = Validator.validate(key, val.choice, state.genericFieldInfo[key].validations, state, {});
52 }
53 if(val.choice !== undefined && val.choice === optionInputOther.OTHER) {
54 result = Validator.validate(key, val.other, state.genericFieldInfo[key].validations, state, {});
55 }
AviZi280f8012017-06-09 02:39:56 +030056 genericFieldInfoCopy[key] = {...genericFieldInfoCopy[key], isValid: result.isValid, errorText: result.errorText};
57 if (!result.isValid) {
58 formReady = false;
59 }
60 });
61 return {
62 ...state,
63 formReady,
64 genericFieldInfo: genericFieldInfoCopy
65 };
66 case actionTypes.VALIDATE_DATA:
67 let specificFields = action.data;
68 if (!action.formName || (state.formName !== action.formName)) {return {...state};}
69 genericFieldInfoCopy = {...state.genericFieldInfo};
70 forOwn(specificFields,(value, key) => {
71 let result = Validator.validate(key, value, state.genericFieldInfo[key].validations, state, action.customValidations);
72 genericFieldInfoCopy[key] = {...genericFieldInfoCopy[key], isValid: result.isValid, errorText: result.errorText};
73 });
74 return {
75 ...state,
76 formReady: null,
77 genericFieldInfo: genericFieldInfoCopy
78 };
79 default:
80 return state;
81 }
82};
83
84export function createPlainDataReducer(loadReducer) {
85 return (state = {}, action) => {
86 if(action.type === actionTypes.VALIDATE_DATA ||
87 action.type === actionTypes.VALIDATE_FORM ||
88 action.type === actionTypes.DATA_CHANGED
89 ) {
90 return updateDataAndValidateReducer(state, action);
91 } else {
92 return loadReducer(state, action);
93 }
94 };
95};
96
97
98
99
100
101