react 16 upgrade
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / processes / SoftwareProductComponentProcessesActionHelper.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 or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import { actionTypes } from './SoftwareProductComponentProcessesConstants.js';
19 import i18n from 'nfvo-utils/i18n/i18n.js';
20 import {
21     actionTypes as modalActionTypes,
22     modalSizes
23 } from 'nfvo-components/modal/GlobalModalConstants.js';
24 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
25
26 function baseUrl(softwareProductId, version, componentId) {
27     const restPrefix = Configuration.get('restPrefix');
28     return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${
29         version.id
30     }/components/${componentId}/processes`;
31 }
32
33 function fetchProcessesList({ softwareProductId, version, componentId }) {
34     return RestAPIUtil.fetch(
35         `${baseUrl(softwareProductId, version, componentId)}`
36     );
37 }
38
39 function deleteProcess({ softwareProductId, version, componentId, processId }) {
40     return RestAPIUtil.destroy(
41         `${baseUrl(softwareProductId, version, componentId)}/${processId}`
42     );
43 }
44
45 function putProcess({ softwareProductId, version, componentId, process }) {
46     return RestAPIUtil.put(
47         `${baseUrl(softwareProductId, version, componentId)}/${process.id}`,
48         {
49             name: process.name,
50             description: process.description,
51             type: process.type === '' ? null : process.type
52         }
53     );
54 }
55
56 function postProcess({ softwareProductId, version, componentId, process }) {
57     return RestAPIUtil.post(
58         `${baseUrl(softwareProductId, version, componentId)}`,
59         {
60             name: process.name,
61             description: process.description,
62             type: process.type === '' ? null : process.type
63         }
64     );
65 }
66
67 function uploadFileToProcess({
68     softwareProductId,
69     version,
70     processId,
71     componentId,
72     formData
73 }) {
74     return RestAPIUtil.post(
75         `${baseUrl(
76             softwareProductId,
77             version,
78             componentId
79         )}/${processId}/upload`,
80         formData
81     );
82 }
83
84 const SoftwareProductComponentProcessesActionHelper = {
85     fetchProcessesList(dispatch, { softwareProductId, version, componentId }) {
86         dispatch({
87             type: actionTypes.FETCH_SOFTWARE_PRODUCT_COMPONENTS_PROCESSES,
88             processesList: []
89         });
90
91         return fetchProcessesList({
92             softwareProductId,
93             version,
94             componentId
95         }).then(response => {
96             dispatch({
97                 type: actionTypes.FETCH_SOFTWARE_PRODUCT_COMPONENTS_PROCESSES,
98                 processesList: response.results
99             });
100         });
101     },
102
103     deleteProcess(
104         dispatch,
105         { process, softwareProductId, version, componentId }
106     ) {
107         return deleteProcess({
108             softwareProductId,
109             version,
110             processId: process.id,
111             componentId
112         }).then(() => {
113             dispatch({
114                 type: actionTypes.DELETE_SOFTWARE_PRODUCT_COMPONENTS_PROCESS,
115                 processId: process.id
116             });
117         });
118     },
119
120     saveProcess(
121         dispatch,
122         { softwareProductId, version, componentId, previousProcess, process }
123     ) {
124         if (previousProcess) {
125             return putProcess({
126                 softwareProductId,
127                 version,
128                 componentId,
129                 process
130             }).then(() => {
131                 if (
132                     process.formData &&
133                     process.formData.name !== previousProcess.artifactName
134                 ) {
135                     uploadFileToProcess({
136                         softwareProductId,
137                         version,
138                         processId: process.id,
139                         formData: process.formData,
140                         componentId
141                     });
142                 }
143                 dispatch({
144                     type: actionTypes.EDIT_SOFTWARE_PRODUCT_COMPONENTS_PROCESS,
145                     process
146                 });
147             });
148         } else {
149             return postProcess({
150                 softwareProductId,
151                 version,
152                 componentId,
153                 process
154             }).then(response => {
155                 if (process.formData) {
156                     uploadFileToProcess({
157                         softwareProductId,
158                         version,
159                         processId: response.value,
160                         formData: process.formData,
161                         componentId
162                     });
163                 }
164                 dispatch({
165                     type: actionTypes.ADD_SOFTWARE_PRODUCT_COMPONENTS_PROCESS,
166                     process: {
167                         ...process,
168                         id: response.value
169                     }
170                 });
171             });
172         }
173     },
174
175     hideDeleteConfirm(dispatch) {
176         dispatch({
177             type:
178                 actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_COMPONENTS_CONFIRM,
179             processToDelete: false
180         });
181     },
182
183     openDeleteProcessesConfirm(dispatch, { process }) {
184         dispatch({
185             type:
186                 actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_COMPONENTS_CONFIRM,
187             processToDelete: process
188         });
189     },
190
191     openEditor(
192         dispatch,
193         { process, softwareProductId, version, isReadOnlyMode, componentId }
194     ) {
195         dispatch({
196             type: actionTypes.SOFTWARE_PRODUCT_PROCESS_COMPONENTS_EDITOR_OPEN,
197             process: process ? process : {}
198         });
199         dispatch({
200             type: modalActionTypes.GLOBAL_MODAL_SHOW,
201             data: {
202                 modalComponentName: modalContentMapper.COMP_PROCESS_EDITOR,
203                 modalComponentProps: {
204                     version,
205                     softwareProductId,
206                     isReadOnlyMode,
207                     componentId,
208                     size: modalSizes.LARGE
209                 },
210                 bodyClassName: 'edit-process-modal',
211                 title: process
212                     ? i18n('Edit Process Details')
213                     : i18n('Create New Process Details')
214             }
215         });
216     },
217     closeEditor(dispatch) {
218         dispatch({
219             type: actionTypes.SOFTWARE_PRODUCT_PROCESS_COMPONENTS_EDITOR_CLOSE
220         });
221         dispatch({
222             type: modalActionTypes.GLOBAL_MODAL_CLOSE
223         });
224     }
225 };
226
227 export default SoftwareProductComponentProcessesActionHelper;