Add new code new version
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / processes / SoftwareProductComponentProcessesActionHelper.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
22 import Configuration from 'sdc-app/config/Configuration.js';
23 import {actionTypes} from './SoftwareProductComponentProcessesConstants.js';
24
25 function baseUrl(softwareProductId, componentId) {
26         const restPrefix = Configuration.get('restPrefix');
27         return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/components/${componentId}/processes`;
28 }
29
30 function fetchProcessesList({softwareProductId, componentId, version}) {
31         let versionQuery = version ? `?version=${version}` : '';
32         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, componentId)}${versionQuery}`);
33 }
34
35 function deleteProcess({softwareProductId, componentId, processId}) {
36         return RestAPIUtil.destroy(`${baseUrl(softwareProductId, componentId)}/${processId}`);
37 }
38
39 function putProcess({softwareProductId, componentId, process}) {
40         return RestAPIUtil.save(`${baseUrl(softwareProductId, componentId)}/${process.id}`, {
41                 name: process.name,
42                 description: process.description
43         });
44 }
45
46 function postProcess({softwareProductId,componentId, process}) {
47         return RestAPIUtil.create(`${baseUrl(softwareProductId, componentId)}`, {
48                 name: process.name,
49                 description: process.description
50         });
51 }
52
53 function uploadFileToProcess({softwareProductId, processId, componentId, formData}) {
54         return RestAPIUtil.create(`${baseUrl(softwareProductId, componentId)}/${processId}/upload`, formData);
55 }
56
57
58
59 const SoftwareProductComponentProcessesActionHelper = {
60         fetchProcessesList(dispatch, {softwareProductId, componentId, version}) {
61                 dispatch({
62                         type: actionTypes.FETCH_SOFTWARE_PRODUCT_COMPONENTS_PROCESSES,
63                         processesList: []
64                 });
65
66                 return fetchProcessesList({softwareProductId, componentId, version}).then(response => {
67                         dispatch({
68                                 type: actionTypes.FETCH_SOFTWARE_PRODUCT_COMPONENTS_PROCESSES,
69                                 processesList: response.results
70                         });
71                 });
72         },
73
74         deleteProcess(dispatch, {process, softwareProductId, componentId}) {
75                 return deleteProcess({softwareProductId, processId:process.id, componentId}).then(() => {
76                         dispatch({
77                                 type: actionTypes.DELETE_SOFTWARE_PRODUCT_COMPONENTS_PROCESS,
78                                 processId: process.id
79                         });
80                 });
81
82         },
83
84         saveProcess(dispatch, {softwareProductId, componentId, previousProcess, process}) {
85                 if (previousProcess) {
86                         return putProcess({softwareProductId,componentId,  process}).then(() => {
87                                 if (process.formData && process.formData.name !== previousProcess.artifactName){
88                                         uploadFileToProcess({softwareProductId, processId: process.id, formData: process.formData, componentId});
89                                 }
90                                 dispatch({
91                                         type: actionTypes.EDIT_SOFTWARE_PRODUCT_COMPONENTS_PROCESS,
92                                         process
93                                 });
94                         });
95                 }
96                 else {
97                         return postProcess({softwareProductId, componentId, process}).then(response => {
98                                 if (process.formData) {
99                                         uploadFileToProcess({softwareProductId, processId: response.value, formData: process.formData, componentId});
100                                 }
101                                 dispatch({
102                                         type: actionTypes.ADD_SOFTWARE_PRODUCT_COMPONENTS_PROCESS,
103                                         process: {
104                                                 ...process,
105                                                 id: response.value
106                                         }
107                                 });
108                         });
109                 }
110         },
111
112         hideDeleteConfirm(dispatch) {
113                 dispatch({
114                         type: actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_COMPONENTS_CONFIRM,
115                         processToDelete: false
116                 });
117         },
118
119         openDeleteProcessesConfirm(dispatch, {process} ) {
120                 dispatch({
121                         type: actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_COMPONENTS_CONFIRM,
122                         processToDelete: process
123                 });
124         },
125
126         openEditor(dispatch, process = {}) {
127                 dispatch({
128                         type: actionTypes.SOFTWARE_PRODUCT_PROCESS_COMPONENTS_EDITOR_OPEN,
129                         process
130                 });
131         },
132         closeEditor(dispatch) {
133                 dispatch({
134                         type:actionTypes.SOFTWARE_PRODUCT_PROCESS_COMPONENTS_EDITOR_CLOSE
135                 });
136         },
137         processEditorDataChanged(dispatch, {deltaData}) {
138                 dispatch({
139                         type: actionTypes.processEditor.DATA_CHANGED,
140                         deltaData
141                 });
142         }
143 };
144
145 export default SoftwareProductComponentProcessesActionHelper;