[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / monitoring / SoftwareProductComponentsMonitoringActionHelper.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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 i18n from 'nfvo-utils/i18n/i18n.js';
17 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
18 import Configuration from 'sdc-app/config/Configuration.js';
19 import {actionTypes} from './SoftwareProductComponentsMonitoringConstants.js';
20 import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
21
22 function baseUrl(vspId, version, componentId) {
23         const versionId = version.id;
24         const restPrefix = Configuration.get('restPrefix');
25         return `${restPrefix}/v1.0/vendor-software-products/${vspId}/versions/${versionId}/components/${componentId}/uploads`;
26 }
27
28 let onInvalidFileSizeUpload = (dispatch) => dispatch({
29         type: modalActionTypes.GLOBAL_MODAL_ERROR,
30         data: {
31                 title: i18n('Upload Failed'),
32                 msg: i18n('no zip file was uploaded or zip file doesn\'t exist')
33         }
34 });
35
36 let uploadFile = (dispatch, {softwareProductId, version, componentId, formData, type}) => {
37         return RestAPIUtil.post(`${baseUrl(softwareProductId, version, componentId)}/types/${type}`, formData).then(()=> dispatch({
38                 type: actionTypes.MONITOR_UPLOADED, data: {filename: formData.get('upload').name, type : type}
39         }));
40 };
41
42 let deleteFile = (dispatch, {softwareProductId, version, componentId, type}) => {
43         return RestAPIUtil.destroy(`${baseUrl(softwareProductId, version, componentId)}/types/${type}`).then(()=> dispatch({
44                 type: actionTypes.MONITOR_DELETED,
45                 data : { type: type}
46         }));
47 };
48
49
50 const SoftwareProductComponentsMonitoringAction = {
51
52         fetchExistingFiles(dispatch, {softwareProductId, version, componentId}){
53                 return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version, componentId)}`).then(response =>
54                         dispatch({
55                                 type: actionTypes.MONITOR_FILES_DATA_CHANGE,
56                                 data: response
57                         })
58                 );
59         },
60
61         uploadFile(dispatch, {softwareProductId, version, componentId, formData, type}){
62                 if (formData.get('upload').size) {
63                         return uploadFile(dispatch, {softwareProductId, version, componentId, formData, type});
64                 }
65                 else {
66                         onInvalidFileSizeUpload(dispatch);
67                 }
68         },
69
70         deleteFile(dispatch, {softwareProductId, version, componentId, type}){
71                 return deleteFile(dispatch, {softwareProductId, version, componentId, type});
72         }
73
74 };
75
76 export default SoftwareProductComponentsMonitoringAction;