[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / src / sdc-app / common / reducers / PlainDataReducer.js
1 /*!
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  */
16 import {actionTypes} from './PlainDataReducerConstants.js';
17 import Validator from 'nfvo-utils/Validator.js';
18 import forOwn from 'lodash/forOwn.js';
19
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};
31                                 }
32                         });
33                         return {
34                                 ...state,
35                                 formReady: null,
36                                 data: {
37                                         ...state.data,
38                                         ...action.deltaData
39                                 },
40                                 genericFieldInfo: genericFieldInfoCopy
41                         };
42                 case actionTypes.VALIDATE_FORM:
43                         if (!action.formName || (state.formName !== action.formName)) {return {...state};}
44                         genericFieldInfoCopy = {...state.genericFieldInfo};
45                         let formReady = true;
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) {
51                                         formReady = false;
52                                 }
53                         });
54                         return {
55                                 ...state,
56                                 formReady,
57                                 genericFieldInfo: genericFieldInfoCopy
58                         };
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};
66                         });
67                         return {
68                                 ...state,
69                                 formReady: null,
70                                 genericFieldInfo: genericFieldInfoCopy
71                         };
72                 default:
73                         return state;
74         }
75 };
76
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
82                 ) {
83                         return updateDataAndValidateReducer(state, action);
84                 } else {
85                         return loadReducer(state, action);
86                 }
87         };
88 };
89
90
91
92
93
94