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 './PlainDataReducerConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 17 | import Validator from 'nfvo-utils/Validator.js'; |
| 18 | import forOwn from 'lodash/forOwn.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 19 | import { other as optionInputOther } from 'nfvo-components/input/validation/InputOptions.jsx'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 20 | |
| 21 | function updateDataAndValidateReducer(state = {}, action) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 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) { |
| 27 | return { ...state }; |
| 28 | } |
| 29 | genericFieldInfoCopy = { ...state.genericFieldInfo }; |
| 30 | forOwn(changed, (value, key) => { |
| 31 | if (state.genericFieldInfo[key]) { |
| 32 | let result = Validator.validate( |
| 33 | key, |
| 34 | value, |
| 35 | state.genericFieldInfo[key].validations, |
| 36 | state, |
| 37 | action.customValidations |
| 38 | ); |
| 39 | genericFieldInfoCopy[key] = { |
| 40 | ...genericFieldInfoCopy[key], |
| 41 | isValid: result.isValid, |
| 42 | errorText: result.errorText |
| 43 | }; |
| 44 | } |
| 45 | }); |
| 46 | return { |
| 47 | ...state, |
| 48 | formReady: null, |
| 49 | data: { |
| 50 | ...state.data, |
| 51 | ...action.deltaData |
| 52 | }, |
| 53 | genericFieldInfo: genericFieldInfoCopy |
| 54 | }; |
| 55 | case actionTypes.VALIDATE_FORM: |
| 56 | if (!action.formName || state.formName !== action.formName) { |
| 57 | return { ...state }; |
| 58 | } |
| 59 | genericFieldInfoCopy = { ...state.genericFieldInfo }; |
| 60 | let formReady = true; |
| 61 | forOwn(state.genericFieldInfo, (value, key) => { |
| 62 | let val = state.data && state.data[key] ? state.data[key] : ''; |
| 63 | let result = Validator.validate( |
| 64 | key, |
| 65 | val, |
| 66 | state.genericFieldInfo[key].validations, |
| 67 | state, |
| 68 | {} |
| 69 | ); |
| 70 | if (val.choice !== undefined) { |
| 71 | result = Validator.validate( |
| 72 | key, |
| 73 | val.choice, |
| 74 | state.genericFieldInfo[key].validations, |
| 75 | state, |
| 76 | {} |
| 77 | ); |
| 78 | } |
| 79 | if ( |
| 80 | val.choice !== undefined && |
| 81 | val.choice === optionInputOther.OTHER |
| 82 | ) { |
| 83 | result = Validator.validate( |
| 84 | key, |
| 85 | val.other, |
| 86 | state.genericFieldInfo[key].validations, |
| 87 | state, |
| 88 | {} |
| 89 | ); |
| 90 | } |
| 91 | genericFieldInfoCopy[key] = { |
| 92 | ...genericFieldInfoCopy[key], |
| 93 | isValid: result.isValid, |
| 94 | errorText: result.errorText |
| 95 | }; |
| 96 | if (!result.isValid) { |
| 97 | formReady = false; |
| 98 | } |
| 99 | }); |
| 100 | return { |
| 101 | ...state, |
| 102 | formReady, |
| 103 | genericFieldInfo: genericFieldInfoCopy |
| 104 | }; |
| 105 | case actionTypes.VALIDATE_DATA: |
| 106 | let specificFields = action.data; |
| 107 | if (!action.formName || state.formName !== action.formName) { |
| 108 | return { ...state }; |
| 109 | } |
| 110 | genericFieldInfoCopy = { ...state.genericFieldInfo }; |
| 111 | forOwn(specificFields, (value, key) => { |
| 112 | let result = Validator.validate( |
| 113 | key, |
| 114 | value, |
| 115 | state.genericFieldInfo[key].validations, |
| 116 | state, |
| 117 | action.customValidations |
| 118 | ); |
| 119 | genericFieldInfoCopy[key] = { |
| 120 | ...genericFieldInfoCopy[key], |
| 121 | isValid: result.isValid, |
| 122 | errorText: result.errorText |
| 123 | }; |
| 124 | }); |
| 125 | return { |
| 126 | ...state, |
| 127 | formReady: null, |
| 128 | genericFieldInfo: genericFieldInfoCopy |
| 129 | }; |
| 130 | default: |
| 131 | return state; |
| 132 | } |
| 133 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 134 | |
| 135 | export function createPlainDataReducer(loadReducer) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 136 | return (state = {}, action) => { |
| 137 | if ( |
| 138 | action.type === actionTypes.VALIDATE_DATA || |
| 139 | action.type === actionTypes.VALIDATE_FORM || |
| 140 | action.type === actionTypes.DATA_CHANGED |
| 141 | ) { |
| 142 | return updateDataAndValidateReducer(state, action); |
| 143 | } else { |
| 144 | return loadReducer(state, action); |
| 145 | } |
| 146 | }; |
| 147 | } |