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 | */ |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 16 | import { actionTypes } from './JSONSchemaReducerConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 23 | function flattenData(data, result, pointer = '') { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 24 | let newPointer = pointer; |
| 25 | if (typeof data === 'object' && !isArray(data)) { |
| 26 | for (let i in data) { |
| 27 | newPointer = newPointer ? newPointer + '/' + i : i; |
| 28 | flattenData(data[i], result, newPointer); |
| 29 | newPointer = pointer; |
| 30 | } |
| 31 | } else { |
| 32 | result[newPointer] = data; |
| 33 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 34 | } |
| 35 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 36 | function updateSchemaDataAndValidateReducer( |
| 37 | state = {}, |
| 38 | action, |
| 39 | questionnaireName |
| 40 | ) { |
| 41 | let genericFieldInfoClone; |
| 42 | switch (action.type) { |
| 43 | case actionTypes.DATA_LOADED: |
| 44 | if (questionnaireName !== action.qName) { |
| 45 | return { ...state }; |
| 46 | } |
| 47 | const schema = action.payload.qschema; |
| 48 | let schemaLoader = new JSONSchema(); |
| 49 | schemaLoader.setSchema(schema); |
| 50 | schemaLoader.setSupportedValidationFunctions( |
| 51 | Object.keys(Validator.globalValidationFunctions) |
| 52 | ); |
| 53 | let { genericFieldInfo } = schemaLoader.flattenSchema(); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 54 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 55 | let data = action.payload.qdata; |
| 56 | let dataMap = {}; |
| 57 | flattenData(data, dataMap); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 58 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 59 | return { |
| 60 | ...state, |
| 61 | qdata: action.payload.qdata, // the original hierarchical data. to be used for submit and save |
| 62 | qgenericFieldInfo: genericFieldInfo, // information about the fields that the view will require and reducer will need, such as validations, enum to use, etc. |
| 63 | dataMap // flattened schema data for ease of use |
| 64 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 65 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 66 | case actionTypes.DATA_CHANGED: |
| 67 | let changedData = action.deltaData; |
| 68 | if (questionnaireName !== action.qName) { |
| 69 | return { ...state }; |
| 70 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 71 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 72 | genericFieldInfoClone = { ...state.qgenericFieldInfo }; |
| 73 | let qDataClone = { ...state.qdata }; |
| 74 | let dataMapClone = { ...state.dataMap }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 75 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 76 | forOwn(changedData, (value, key) => { |
| 77 | if (state.qgenericFieldInfo[key]) { |
| 78 | let result = Validator.validate( |
| 79 | key, |
| 80 | value, |
| 81 | state.qgenericFieldInfo[key].validations, |
| 82 | state, |
| 83 | action.customValidations |
| 84 | ); |
| 85 | genericFieldInfoClone[key] = { |
| 86 | ...genericFieldInfoClone[key], |
| 87 | isValid: result.isValid, |
| 88 | errorText: result.errorText |
| 89 | }; |
| 90 | qDataClone = JSONPointer.setValue( |
| 91 | state.qdata, |
| 92 | '/' + key, |
| 93 | value |
| 94 | ); |
| 95 | dataMapClone[key] = value; |
| 96 | } |
| 97 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 98 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 99 | return { |
| 100 | ...state, |
| 101 | qdata: qDataClone, |
| 102 | dataMap: dataMapClone, |
| 103 | qgenericFieldInfo: genericFieldInfoClone |
| 104 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 105 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 106 | case actionTypes.VALIDATE_DATA: |
| 107 | let specificFields = action.data; |
| 108 | if (questionnaireName !== action.qName) { |
| 109 | return { ...state }; |
| 110 | } |
| 111 | genericFieldInfoClone = { ...state.qgenericFieldInfo }; |
| 112 | forOwn(specificFields, (value, key) => { |
| 113 | let result = Validator.validate( |
| 114 | key, |
| 115 | value, |
| 116 | state.qgenericFieldInfo[key].validations, |
| 117 | state, |
| 118 | action.customValidations |
| 119 | ); |
| 120 | genericFieldInfoClone[key] = { |
| 121 | ...genericFieldInfoClone[key], |
| 122 | isValid: result.isValid, |
| 123 | errorText: result.errorText |
| 124 | }; |
| 125 | }); |
| 126 | return { |
| 127 | ...state, |
| 128 | formReady: null, |
| 129 | qgenericFieldInfo: genericFieldInfoClone |
| 130 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 131 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 132 | case actionTypes.VALIDATE_FORM: |
| 133 | if (questionnaireName !== action.qName) { |
| 134 | return { ...state }; |
| 135 | } |
| 136 | genericFieldInfoClone = { ...state.qgenericFieldInfo }; |
| 137 | let formReady = true; |
| 138 | forOwn(state.qgenericFieldInfo, (value, key) => { |
| 139 | let val = state.dataMap[key] ? state.dataMap[key] : ''; |
| 140 | let result = Validator.validate( |
| 141 | key, |
| 142 | val, |
| 143 | state.qgenericFieldInfo[key].validations, |
| 144 | state, |
| 145 | action.customValidations |
| 146 | ); |
| 147 | genericFieldInfoClone[key] = { |
| 148 | ...genericFieldInfoClone[key], |
| 149 | isValid: result.isValid, |
| 150 | errorText: result.errorText |
| 151 | }; |
| 152 | if (!result.isValid) { |
| 153 | formReady = false; |
| 154 | } |
| 155 | }); |
| 156 | return { |
| 157 | ...state, |
| 158 | formReady, |
| 159 | qgenericFieldInfo: genericFieldInfoClone |
| 160 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 161 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 162 | default: |
| 163 | return state; |
| 164 | } |
| 165 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 166 | |
| 167 | export function createJSONSchemaReducer(questionnaireName) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 168 | return (state = {}, action) => { |
| 169 | return updateSchemaDataAndValidateReducer( |
| 170 | state, |
| 171 | action, |
| 172 | questionnaireName |
| 173 | ); |
| 174 | }; |
| 175 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 176 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 177 | export function createComposedJSONSchemaReducer( |
| 178 | questionnaireName, |
| 179 | additionalActionsReducer |
| 180 | ) { |
| 181 | return (state = {}, action) => { |
| 182 | if ( |
| 183 | action.type === actionTypes.VALIDATE_DATA || |
| 184 | action.type === actionTypes.VALIDATE_FORM || |
| 185 | action.type === actionTypes.DATA_CHANGED || |
| 186 | action.type === actionTypes.DATA_LOADED |
| 187 | ) { |
| 188 | return updateSchemaDataAndValidateReducer( |
| 189 | state, |
| 190 | action, |
| 191 | questionnaireName |
| 192 | ); |
| 193 | } else { |
| 194 | return additionalActionsReducer(state, action); |
| 195 | } |
| 196 | }; |
| 197 | } |