Add new code new version
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / processes / SoftwareProductProcessesActionHelper.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 {actionTypes} from './SoftwareProductProcessesConstants.js';
22 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
23 import Configuration from 'sdc-app/config/Configuration.js';
24
25 function baseUrl(svpId) {
26         const restPrefix = Configuration.get('restPrefix');
27         return `${restPrefix}/v1.0/vendor-software-products/${svpId}/processes`;
28 }
29
30 function putProcess(softwareProductId, process) {
31         return RestAPIUtil.save(`${baseUrl(softwareProductId)}/${process.id}`, {
32                 name: process.name,
33                 description: process.description
34         });
35 }
36
37 function postProcess(softwareProductId, process) {
38         return RestAPIUtil.create(`${baseUrl(softwareProductId)}`, {
39                 name: process.name,
40                 description: process.description
41         });
42 }
43
44 function deleteProcess(softwareProductId, processId) {
45         return RestAPIUtil.destroy(`${baseUrl(softwareProductId)}/${processId}`);
46 }
47
48 function uploadFileToProcess(softwareProductId, processId, formData)
49 {
50         return RestAPIUtil.create(`${baseUrl(softwareProductId)}/${processId}/upload`, formData);
51 }
52
53 function fetchProcesses(softwareProductId, version) {
54         let versionQuery = version ? `?version=${version}` : '';
55         return RestAPIUtil.fetch(`${baseUrl(softwareProductId)}${versionQuery}`);
56 }
57
58
59
60 const SoftwareProductActionHelper = {
61
62         fetchProcessesList(dispatch, {softwareProductId, version}) {
63
64                 dispatch({
65                         type: actionTypes.FETCH_SOFTWARE_PRODUCT_PROCESSES,
66                         processesList: []
67                 });
68
69                 return fetchProcesses(softwareProductId, version).then(response => {
70                         dispatch({
71                                 type: actionTypes.FETCH_SOFTWARE_PRODUCT_PROCESSES,
72                                 processesList: response.results
73                         });
74                 });
75         },
76         openEditor(dispatch, process = {}) {
77                 dispatch({
78                         type: actionTypes.SOFTWARE_PRODUCT_PROCESS_EDITOR_OPEN,
79                         process
80                 });
81         },
82
83         deleteProcess(dispatch, {process, softwareProductId}) {
84                 return deleteProcess(softwareProductId, process.id).then(() => {
85                         dispatch({
86                                 type: actionTypes.DELETE_SOFTWARE_PRODUCT_PROCESS,
87                                 processId: process.id
88                         });
89                 });
90
91         },
92
93         closeEditor(dispatch) {
94                 dispatch({
95                         type:actionTypes.SOFTWARE_PRODUCT_PROCESS_EDITOR_CLOSE
96                 });
97         },
98
99         processEditorDataChanged(dispatch, {deltaData}) {
100                 dispatch({
101                         type: actionTypes.processEditor.DATA_CHANGED,
102                         deltaData
103                 });
104         },
105
106         saveProcess(dispatch, {softwareProductId, previousProcess, process}) {
107                 if (previousProcess) {
108                         return putProcess(softwareProductId, process).then(() => {
109                                 if (process.formData){
110                                         uploadFileToProcess(softwareProductId, process.id, process.formData);
111                                 }
112                                 dispatch({
113                                         type: actionTypes.EDIT_SOFTWARE_PRODUCT_PROCESS,
114                                         process
115                                 });
116                         });
117                 }
118                 else {
119                         return postProcess(softwareProductId, process).then(response => {
120                                 if (process.formData) {
121                                         uploadFileToProcess(softwareProductId, response.value, process.formData);
122                                 }
123                                 dispatch({
124                                         type: actionTypes.ADD_SOFTWARE_PRODUCT_PROCESS,
125                                         process: {
126                                                 ...process,
127                                                 id: response.value
128                                         }
129                                 });
130                         });
131                 }
132         },
133
134         hideDeleteConfirm(dispatch) {
135                 dispatch({
136                         type: actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_CONFIRM,
137                         processToDelete: false
138                 });
139         },
140
141         openDeleteProcessesConfirm(dispatch, {process} ) {
142                 dispatch({
143                         type: actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_CONFIRM,
144                         processToDelete: process
145                 });
146         }
147
148 };
149
150 export default SoftwareProductActionHelper;
151