Adding Prettier and fixing up eslint version
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / featureGroups / FeatureGroupEditor.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 { connect } from 'react-redux';
17
18 import FeatureGroupsActionHelper from './FeatureGroupsActionHelper.js';
19 import FeatureGroupEditorView from './FeatureGroupEditorView.jsx';
20 import ValidationHelper from 'sdc-app/common/helpers/ValidationHelper.js';
21
22 export const mapStateToProps = ({
23     licenseModel: { featureGroup, entitlementPool, licenseKeyGroup }
24 }) => {
25     let { entitlementPoolsList = [] } = entitlementPool;
26     let { licenseKeyGroupsList = [] } = licenseKeyGroup;
27     const { featureGroupEditor } = featureGroup;
28     let { data, selectedTab, genericFieldInfo, formReady } = featureGroupEditor;
29     const featureGroupId = data ? data.id : null;
30     const list = featureGroup.featureGroupsList;
31
32     let previousData,
33         FGNames = {},
34         isFormValid = true,
35         invalidTabs = [];
36
37     if (featureGroupId) {
38         previousData = list.find(
39             featureGroup => featureGroup.id === featureGroupId
40         );
41     }
42
43     for (let i = 0; i < list.length; i++) {
44         FGNames[list[i].name.toLowerCase()] = list[i].id;
45     }
46
47     for (let field in genericFieldInfo) {
48         if (!genericFieldInfo[field].isValid) {
49             isFormValid = false;
50             let tabId = genericFieldInfo[field].tabId;
51             if (invalidTabs.indexOf(tabId) === -1) {
52                 invalidTabs[invalidTabs.length] = genericFieldInfo[field].tabId;
53             }
54         }
55     }
56
57     return {
58         data,
59         previousData,
60         selectedTab,
61         entitlementPoolsList,
62         licenseKeyGroupsList,
63         isFormValid,
64         formReady,
65         genericFieldInfo,
66         invalidTabs,
67         FGNames
68     };
69 };
70
71 const mapActionsToProps = (dispatch, { licenseModelId, version }) => {
72     return {
73         onDataChanged: (deltaData, formName, customValidations) =>
74             ValidationHelper.dataChanged(dispatch, {
75                 deltaData,
76                 formName,
77                 customValidations
78             }),
79         onTabSelect: tab =>
80             FeatureGroupsActionHelper.selectEntitlementPoolsEditorTab(
81                 dispatch,
82                 {
83                     tab
84                 }
85             ),
86         onSubmit: (previousFeatureGroup, featureGroup) => {
87             FeatureGroupsActionHelper.closeFeatureGroupsEditor(dispatch);
88             FeatureGroupsActionHelper.saveFeatureGroup(dispatch, {
89                 licenseModelId,
90                 previousFeatureGroup,
91                 featureGroup,
92                 version
93             });
94         },
95         onCancel: () =>
96             FeatureGroupsActionHelper.closeFeatureGroupsEditor(dispatch),
97         onValidateForm: formName =>
98             ValidationHelper.validateForm(dispatch, formName)
99     };
100 };
101
102 export default connect(mapStateToProps, mapActionsToProps)(
103     FeatureGroupEditorView
104 );