[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / LicenseModel.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 i18n from 'nfvo-utils/i18n/i18n.js';
19 import VersionControllerUtils from 'nfvo-components/panel/versionController/VersionControllerUtils.js';
20 import TabulatedEditor from 'src/nfvo-components/editor/TabulatedEditor.jsx';
21 import ActivityLogActionHelper from 'sdc-app/common/activity-log/ActivityLogActionHelper.js';
22 import {enums} from 'sdc-app/onboarding/OnboardingConstants.js';
23 import OnboardingActionHelper from 'sdc-app/onboarding/OnboardingActionHelper.js';
24
25 import {navigationItems} from './LicenseModelConstants.js';
26 import LicenseModelActionHelper from './LicenseModelActionHelper.js';
27 import LicenseAgreementActionHelper from './licenseAgreement/LicenseAgreementActionHelper.js';
28 import FeatureGroupsActionHelper from './featureGroups/FeatureGroupsActionHelper.js';
29 import EntitlementPoolsActionHelper from './entitlementPools/EntitlementPoolsActionHelper.js';
30 import LicenseKeyGroupsActionHelper from './licenseKeyGroups/LicenseKeyGroupsActionHelper.js';
31
32
33 const buildNavigationBarProps = (licenseModel, screen) => {
34         const {id, vendorName, version} = licenseModel;
35         const meta = {version};
36
37         const groups = [{
38                 id,
39                 name: vendorName,
40                 items: [
41                         {
42                                 id: navigationItems.LICENSE_MODEL_OVERVIEW,
43                                 name: i18n('Overview'),
44                                 meta
45                         },
46                         {
47                                 id: navigationItems.LICENSE_AGREEMENTS,
48                                 name: i18n('License Agreements'),
49                                 meta
50                         },
51                         {
52                                 id: navigationItems.FEATURE_GROUPS,
53                                 name: i18n('Feature Groups'),
54                                 meta
55                         },
56                         {
57                                 id: navigationItems.ENTITLEMENT_POOLS,
58                                 name: i18n('Entitlement Pools'),
59                                 meta
60                         },
61                         {
62                                 id: navigationItems.LICENSE_KEY_GROUPS,
63                                 name: i18n('License Key Groups'),
64                                 meta
65                         },
66                         {
67                                 id: navigationItems.ACTIVITY_LOG,
68                                 name: i18n('Activity Log'),
69                                 meta
70                         }
71                 ]
72         }];
73
74         const activeItemId = ({
75                 [enums.SCREEN.LICENSE_MODEL_OVERVIEW]: navigationItems.LICENSE_MODEL_OVERVIEW,
76                 [enums.SCREEN.LICENSE_AGREEMENTS]: navigationItems.LICENSE_AGREEMENTS,
77                 [enums.SCREEN.FEATURE_GROUPS]: navigationItems.FEATURE_GROUPS,
78                 [enums.SCREEN.ENTITLEMENT_POOLS]: navigationItems.ENTITLEMENT_POOLS,
79                 [enums.SCREEN.LICENSE_KEY_GROUPS]: navigationItems.LICENSE_KEY_GROUPS,
80                 [enums.SCREEN.ACTIVITY_LOG]: navigationItems.ACTIVITY_LOG
81         })[screen];
82
83         return {
84                 activeItemId, groups
85         };
86 };
87
88
89 const buildVersionControllerProps = (licenseModel) => {
90         let {version, viewableVersions, status: currentStatus, lockingUser} = licenseModel;
91         let {status, isCheckedOut} = VersionControllerUtils.getCheckOutStatusKindByUserID(currentStatus, lockingUser);
92
93         return {
94                 version,
95                 viewableVersions,
96                 status,
97                 isCheckedOut
98         };
99 };
100
101
102 const mapStateToProps = ({licenseModel: {licenseModelEditor}}, {currentScreen: {screen}}) => {
103         return {
104                 versionControllerProps: buildVersionControllerProps(licenseModelEditor.data),
105                 navigationBarProps: buildNavigationBarProps(licenseModelEditor.data, screen)
106         };
107 };
108
109
110 const mapActionsToProps = (dispatch, {currentScreen: {screen, props: {licenseModelId}}}) => {
111
112         return {
113                 onVersionControllerAction: (action, version) =>
114                         LicenseModelActionHelper.performVCAction(dispatch, {licenseModelId, action, version}).then((newVersion) => {
115                                 switch(screen) {
116                                         case enums.SCREEN.LICENSE_MODEL_OVERVIEW:
117                                                 /**
118                                                  * TODO change to specific rest
119                                                  */
120                                                 LicenseAgreementActionHelper.fetchLicenseAgreementList(dispatch, {licenseModelId, version: newVersion});
121                                                 break;
122                                         case enums.SCREEN.LICENSE_AGREEMENTS:
123                                                 LicenseAgreementActionHelper.fetchLicenseAgreementList(dispatch, {licenseModelId, version: newVersion});
124                                                 break;
125                                         case enums.SCREEN.FEATURE_GROUPS:
126                                                 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {licenseModelId, version: newVersion});
127                                                 break;
128                                         case enums.SCREEN.ENTITLEMENT_POOLS:
129                                                 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {licenseModelId, version: newVersion});
130                                                 break;
131                                         case enums.SCREEN.LICENSE_KEY_GROUPS:
132                                                 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version: newVersion});
133                                                 break;
134                                         case enums.SCREEN.ACTIVITY_LOG:
135                                                 ActivityLogActionHelper.fetchActivityLog(dispatch, {itemId: licenseModelId, versionId: newVersion.id});
136                                                 break;
137                                 }
138                         }),
139                 onVersionSwitching: version => {
140                         LicenseModelActionHelper.switchVersion(dispatch, {licenseModelId, version});
141                         if(screen === enums.SCREEN.ACTIVITY_LOG) {
142                                 ActivityLogActionHelper.fetchActivityLog(dispatch, {itemId: licenseModelId, versionId: version.id});
143                         }
144                 },
145
146                 onNavigate: ({id, meta: {version}}) => {
147                         switch(id) {
148                                 case navigationItems.LICENSE_MODEL_OVERVIEW:
149                                         OnboardingActionHelper.navigateToLicenseModelOverview(dispatch, {licenseModelId, version});
150                                         break;
151                                 case navigationItems.LICENSE_AGREEMENTS:
152                                         OnboardingActionHelper.navigateToLicenseAgreements(dispatch, {licenseModelId, version});
153                                         break;
154                                 case navigationItems.FEATURE_GROUPS:
155                                         OnboardingActionHelper.navigateToFeatureGroups(dispatch, {licenseModelId, version});
156                                         break;
157                                 case navigationItems.ENTITLEMENT_POOLS:
158                                         OnboardingActionHelper.navigateToEntitlementPools(dispatch, {licenseModelId, version});
159                                         break;
160                                 case navigationItems.LICENSE_KEY_GROUPS:
161                                         OnboardingActionHelper.navigateToLicenseKeyGroups(dispatch, {licenseModelId, version});
162                                         break;
163                                 case navigationItems.ACTIVITY_LOG:
164                                         OnboardingActionHelper.navigateToLicenseModelActivityLog(dispatch, {licenseModelId, version});
165                                         break;
166                         }
167                 }
168         };
169 };
170
171 export default connect(mapStateToProps, mapActionsToProps)(TabulatedEditor);