react 16 upgrade
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / processes / SoftwareProductProcessesActionHelper.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 { actionTypes } from './SoftwareProductProcessesConstants.js';
17 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
18 import Configuration from 'sdc-app/config/Configuration.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(vspId, version) {
27     let { id: versionId } = version;
28     const restPrefix = Configuration.get('restPrefix');
29     return `${restPrefix}/v1.0/vendor-software-products/${vspId}/versions/${versionId}/processes`;
30 }
31
32 function putProcess(softwareProductId, version, process) {
33     return RestAPIUtil.put(
34         `${baseUrl(softwareProductId, version)}/${process.id}`,
35         {
36             name: process.name,
37             description: process.description,
38             type: process.type === '' ? null : process.type
39         }
40     );
41 }
42
43 function postProcess(softwareProductId, version, process) {
44     return RestAPIUtil.post(`${baseUrl(softwareProductId, version)}`, {
45         name: process.name,
46         description: process.description,
47         type: process.type === '' ? null : process.type
48     });
49 }
50
51 function deleteProcess(softwareProductId, version, processId) {
52     return RestAPIUtil.destroy(
53         `${baseUrl(softwareProductId, version)}/${processId}`
54     );
55 }
56
57 function uploadFileToProcess(softwareProductId, version, processId, formData) {
58     return RestAPIUtil.post(
59         `${baseUrl(softwareProductId, version)}/${processId}/upload`,
60         formData
61     );
62 }
63
64 function fetchProcesses(softwareProductId, version) {
65     return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}`);
66 }
67
68 const SoftwareProductActionHelper = {
69     fetchProcessesList(dispatch, { softwareProductId, version }) {
70         dispatch({
71             type: actionTypes.FETCH_SOFTWARE_PRODUCT_PROCESSES,
72             processesList: []
73         });
74
75         return fetchProcesses(softwareProductId, version).then(response => {
76             dispatch({
77                 type: actionTypes.FETCH_SOFTWARE_PRODUCT_PROCESSES,
78                 processesList: response.results
79             });
80         });
81     },
82     openEditor(
83         dispatch,
84         { process, softwareProductId, version, isReadOnlyMode }
85     ) {
86         dispatch({
87             type: actionTypes.SOFTWARE_PRODUCT_PROCESS_EDITOR_OPEN,
88             process: process ? process : {}
89         });
90         dispatch({
91             type: modalActionTypes.GLOBAL_MODAL_SHOW,
92             data: {
93                 modalComponentName: modalContentMapper.PROCESS_EDITOR,
94                 modalComponentProps: {
95                     version,
96                     softwareProductId,
97                     isReadOnlyMode,
98                     size: modalSizes.LARGE
99                 },
100                 bodyClassName: 'edit-process-modal',
101                 title: process
102                     ? i18n('Edit Process Details')
103                     : i18n('Create New Process Details')
104             }
105         });
106     },
107
108     deleteProcess(dispatch, { process, softwareProductId, version }) {
109         return deleteProcess(softwareProductId, version, process.id).then(
110             () => {
111                 dispatch({
112                     type: actionTypes.DELETE_SOFTWARE_PRODUCT_PROCESS,
113                     processId: process.id
114                 });
115             }
116         );
117     },
118
119     closeEditor(dispatch) {
120         dispatch({
121             type: actionTypes.SOFTWARE_PRODUCT_PROCESS_EDITOR_CLOSE
122         });
123         dispatch({
124             type: modalActionTypes.GLOBAL_MODAL_CLOSE
125         });
126     },
127
128     saveProcess(
129         dispatch,
130         { softwareProductId, version, previousProcess, process }
131     ) {
132         if (previousProcess) {
133             return putProcess(softwareProductId, version, process).then(() => {
134                 if (process.formData) {
135                     uploadFileToProcess(
136                         softwareProductId,
137                         version,
138                         process.id,
139                         process.formData
140                     );
141                 }
142                 dispatch({
143                     type: actionTypes.EDIT_SOFTWARE_PRODUCT_PROCESS,
144                     process
145                 });
146             });
147         } else {
148             return postProcess(softwareProductId, version, process).then(
149                 response => {
150                     if (process.formData) {
151                         uploadFileToProcess(
152                             softwareProductId,
153                             version,
154                             response.value,
155                             process.formData
156                         );
157                     }
158                     dispatch({
159                         type: actionTypes.ADD_SOFTWARE_PRODUCT_PROCESS,
160                         process: {
161                             ...process,
162                             id: response.value
163                         }
164                     });
165                 }
166             );
167         }
168     },
169
170     hideDeleteConfirm(dispatch) {
171         dispatch({
172             type: actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_CONFIRM,
173             processToDelete: false
174         });
175     },
176
177     openDeleteProcessesConfirm(dispatch, { process }) {
178         dispatch({
179             type: actionTypes.SOFTWARE_PRODUCT_PROCESS_DELETE_CONFIRM,
180             processToDelete: process
181         });
182     }
183 };
184
185 export default SoftwareProductActionHelper;