react 16 upgrade
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / images / SoftwareProductComponentsImageActionHelper.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 i18n from 'nfvo-utils/i18n/i18n.js';
18 import ValidationHelper from 'sdc-app/common/helpers/ValidationHelper.js';
19 import Configuration from 'sdc-app/config/Configuration.js';
20 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
21 import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
22 import { IMAGE_QUESTIONNAIRE } from './SoftwareProductComponentsImageConstants.js';
23 import { actionTypes } from './SoftwareProductComponentsImageConstants.js';
24
25 function baseUrl(softwareProductId, version, componentId) {
26     const versionId = version.id;
27     const restPrefix = Configuration.get('restPrefix');
28     return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/components/${componentId}/images`;
29 }
30
31 function fetchImagesList({ softwareProductId, componentId, version }) {
32     return RestAPIUtil.fetch(
33         `${baseUrl(softwareProductId, version, componentId)}`
34     );
35 }
36
37 function fetchImage({ softwareProductId, componentId, imageId, version }) {
38     return RestAPIUtil.fetch(
39         `${baseUrl(softwareProductId, version, componentId)}/${imageId}`
40     );
41 }
42
43 function destroyImage({ softwareProductId, componentId, version, imageId }) {
44     return RestAPIUtil.destroy(
45         `${baseUrl(softwareProductId, version, componentId)}/${imageId}`
46     );
47 }
48
49 function createImage({ softwareProductId, componentId, version, data }) {
50     return RestAPIUtil.post(baseUrl(softwareProductId, version, componentId), {
51         fileName: data.fileName
52     });
53 }
54
55 function fetchImageQuestionnaire({
56     softwareProductId,
57     componentId,
58     imageId,
59     version
60 }) {
61     return RestAPIUtil.fetch(
62         `${baseUrl(
63             softwareProductId,
64             version,
65             componentId
66         )}/${imageId}/questionnaire`
67     );
68 }
69
70 function saveImage({
71     softwareProductId,
72     version,
73     componentId,
74     image: { id, fileName }
75 }) {
76     return RestAPIUtil.put(
77         `${baseUrl(softwareProductId, version, componentId)}/${id}`,
78         {
79             fileName
80         }
81     );
82 }
83
84 function saveImageQuestionnaire({
85     softwareProductId,
86     componentId,
87     version,
88     imageId,
89     qdata
90 }) {
91     return RestAPIUtil.put(
92         `${baseUrl(
93             softwareProductId,
94             version,
95             componentId
96         )}/${imageId}/questionnaire`,
97         qdata
98     );
99 }
100
101 const SoftwareProductComponentImagesActionHelper = {
102     fetchImagesList(dispatch, { softwareProductId, componentId, version }) {
103         dispatch({
104             type: actionTypes.IMAGES_LIST_UPDATE,
105             response: []
106         });
107
108         return fetchImagesList({
109             softwareProductId,
110             componentId,
111             version
112         }).then(response => {
113             dispatch({
114                 type: actionTypes.IMAGES_LIST_UPDATE,
115                 response: response.results,
116                 componentId: componentId
117             });
118         });
119     },
120
121     deleteImage(
122         dispatch,
123         { softwareProductId, componentId, version, imageId }
124     ) {
125         return destroyImage({
126             softwareProductId,
127             componentId,
128             version,
129             imageId
130         }).then(() => {
131             return SoftwareProductComponentImagesActionHelper.fetchImagesList(
132                 dispatch,
133                 { softwareProductId, componentId, version }
134             );
135         });
136     },
137
138     loadImageData({ softwareProductId, componentId, imageId, version }) {
139         return fetchImage({ softwareProductId, componentId, imageId, version });
140     },
141
142     openEditImageEditor(
143         dispatch,
144         { image, softwareProductId, componentId, version, isReadOnlyMode }
145     ) {
146         return SoftwareProductComponentImagesActionHelper.loadImageData({
147             softwareProductId,
148             componentId,
149             imageId: image.id,
150             version
151         }).then(({ data }) => {
152             SoftwareProductComponentImagesActionHelper.loadImageQuestionnaire(
153                 dispatch,
154                 {
155                     softwareProductId,
156                     componentId,
157                     imageId: image.id,
158                     version
159                 }
160             ).then(() => {
161                 SoftwareProductComponentImagesActionHelper.openImageEditor(
162                     dispatch,
163                     {
164                         softwareProductId,
165                         componentId,
166                         version,
167                         isReadOnlyMode,
168                         image,
169                         data
170                     }
171                 );
172             });
173         });
174     },
175
176     openImageEditor(
177         dispatch,
178         {
179             image = {},
180             data = {},
181             softwareProductId,
182             componentId,
183             version,
184             isReadOnlyMode
185         }
186     ) {
187         let { id } = image;
188         let title = id ? i18n('Edit Image') : i18n('Create New Image');
189         let className = id ? 'image-modal-edit' : 'image-modal-new';
190
191         dispatch({
192             type: actionTypes.ImageEditor.OPEN,
193             image: { ...data, id }
194         });
195
196         dispatch({
197             type: modalActionTypes.GLOBAL_MODAL_SHOW,
198             data: {
199                 modalComponentName:
200                     modalContentMapper.SOFTWARE_PRODUCT_COMPONENT_IMAGE_EDITOR,
201                 title: title,
202                 bodyClassName: className,
203                 modalComponentProps: {
204                     softwareProductId,
205                     componentId,
206                     version,
207                     isReadOnlyMode
208                 }
209             }
210         });
211     },
212
213     closeImageEditor(dispatch) {
214         dispatch({
215             type: modalActionTypes.GLOBAL_MODAL_CLOSE
216         });
217
218         dispatch({
219             type: actionTypes.ImageEditor.CLOSE
220         });
221     },
222
223     loadImageQuestionnaire(
224         dispatch,
225         { softwareProductId, componentId, imageId, version }
226     ) {
227         return fetchImageQuestionnaire({
228             softwareProductId,
229             componentId,
230             imageId,
231             version
232         }).then(response => {
233             ValidationHelper.qDataLoaded(dispatch, {
234                 qName: IMAGE_QUESTIONNAIRE,
235                 response: {
236                     qdata: response.data ? JSON.parse(response.data) : {},
237                     qschema: JSON.parse(response.schema)
238                 }
239             });
240         });
241     },
242
243     saveImageDataAndQuestionnaire(
244         dispatch,
245         { softwareProductId, componentId, version, data, qdata }
246     ) {
247         SoftwareProductComponentImagesActionHelper.closeImageEditor(dispatch);
248         if (data !== null && data.id) {
249             // editor in edit mode
250             return Promise.all([
251                 saveImageQuestionnaire({
252                     softwareProductId,
253                     version,
254                     componentId,
255                     imageId: data.id,
256                     qdata
257                 }),
258                 saveImage({
259                     softwareProductId,
260                     version,
261                     componentId,
262                     image: data
263                 }).then(() => {
264                     return SoftwareProductComponentImagesActionHelper.fetchImagesList(
265                         dispatch,
266                         { softwareProductId, componentId, version }
267                     );
268                 })
269             ]);
270         } else {
271             // editor in create mode
272             createImage({ softwareProductId, componentId, version, data }).then(
273                 () => {
274                     return SoftwareProductComponentImagesActionHelper.fetchImagesList(
275                         dispatch,
276                         { softwareProductId, componentId, version }
277                     );
278                 }
279             );
280         }
281     }
282 };
283
284 export default SoftwareProductComponentImagesActionHelper;