[SDC-29] Amdocs OnBoard 1707 initial commit.
[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 = ({licenseModel: {featureGroup, entitlementPool, licenseKeyGroup}}) => {
23         let {entitlementPoolsList = []} = entitlementPool;
24         let {licenseKeyGroupsList = []} = licenseKeyGroup;
25         const {featureGroupEditor} = featureGroup;
26         let {data, selectedTab, genericFieldInfo, formReady} = featureGroupEditor;
27         const featureGroupId = data ? data.id : null;
28         const list = featureGroup.featureGroupsList;
29
30         let previousData, FGNames = {}, isFormValid = true, invalidTabs = [];
31
32         if (featureGroupId) {
33                 previousData = list.find(featureGroup => featureGroup.id === featureGroupId);
34         }
35
36         for (let i = 0; i < list.length; i++) {
37                 FGNames[list[i].name] = list[i].id;
38         }
39
40         for (let field in genericFieldInfo) {
41                 if (!genericFieldInfo[field].isValid) {
42                         isFormValid = false;
43                         let tabId = genericFieldInfo[field].tabId;
44                         if (invalidTabs.indexOf(tabId) === -1) {
45                                 invalidTabs[invalidTabs.length] = genericFieldInfo[field].tabId;
46                         }
47                 }
48         }
49
50         return {
51                 data,
52                 previousData,
53                 selectedTab,
54                 entitlementPoolsList,
55                 licenseKeyGroupsList,
56                 isFormValid,
57                 formReady,
58                 genericFieldInfo,
59                 invalidTabs,
60                 FGNames
61         };
62 };
63
64
65 const mapActionsToProps = (dispatch, {licenseModelId, version}) => {
66         return {
67                 onDataChanged: (deltaData, formName, customValidations) => ValidationHelper.dataChanged(dispatch, {deltaData, formName, customValidations}),
68                 onTabSelect: tab => FeatureGroupsActionHelper.selectEntitlementPoolsEditorTab(dispatch, {tab}),
69                 onSubmit: (previousFeatureGroup, featureGroup) => {
70                         FeatureGroupsActionHelper.closeFeatureGroupsEditor(dispatch);
71                         FeatureGroupsActionHelper.saveFeatureGroup(dispatch, {licenseModelId, previousFeatureGroup, featureGroup, version});
72                 },
73                 onCancel: () => FeatureGroupsActionHelper.closeFeatureGroupsEditor(dispatch),
74                 onValidateForm: (formName) => ValidationHelper.validateForm(dispatch, formName)
75         };
76 };
77
78 export default connect(mapStateToProps, mapActionsToProps)(FeatureGroupEditorView);