AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 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 './JSONSchemaReducerConstants.js'; |
| 17 | import Validator from 'nfvo-utils/Validator.js'; |
| 18 | import JSONSchema from 'nfvo-utils/json/JSONSchema.js'; |
| 19 | import JSONPointer from 'nfvo-utils/json/JSONPointer.js'; |
| 20 | import forOwn from 'lodash/forOwn.js'; |
| 21 | import isArray from 'lodash/isArray.js'; |
| 22 | |
| 23 | |
| 24 | function flattenData(data, result, pointer = '') { |
| 25 | let newPointer = pointer; |
| 26 | if (typeof data === 'object' && !isArray(data)) { |
| 27 | for (let i in data) { |
| 28 | newPointer = newPointer ? newPointer + '/' + i : i; |
| 29 | flattenData(data[i], result, newPointer); |
| 30 | newPointer = pointer; |
| 31 | } |
| 32 | } else { |
| 33 | result[newPointer] = data; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | function updateSchemaDataAndValidateReducer (state = {}, action, questionnaireName) { |
| 38 | let genericFieldInfoClone; |
| 39 | switch (action.type) { |
| 40 | case actionTypes.DATA_LOADED: |
| 41 | if (questionnaireName !== action.qName) {return {...state};} |
| 42 | const schema = action.payload.qschema; |
| 43 | let schemaLoader = new JSONSchema(); |
| 44 | schemaLoader.setSchema(schema); |
| 45 | schemaLoader.setSupportedValidationFunctions(Object.keys(Validator.globalValidationFunctions)); |
| 46 | let {genericFieldInfo} = schemaLoader.flattenSchema(); |
| 47 | |
| 48 | let data = action.payload.qdata; |
| 49 | let dataMap = {}; |
| 50 | flattenData(data, dataMap); |
| 51 | |
| 52 | return { |
| 53 | ...state, |
| 54 | qdata: action.payload.qdata, // the original hierarchical data. to be used for submit and save |
| 55 | qgenericFieldInfo : genericFieldInfo, // information about the fields that the view will require and reducer will need, such as validations, enum to use, etc. |
| 56 | dataMap // flattened schema data for ease of use |
| 57 | }; |
| 58 | |
| 59 | case actionTypes.DATA_CHANGED: |
| 60 | let changedData = action.deltaData; |
| 61 | if (questionnaireName !== action.qName) {return {...state};} |
| 62 | |
| 63 | genericFieldInfoClone = {...state.qgenericFieldInfo}; |
| 64 | let qDataClone = {...state.qdata}; |
| 65 | let dataMapClone = {...state.dataMap}; |
| 66 | |
| 67 | forOwn(changedData,(value, key) => { |
| 68 | if (state.qgenericFieldInfo[key]) { |
| 69 | let result = Validator.validate(key, value, state.qgenericFieldInfo[key].validations, state, action.customValidations); |
| 70 | genericFieldInfoClone[key] = {...genericFieldInfoClone[key], isValid: result.isValid, errorText: result.errorText}; |
| 71 | qDataClone = JSONPointer.setValue(state.qdata, '/' + key, value); |
| 72 | dataMapClone[key] = value; |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | return { |
| 77 | ...state, |
| 78 | qdata: qDataClone, |
| 79 | dataMap: dataMapClone, |
| 80 | qgenericFieldInfo: genericFieldInfoClone |
| 81 | }; |
| 82 | |
| 83 | case actionTypes.VALIDATE_DATA: |
| 84 | let specificFields = action.data; |
| 85 | if (questionnaireName !== action.qName) {return {...state};} |
| 86 | genericFieldInfoClone = {...state.qgenericFieldInfo}; |
| 87 | forOwn(specificFields,(value, key) => { |
| 88 | let result = Validator.validate(key, value, state.qgenericFieldInfo[key].validations, state, action.customValidations); |
| 89 | genericFieldInfoClone[key] = {...genericFieldInfoClone[key], isValid: result.isValid, errorText: result.errorText}; |
| 90 | }); |
| 91 | return { |
| 92 | ...state, |
| 93 | formReady: null, |
| 94 | qgenericFieldInfo: genericFieldInfoClone |
| 95 | }; |
| 96 | |
| 97 | case actionTypes.VALIDATE_FORM: |
| 98 | if (questionnaireName !== action.qName) {return {...state};} |
| 99 | genericFieldInfoClone = {...state.qgenericFieldInfo}; |
| 100 | let formReady = true; |
| 101 | forOwn(state.qgenericFieldInfo,(value, key) => { |
| 102 | let val = state.data[key] ? state.data[key] : ''; |
| 103 | let result = Validator.validate(key, val, state.qgenericFieldInfo[key].validations, state, {}); |
| 104 | genericFieldInfoClone[key] = {...genericFieldInfoClone[key], isValid: result.isValid, errorText: result.errorText}; |
| 105 | if (!result.isValid) { |
| 106 | formReady = false; |
| 107 | } |
| 108 | }); |
| 109 | return { |
| 110 | ...state, |
| 111 | formReady, |
| 112 | qgenericFieldInfo: genericFieldInfoClone |
| 113 | }; |
| 114 | |
| 115 | default: |
| 116 | return state; |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | export function createJSONSchemaReducer(questionnaireName) { |
| 121 | return (state = {}, action) => { |
| 122 | return updateSchemaDataAndValidateReducer(state, action, questionnaireName); |
| 123 | }; |
| 124 | }; |
| 125 | |
| 126 | export function createComposedJSONSchemaReducer(questionnaireName, additionalActionsReducer) { |
| 127 | return (state = {}, action) => { |
| 128 | if(action.type === actionTypes.VALIDATE_DATA || |
| 129 | action.type === actionTypes.VALIDATE_FORM || |
| 130 | action.type === actionTypes.DATA_CHANGED || |
| 131 | action.type === actionTypes.DATA_LOADED |
| 132 | ) { |
| 133 | return updateSchemaDataAndValidateReducer(state, action, questionnaireName); |
| 134 | } else { |
| 135 | return additionalActionsReducer(state, action); |
| 136 | } |
| 137 | }; |
| 138 | }; |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |