Specify a model while creating a VSP
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / creation / SoftwareProductCreation.js
1 /*!
2  * Copyright © 2016-2018 European Support Limited
3  * Modifications Copyright (C) 2021 Nordix Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing
15  * permissions and limitations under the License.
16  */
17 import { connect } from 'react-redux';
18 import SoftwareProductCreationActionHelper from './SoftwareProductCreationActionHelper.js';
19 import SoftwareProductCreationView from './SoftwareProductCreationView.jsx';
20 import ValidationHelper from 'sdc-app/common/helpers/ValidationHelper.js';
21 import SoftwareProductActionHelper from '../SoftwareProductActionHelper.js';
22 import VersionsPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js';
23 import { itemTypes as versionItemTypes } from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js';
24 import ScreensHelper from 'sdc-app/common/helpers/ScreensHelper.js';
25 import { enums, screenTypes } from 'sdc-app/onboarding/OnboardingConstants.js';
26 import PermissionsActionHelper from 'sdc-app/onboarding/permissions/PermissionsActionHelper.js';
27 import UniqueTypesHelper from 'sdc-app/common/helpers/UniqueTypesHelper.js';
28 import i18n from 'nfvo-utils/i18n/i18n.js';
29 import { itemType } from 'sdc-app/common/helpers/ItemsHelperConstants.js';
30
31 export const mapStateToProps = ({
32     finalizedLicenseModelList,
33     users: { usersList },
34     archivedSoftwareProductList,
35     softwareProductList,
36     finalizedSoftwareProductList,
37     softwareProduct: {
38         softwareProductCreation,
39         softwareProductCategories,
40         modelList
41     }
42 }) => {
43     let { genericFieldInfo, vendorList = [] } = softwareProductCreation;
44     let isFormValid = ValidationHelper.checkFormValid(genericFieldInfo);
45
46     let VSPNames = {};
47     const allVspList = [
48         ...softwareProductList,
49         ...finalizedSoftwareProductList,
50         ...archivedSoftwareProductList
51     ];
52     allVspList.map(item => {
53         VSPNames[item.name.toLowerCase()] = item.id;
54     });
55
56     return {
57         data: softwareProductCreation.data,
58         selectedVendorId: softwareProductCreation.selectedVendorId,
59         disableVendor: softwareProductCreation.disableVendor,
60         softwareProductCategories,
61         finalizedLicenseModelList,
62         vendorList,
63         modelList: modelList,
64         isFormValid,
65         formReady: softwareProductCreation.formReady,
66         genericFieldInfo,
67         VSPNames,
68         usersList
69     };
70 };
71
72 export const mapActionsToProps = dispatch => {
73     return {
74         onDataChanged: (deltaData, formName, customValidations) => {
75             ValidationHelper.dataChanged(dispatch, {
76                 deltaData,
77                 formName,
78                 customValidations
79             });
80         },
81         onCancel: () => SoftwareProductCreationActionHelper.resetData(dispatch),
82         onSubmit: (softwareProduct, usersList) => {
83             SoftwareProductCreationActionHelper.resetData(dispatch);
84             SoftwareProductCreationActionHelper.createSoftwareProduct(
85                 dispatch,
86                 { softwareProduct }
87             ).then(response => {
88                 let { itemId, version } = response;
89                 SoftwareProductActionHelper.fetchSoftwareProductList(
90                     dispatch
91                 ).then(() =>
92                     PermissionsActionHelper.fetchItemUsers(dispatch, {
93                         itemId,
94                         allUsers: usersList
95                     }).then(() =>
96                         VersionsPageActionHelper.fetchVersions(dispatch, {
97                             itemType: versionItemTypes.SOFTWARE_PRODUCT,
98                             itemId
99                         }).then(() =>
100                             ScreensHelper.loadScreen(dispatch, {
101                                 screen:
102                                     enums.SCREEN.SOFTWARE_PRODUCT_LANDING_PAGE,
103                                 screenType: screenTypes.SOFTWARE_PRODUCT,
104                                 props: { softwareProductId: itemId, version }
105                             })
106                         )
107                     )
108                 );
109             });
110         },
111         onValidateForm: formName =>
112             ValidationHelper.validateForm(dispatch, formName),
113         isNameUnique: (value, name, formName) =>
114             UniqueTypesHelper.isNameUnique(dispatch, {
115                 value,
116                 name,
117                 formName,
118                 errorText: `${i18n(
119                     'Software product by the name'
120                 )} ${value} ${i18n('already exists')}. ${i18n(
121                     'Software product name must be unique'
122                 )}`,
123                 itemType: itemType.VSP
124             })
125     };
126 };
127
128 export default connect(mapStateToProps, mapActionsToProps, null, {
129     withRef: true
130 })(SoftwareProductCreationView);