fa136f2f1bfa981b91406d58decc4e1c80a8f4b4
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / landingPage / SoftwareProductLandingPage.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 i18n from 'nfvo-utils/i18n/i18n.js';
18 import SoftwareProductActionHelper from 'sdc-app/onboarding/softwareProduct/SoftwareProductActionHelper.js';
19 import LandingPageView from './SoftwareProductLandingPageView.jsx';
20 import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
21 import { onboardingMethod } from '../SoftwareProductConstants.js';
22 import ScreensHelper from 'sdc-app/common/helpers/ScreensHelper.js';
23 import { enums, screenTypes } from 'sdc-app/onboarding/OnboardingConstants.js';
24 import VNFImportActionHelper from '../vnfMarketPlace/VNFImportActionHelper.js';
25
26 export const mapStateToProps = ({
27     features,
28     softwareProduct,
29     licenseModel: { licenseAgreement },
30     currentScreen: { itemPermission: { isCertified } }
31 }) => {
32     let {
33         softwareProductEditor: { data: currentSoftwareProduct = {} },
34         softwareProductComponents,
35         softwareProductCategories = []
36     } = softwareProduct;
37     let { licensingData = {} } = currentSoftwareProduct;
38     let { licenseAgreementList } = licenseAgreement;
39     let { componentsList } = softwareProductComponents;
40     let licenseAgreementName = licenseAgreementList.find(
41         la => la.id === licensingData.licenseAgreement
42     );
43     if (licenseAgreementName) {
44         licenseAgreementName = licenseAgreementName.name;
45     } else if (licenseAgreementList.length === 0) {
46         // otherwise the state of traingle svgicon will be updated post unmounting
47         licenseAgreementName = null;
48     }
49
50     let categoryName = '',
51         subCategoryName = '',
52         fullCategoryDisplayName = '';
53     const category = softwareProductCategories.find(
54         ca => ca.uniqueId === currentSoftwareProduct.category
55     );
56     if (category) {
57         categoryName = category.name;
58         const subcategories = category.subcategories || [];
59         const subcat = subcategories.find(
60             sc => sc.uniqueId === currentSoftwareProduct.subCategory
61         );
62         subCategoryName = subcat && subcat.name ? subcat.name : '';
63     }
64     fullCategoryDisplayName = `${subCategoryName} (${categoryName})`;
65
66     return {
67         features,
68         currentSoftwareProduct: {
69             ...currentSoftwareProduct,
70             licenseAgreementName,
71             fullCategoryDisplayName
72         },
73         isCertified,
74         componentsList,
75         isManual:
76             currentSoftwareProduct.onboardingMethod === onboardingMethod.MANUAL
77     };
78 };
79
80 function handleScreenChange(softwareProduct, dispatch, version) {
81     const softwareProductId = softwareProduct.id;
82     if (softwareProduct.licenseType === 'INTERNAL') {
83         ScreensHelper.loadScreen(dispatch, {
84             screen: enums.SCREEN.SOFTWARE_PRODUCT_DETAILS,
85             screenType: screenTypes.SOFTWARE_PRODUCT,
86             props: { softwareProductId, version }
87         });
88     } else {
89         ScreensHelper.loadScreen(dispatch, {
90             screen: enums.SCREEN.SOFTWARE_PRODUCT_LANDING_PAGE,
91             screenType: screenTypes.SOFTWARE_PRODUCT,
92             props: { softwareProductId, version }
93         });
94     }
95 }
96
97 const mapActionsToProps = (dispatch, { version }) => {
98     return {
99         onLicenseChange: softwareProduct => {
100             SoftwareProductActionHelper.updateSoftwareProductData(dispatch, {
101                 softwareProduct,
102                 version
103             }).then(() => {});
104             handleScreenChange(softwareProduct, dispatch, version);
105         },
106         onCandidateInProcess: softwareProductId =>
107             ScreensHelper.loadScreen(dispatch, {
108                 screen: enums.SCREEN.SOFTWARE_PRODUCT_ATTACHMENTS_SETUP,
109                 screenType: screenTypes.SOFTWARE_PRODUCT,
110                 props: { softwareProductId, version }
111             }),
112         onUpload: (softwareProductId, formData) =>
113             SoftwareProductActionHelper.uploadFile(dispatch, {
114                 softwareProductId,
115                 formData,
116                 failedNotificationTitle: i18n('Upload validation failed'),
117                 version
118             }),
119
120         onUploadConfirmation: (softwareProductId, formData) =>
121             dispatch({
122                 type: modalActionTypes.GLOBAL_MODAL_WARNING,
123                 data: {
124                     msg: i18n(
125                         'Upload will erase existing data. Do you want to continue?'
126                     ),
127                     confirmationButtonText: i18n('Continue'),
128                     title: i18n('Warning'),
129                     onConfirmed: () =>
130                         SoftwareProductActionHelper.uploadFile(dispatch, {
131                             softwareProductId,
132                             formData,
133                             failedNotificationTitle: i18n(
134                                 'Upload validation failed'
135                             ),
136                             version
137                         }),
138                     onDeclined: () =>
139                         dispatch({
140                             type: modalActionTypes.GLOBAL_MODAL_CLOSE
141                         })
142                 }
143             }),
144
145         onInvalidFileSizeUpload: () =>
146             dispatch({
147                 type: modalActionTypes.GLOBAL_MODAL_ERROR,
148                 data: {
149                     title: i18n('Upload Failed'),
150                     confirmationButtonText: i18n('Continue'),
151                     msg: i18n(
152                         "no zip or csar file was uploaded or expected file doesn't exist"
153                     )
154                 }
155             }),
156         onComponentSelect: ({ id: softwareProductId, componentId }) =>
157             ScreensHelper.loadScreen(dispatch, {
158                 screen: screenTypes.SOFTWARE_PRODUCT_COMPONENT_DEFAULT_GENERAL,
159                 screenType: screenTypes.SOFTWARE_PRODUCT,
160                 props: { softwareProductId, version, componentId }
161             }),
162         /** for the next version */
163         onAddComponent: () =>
164             SoftwareProductActionHelper.addComponent(dispatch),
165
166         onBrowseVNF: currentSoftwareProduct => {
167             VNFImportActionHelper.open(dispatch, currentSoftwareProduct);
168         }
169     };
170 };
171
172 export default connect(mapStateToProps, mapActionsToProps, null, {
173     withRef: true
174 })(LandingPageView);