[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / licenseAgreement / LicenseAgreementEditor.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 import LicenseAgreementActionHelper from './LicenseAgreementActionHelper.js';
18 import LicenseAgreementEditorView from './LicenseAgreementEditorView.jsx';
19 import ValidationHelper from 'sdc-app/common/helpers/ValidationHelper.js';
20
21 export const mapStateToProps = ({licenseModel: {licenseAgreement, featureGroup}}) => {
22
23
24         let {data, selectedTab, genericFieldInfo, formReady} = licenseAgreement.licenseAgreementEditor;
25         const list = licenseAgreement.licenseAgreementList;
26         const LANames = {};
27
28         let previousData;
29         const licenseAgreementId = data ? data.id : null;
30         if(licenseAgreementId) {
31                 previousData = licenseAgreement.licenseAgreementList.find(licenseAgreement => licenseAgreement.id === licenseAgreementId);
32         }
33
34         for (let i = 0; i < list.length; i++) {
35                 LANames[list[i].name] = list[i].id;
36         }
37
38         const {featureGroupsList = []} = featureGroup;
39
40         let isFormValid = true;
41         let invalidTabs = [];
42         for (let field in genericFieldInfo) {
43                 if (!genericFieldInfo[field].isValid) {
44                         isFormValid = false;
45                         let tabId = genericFieldInfo[field].tabId;
46                         if (invalidTabs.indexOf(tabId) === -1) {
47                                 invalidTabs[invalidTabs.length] = genericFieldInfo[field].tabId;
48                         }
49                 }
50         }
51
52         return {
53                 data,
54                 previousData,
55                 selectedTab,
56                 featureGroupsList,
57                 LANames,
58                 genericFieldInfo,
59                 isFormValid,
60                 formReady,
61                 invalidTabs
62         };
63 };
64
65 export const mapActionsToProps = (dispatch, {licenseModelId, version}) => {
66         return {
67                 onDataChanged: (deltaData, formName, customValidations) => ValidationHelper.dataChanged(dispatch, {deltaData, formName, customValidations}),
68                 onTabSelect: tab => LicenseAgreementActionHelper.selectLicenseAgreementEditorTab(dispatch, {tab}),
69                 onCancel: () => LicenseAgreementActionHelper.closeLicenseAgreementEditor(dispatch),
70                 onSubmit: ({previousLicenseAgreement, licenseAgreement}) => {
71                         LicenseAgreementActionHelper.closeLicenseAgreementEditor(dispatch);
72                         LicenseAgreementActionHelper.saveLicenseAgreement(dispatch, {licenseModelId, previousLicenseAgreement, licenseAgreement, version});
73                 },
74                 onValidateForm: (formName) => ValidationHelper.validateForm(dispatch, formName)
75         };
76 };
77
78 export default connect(mapStateToProps, mapActionsToProps)(LicenseAgreementEditorView);