Obtain upload lock before uploading
[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 import VspUploadStatus from 'sdc-app/onboarding/softwareProduct/landingPage/VspUploadStatus';
26
27 export const mapStateToProps = ({
28     features,
29     softwareProduct,
30     licenseModel: { licenseAgreement },
31     currentScreen: { itemPermission: { isCertified } }
32 }) => {
33     let {
34         softwareProductEditor: { data: currentSoftwareProduct = {} },
35         softwareProductComponents,
36         softwareProductCategories = []
37     } = softwareProduct;
38     let { licensingData = {} } = currentSoftwareProduct;
39     let { licenseAgreementList } = licenseAgreement;
40     let { componentsList } = softwareProductComponents;
41     let licenseAgreementName = licenseAgreementList.find(
42         la => la.id === licensingData.licenseAgreement
43     );
44     if (licenseAgreementName) {
45         licenseAgreementName = licenseAgreementName.name;
46     } else if (licenseAgreementList.length === 0) {
47         // otherwise the state of traingle svgicon will be updated post unmounting
48         licenseAgreementName = null;
49     }
50
51     let categoryName = '',
52         subCategoryName = '',
53         fullCategoryDisplayName = '';
54     const category = softwareProductCategories.find(
55         ca => ca.uniqueId === currentSoftwareProduct.category
56     );
57     if (category) {
58         categoryName = category.name;
59         const subcategories = category.subcategories || [];
60         const subcat = subcategories.find(
61             sc => sc.uniqueId === currentSoftwareProduct.subCategory
62         );
63         subCategoryName = subcat && subcat.name ? subcat.name : '';
64     }
65     fullCategoryDisplayName = `${subCategoryName} (${categoryName})`;
66
67     return {
68         features,
69         currentSoftwareProduct: {
70             ...currentSoftwareProduct,
71             licenseAgreementName,
72             fullCategoryDisplayName
73         },
74         isCertified,
75         componentsList,
76         isManual:
77             currentSoftwareProduct.onboardingMethod === onboardingMethod.MANUAL
78     };
79 };
80
81 function handleScreenChange(softwareProduct, dispatch, version) {
82     const softwareProductId = softwareProduct.id;
83     if (softwareProduct.licenseType === 'INTERNAL') {
84         ScreensHelper.loadScreen(dispatch, {
85             screen: enums.SCREEN.SOFTWARE_PRODUCT_DETAILS,
86             screenType: screenTypes.SOFTWARE_PRODUCT,
87             props: { softwareProductId, version }
88         });
89     } else {
90         ScreensHelper.loadScreen(dispatch, {
91             screen: enums.SCREEN.SOFTWARE_PRODUCT_LANDING_PAGE,
92             screenType: screenTypes.SOFTWARE_PRODUCT,
93             props: { softwareProductId, version }
94         });
95     }
96 }
97
98 const mapActionsToProps = (dispatch, { version }) => {
99     return {
100         onLicenseChange: softwareProduct => {
101             SoftwareProductActionHelper.updateSoftwareProductData(dispatch, {
102                 softwareProduct,
103                 version
104             }).then(() => {});
105             handleScreenChange(softwareProduct, dispatch, version);
106         },
107         onCandidateInProcess: softwareProductId =>
108             ScreensHelper.loadScreen(dispatch, {
109                 screen: enums.SCREEN.SOFTWARE_PRODUCT_ATTACHMENTS_SETUP,
110                 screenType: screenTypes.SOFTWARE_PRODUCT,
111                 props: { softwareProductId, version }
112             }),
113         onUpload: (
114             softwareProductId,
115             formData,
116             onUploadStart = () => {
117                 // do nothing by default
118             },
119             onUploadProgress = undefined,
120             onUploadFinished = () => {
121                 // do nothing by default
122             }
123         ) => {
124             SoftwareProductActionHelper.createUploadStatus(
125                 softwareProductId,
126                 version.id
127             )
128                 .then(response => {
129                     const vspUploadStatus = new VspUploadStatus(response);
130                     onUploadStart(vspUploadStatus);
131                     SoftwareProductActionHelper.uploadFile(dispatch, {
132                         softwareProductId,
133                         formData,
134                         failedNotificationTitle: i18n(
135                             'Upload validation failed'
136                         ),
137                         version,
138                         onUploadProgress
139                     }).finally(() => {
140                         onUploadFinished();
141                     });
142                 })
143                 .catch(error => {
144                     dispatch({
145                         type: modalActionTypes.GLOBAL_MODAL_ERROR,
146                         data: {
147                             title: i18n('upload.failed'),
148                             msg: error.message
149                         }
150                     });
151                 });
152         },
153
154         onUploadConfirmation: (
155             softwareProductId,
156             formData,
157             onUploadStart = () => {
158                 // do nothing by default
159             },
160             onUploadProgress = undefined,
161             onUploadFinished = () => {
162                 // do nothing by default
163             }
164         ) =>
165             dispatch({
166                 type: modalActionTypes.GLOBAL_MODAL_WARNING,
167                 data: {
168                     msg: i18n(
169                         'Upload will erase existing data. Do you want to continue?'
170                     ),
171                     confirmationButtonText: i18n('Continue'),
172                     title: i18n('Warning'),
173                     onConfirmed: () => {
174                         SoftwareProductActionHelper.createUploadStatus(
175                             softwareProductId,
176                             version.id
177                         )
178                             .then(response => {
179                                 const vspUploadStatus = new VspUploadStatus(
180                                     response
181                                 );
182                                 onUploadStart(vspUploadStatus);
183                                 SoftwareProductActionHelper.uploadFile(
184                                     dispatch,
185                                     {
186                                         softwareProductId,
187                                         formData,
188                                         failedNotificationTitle: i18n(
189                                             'Upload validation failed'
190                                         ),
191                                         version,
192                                         onUploadProgress
193                                     }
194                                 ).finally(() => {
195                                     onUploadFinished();
196                                 });
197                             })
198                             .catch(error => {
199                                 dispatch({
200                                     type: modalActionTypes.GLOBAL_MODAL_ERROR,
201                                     data: {
202                                         title: i18n('upload.failed'),
203                                         msg: error.message
204                                     }
205                                 });
206                             });
207                     },
208                     onDeclined: () =>
209                         dispatch({
210                             type: modalActionTypes.GLOBAL_MODAL_CLOSE
211                         })
212                 }
213             }),
214
215         onInvalidFileSizeUpload: () =>
216             dispatch({
217                 type: modalActionTypes.GLOBAL_MODAL_ERROR,
218                 data: {
219                     title: i18n('Upload Failed'),
220                     confirmationButtonText: i18n('Continue'),
221                     msg: i18n(
222                         "no zip or csar file was uploaded or expected file doesn't exist"
223                     )
224                 }
225             }),
226
227         fetchUploadStatus: softwareProductId => {
228             return SoftwareProductActionHelper.fetchUploadStatus(
229                 softwareProductId,
230                 version.id
231             );
232         },
233
234         onComponentSelect: ({ id: softwareProductId, componentId }) =>
235             ScreensHelper.loadScreen(dispatch, {
236                 screen: screenTypes.SOFTWARE_PRODUCT_COMPONENT_DEFAULT_GENERAL,
237                 screenType: screenTypes.SOFTWARE_PRODUCT,
238                 props: { softwareProductId, version, componentId }
239             }),
240         /** for the next version */
241         onAddComponent: () =>
242             SoftwareProductActionHelper.addComponent(dispatch),
243
244         onBrowseVNF: currentSoftwareProduct => {
245             VNFImportActionHelper.open(dispatch, currentSoftwareProduct);
246         }
247     };
248 };
249
250 export default connect(mapStateToProps, mapActionsToProps, null, {
251     withRef: true
252 })(LandingPageView);