Obtain and control VSP package upload status
[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: (
113             softwareProductId,
114             formData,
115             onUploadStart = () => {
116                 // do nothing by default
117             },
118             onUploadProgress = undefined,
119             onUploadFinished = () => {
120                 // do nothing by default
121             }
122         ) => {
123             SoftwareProductActionHelper.uploadFile(dispatch, {
124                 softwareProductId,
125                 formData,
126                 failedNotificationTitle: i18n('Upload validation failed'),
127                 version,
128                 onUploadProgress
129             }).finally(() => {
130                 onUploadFinished();
131             });
132             onUploadStart();
133         },
134
135         onUploadConfirmation: (
136             softwareProductId,
137             formData,
138             onUploadStart = () => {
139                 // do nothing by default
140             },
141             onUploadProgress = undefined,
142             onUploadFinished = () => {
143                 // do nothing by default
144             }
145         ) =>
146             dispatch({
147                 type: modalActionTypes.GLOBAL_MODAL_WARNING,
148                 data: {
149                     msg: i18n(
150                         'Upload will erase existing data. Do you want to continue?'
151                     ),
152                     confirmationButtonText: i18n('Continue'),
153                     title: i18n('Warning'),
154                     onConfirmed: () => {
155                         SoftwareProductActionHelper.uploadFile(dispatch, {
156                             softwareProductId,
157                             formData,
158                             failedNotificationTitle: i18n(
159                                 'Upload validation failed'
160                             ),
161                             version,
162                             onUploadProgress
163                         }).finally(value => {
164                             console.log('upload finished', value);
165                             onUploadFinished();
166                         });
167                         onUploadStart();
168                     },
169                     onDeclined: () =>
170                         dispatch({
171                             type: modalActionTypes.GLOBAL_MODAL_CLOSE
172                         })
173                 }
174             }),
175
176         onInvalidFileSizeUpload: () =>
177             dispatch({
178                 type: modalActionTypes.GLOBAL_MODAL_ERROR,
179                 data: {
180                     title: i18n('Upload Failed'),
181                     confirmationButtonText: i18n('Continue'),
182                     msg: i18n(
183                         "no zip or csar file was uploaded or expected file doesn't exist"
184                     )
185                 }
186             }),
187
188         fetchUploadStatus: softwareProductId => {
189             return SoftwareProductActionHelper.fetchUploadStatus(
190                 softwareProductId,
191                 version.id
192             );
193         },
194
195         onComponentSelect: ({ id: softwareProductId, componentId }) =>
196             ScreensHelper.loadScreen(dispatch, {
197                 screen: screenTypes.SOFTWARE_PRODUCT_COMPONENT_DEFAULT_GENERAL,
198                 screenType: screenTypes.SOFTWARE_PRODUCT,
199                 props: { softwareProductId, version, componentId }
200             }),
201         /** for the next version */
202         onAddComponent: () =>
203             SoftwareProductActionHelper.addComponent(dispatch),
204
205         onBrowseVNF: currentSoftwareProduct => {
206             VNFImportActionHelper.open(dispatch, currentSoftwareProduct);
207         }
208     };
209 };
210
211 export default connect(mapStateToProps, mapActionsToProps, null, {
212     withRef: true
213 })(LandingPageView);