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 as commonActionTypes } from 'sdc-app/common/reducers/PlainDataReducerConstants.js'; |
| 17 | import { actionTypes as qcommonActionTypes } from 'sdc-app/common/reducers/JSONSchemaReducerConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 18 | |
| 19 | class ValidationHelper { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 20 | static dataChanged( |
| 21 | dispatch, |
| 22 | { deltaData, formName, customValidations = {} } |
| 23 | ) { |
| 24 | dispatch({ |
| 25 | type: commonActionTypes.DATA_CHANGED, |
| 26 | deltaData, |
| 27 | formName, |
| 28 | customValidations |
| 29 | }); |
| 30 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 31 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 32 | static validateForm(dispatch, formName) { |
| 33 | dispatch({ |
| 34 | type: commonActionTypes.VALIDATE_FORM, |
| 35 | formName |
| 36 | }); |
| 37 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 38 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 39 | static validateData(dispatch, { formName, data }) { |
| 40 | dispatch({ |
| 41 | type: commonActionTypes.VALIDATE_DATA, |
| 42 | formName, |
| 43 | data |
| 44 | }); |
| 45 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 46 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 47 | static qValidateData(dispatch, { data, qName, customValidations = {} }) { |
| 48 | dispatch({ |
| 49 | type: qcommonActionTypes.VALIDATE_DATA, |
| 50 | data, |
| 51 | qName, |
| 52 | customValidations |
| 53 | }); |
| 54 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 55 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 56 | static qValidateForm(dispatch, qName, customValidations) { |
| 57 | dispatch({ |
| 58 | type: qcommonActionTypes.VALIDATE_FORM, |
| 59 | qName, |
| 60 | customValidations |
| 61 | }); |
| 62 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 63 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 64 | static qDataChanged( |
| 65 | dispatch, |
| 66 | { deltaData, qName, customValidations = {} } |
| 67 | ) { |
| 68 | dispatch({ |
| 69 | type: qcommonActionTypes.DATA_CHANGED, |
| 70 | deltaData, |
| 71 | qName, |
| 72 | customValidations |
| 73 | }); |
| 74 | } |
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 | static qDataLoaded(dispatch, { qName, response: { qdata, qschema } }) { |
| 77 | dispatch({ |
| 78 | type: qcommonActionTypes.DATA_LOADED, |
| 79 | payload: { |
| 80 | qdata, |
| 81 | qschema |
| 82 | }, |
| 83 | qName |
| 84 | }); |
| 85 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 86 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 87 | static checkFormValid(genericFieldInfo) { |
| 88 | for (let field in genericFieldInfo) { |
| 89 | if (!genericFieldInfo[field].isValid) { |
| 90 | return false; |
| 91 | } |
| 92 | } |
| 93 | return true; |
| 94 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | export default ValidationHelper; |