Creation of Vendor Licensing Model is an optional step in VSP onboarding
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / creation / SoftwareProductCreation.js
1 /*!
2  * Copyright © 2016-2018 European Support Limited
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 SoftwareProductCreationActionHelper from './SoftwareProductCreationActionHelper.js';
18 import SoftwareProductCreationView from './SoftwareProductCreationView.jsx';
19 import ValidationHelper from 'sdc-app/common/helpers/ValidationHelper.js';
20 import SoftwareProductActionHelper from '../SoftwareProductActionHelper.js';
21 import VersionsPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js';
22 import { itemTypes as versionItemTypes } from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js';
23 import ScreensHelper from 'sdc-app/common/helpers/ScreensHelper.js';
24 import { enums, screenTypes } from 'sdc-app/onboarding/OnboardingConstants.js';
25 import PermissionsActionHelper from 'sdc-app/onboarding/permissions/PermissionsActionHelper.js';
26 import UniqueTypesHelper from 'sdc-app/common/helpers/UniqueTypesHelper.js';
27 import i18n from 'nfvo-utils/i18n/i18n.js';
28 import { itemType } from 'sdc-app/common/helpers/ItemsHelperConstants.js';
29
30 export const mapStateToProps = ({
31     finalizedLicenseModelList,
32     users: { usersList },
33     archivedSoftwareProductList,
34     softwareProductList,
35     finalizedSoftwareProductList,
36     softwareProduct: { softwareProductCreation, softwareProductCategories }
37 }) => {
38     let { genericFieldInfo, vendorList = [] } = softwareProductCreation;
39     let isFormValid = ValidationHelper.checkFormValid(genericFieldInfo);
40
41     let VSPNames = {};
42     const allVspList = [
43         ...softwareProductList,
44         ...finalizedSoftwareProductList,
45         ...archivedSoftwareProductList
46     ];
47     allVspList.map(item => {
48         VSPNames[item.name.toLowerCase()] = item.id;
49     });
50
51     return {
52         data: softwareProductCreation.data,
53         selectedVendorId: softwareProductCreation.selectedVendorId,
54         disableVendor: softwareProductCreation.disableVendor,
55         softwareProductCategories,
56         finalizedLicenseModelList,
57         vendorList,
58         isFormValid,
59         formReady: softwareProductCreation.formReady,
60         genericFieldInfo,
61         VSPNames,
62         usersList
63     };
64 };
65
66 export const mapActionsToProps = dispatch => {
67     return {
68         onDataChanged: (deltaData, formName, customValidations) =>
69             ValidationHelper.dataChanged(dispatch, {
70                 deltaData,
71                 formName,
72                 customValidations
73             }),
74         onCancel: () => SoftwareProductCreationActionHelper.resetData(dispatch),
75         onSubmit: (softwareProduct, usersList) => {
76             SoftwareProductCreationActionHelper.resetData(dispatch);
77             SoftwareProductCreationActionHelper.createSoftwareProduct(
78                 dispatch,
79                 { softwareProduct }
80             ).then(response => {
81                 let { itemId, version } = response;
82                 SoftwareProductActionHelper.fetchSoftwareProductList(
83                     dispatch
84                 ).then(() =>
85                     PermissionsActionHelper.fetchItemUsers(dispatch, {
86                         itemId,
87                         allUsers: usersList
88                     }).then(() =>
89                         VersionsPageActionHelper.fetchVersions(dispatch, {
90                             itemType: versionItemTypes.SOFTWARE_PRODUCT,
91                             itemId
92                         }).then(() =>
93                             ScreensHelper.loadScreen(dispatch, {
94                                 screen:
95                                     enums.SCREEN.SOFTWARE_PRODUCT_LANDING_PAGE,
96                                 screenType: screenTypes.SOFTWARE_PRODUCT,
97                                 props: { softwareProductId: itemId, version }
98                             })
99                         )
100                     )
101                 );
102             });
103         },
104         onValidateForm: formName =>
105             ValidationHelper.validateForm(dispatch, formName),
106         isNameUnique: (value, name, formName) =>
107             UniqueTypesHelper.isNameUnique(dispatch, {
108                 value,
109                 name,
110                 formName,
111                 errorText: `${i18n(
112                     'Software product by the name'
113                 )} ${value} ${i18n('already exists')}. ${i18n(
114                     'Software product name must be unique'
115                 )}`,
116                 itemType: itemType.VSP
117             })
118     };
119 };
120
121 export default connect(mapStateToProps, mapActionsToProps, null, {
122     withRef: true
123 })(SoftwareProductCreationView);