Add collaboration feature
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / images / SoftwareProductComponentsImageActionHelper.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13  * or implied. See the License for the specific language governing
14  * permissions and 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(`${baseUrl(softwareProductId, version, componentId)}`);
33 }
34
35 function fetchImage({softwareProductId, componentId, imageId, version}) {
36         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version, componentId)}/${imageId}`);
37 }
38
39 function destroyImage({softwareProductId, componentId, version, imageId}) {
40         return RestAPIUtil.destroy(`${baseUrl(softwareProductId, version, componentId)}/${imageId}`);
41 }
42
43 function createImage({softwareProductId, componentId, version, data}) {
44         return RestAPIUtil.post(baseUrl(softwareProductId, version, componentId), {
45                 fileName: data.fileName
46         });
47 }
48
49 function fetchImageQuestionnaire({softwareProductId, componentId, imageId, version}) {
50         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version, componentId)}/${imageId}/questionnaire`);
51 }
52
53 function saveImage({softwareProductId, version, componentId, image: {id, fileName}}) {
54         return RestAPIUtil.put(`${baseUrl(softwareProductId, version, componentId)}/${id}`,{
55                 fileName
56         });
57
58 }
59
60 function saveImageQuestionnaire({softwareProductId, componentId, version, imageId, qdata}) {
61         return RestAPIUtil.put(`${baseUrl(softwareProductId, version, componentId)}/${imageId}/questionnaire`, qdata);
62 }
63
64 const SoftwareProductComponentImagesActionHelper = {
65         fetchImagesList(dispatch, {softwareProductId, componentId, version}) {
66                 dispatch({
67                         type: actionTypes.IMAGES_LIST_UPDATE,
68                         response: []
69                 });
70
71                 return fetchImagesList({softwareProductId, componentId, version}).then((response) => {
72                         dispatch({
73                                 type: actionTypes.IMAGES_LIST_UPDATE,
74                                 response: response.results,
75                                 componentId : componentId
76                         });
77                 });
78         },
79
80         deleteImage(dispatch, {softwareProductId, componentId, version, imageId}) {
81                 return destroyImage({softwareProductId, componentId, version, imageId}).then(() => {
82                         return SoftwareProductComponentImagesActionHelper.fetchImagesList(dispatch, {softwareProductId, componentId, version});
83                 });
84         },
85
86         loadImageData({softwareProductId, componentId, imageId, version}) {
87                 return fetchImage({softwareProductId, componentId, imageId, version});
88         },
89
90         openEditImageEditor(dispatch, {image, softwareProductId, componentId, version, isReadOnlyMode}) {
91                 return SoftwareProductComponentImagesActionHelper.loadImageData({softwareProductId, componentId, imageId: image.id, version}).then(({data}) => {
92                         SoftwareProductComponentImagesActionHelper.loadImageQuestionnaire(dispatch, {
93                                 softwareProductId,
94                                 componentId,
95                                 imageId: image.id,
96                                 version
97                         }).then(() => {
98                                 SoftwareProductComponentImagesActionHelper.openImageEditor(dispatch, {
99                                         softwareProductId,
100                                         componentId,
101                                         version,
102                                         isReadOnlyMode,
103                                         image,
104                                         data
105                                 });
106                         });
107                 });
108         },
109
110         openImageEditor(dispatch, {image = {}, data = {}, softwareProductId, componentId, version, isReadOnlyMode}) {
111
112                 let {id} = image;
113                 let title = id ?  i18n('Edit Image') : i18n('Create New Image');
114                 let className = id ? 'image-modal-edit' : 'image-modal-new';
115
116                 dispatch({
117                         type: actionTypes.ImageEditor.OPEN,
118                         image: {...data, id}
119                 });
120
121                 dispatch({
122                         type: modalActionTypes.GLOBAL_MODAL_SHOW,
123                         data: {
124                                 modalComponentName: modalContentMapper.SOFTWARE_PRODUCT_COMPONENT_IMAGE_EDITOR,
125                                 title: title,
126                                 modalClassName: className,
127                                 modalComponentProps: {softwareProductId, componentId, version, isReadOnlyMode}
128                         }
129                 });
130
131         },
132
133         closeImageEditor(dispatch) {
134
135                 dispatch({
136                         type: modalActionTypes.GLOBAL_MODAL_CLOSE
137                 });
138
139                 dispatch({
140                         type: actionTypes.ImageEditor.CLOSE
141                 });
142
143         },
144
145         loadImageQuestionnaire(dispatch, {softwareProductId, componentId, imageId, version}) {
146                 return fetchImageQuestionnaire({softwareProductId, componentId, imageId, version}).then((response) => {
147                         ValidationHelper.qDataLoaded(dispatch, {qName: IMAGE_QUESTIONNAIRE ,response: {
148                                 qdata: response.data ? JSON.parse(response.data) : {},
149                                 qschema: JSON.parse(response.schema)
150                         }});
151                 });
152         },
153
154         saveImageDataAndQuestionnaire(dispatch, {softwareProductId, componentId, version, data, qdata}) {
155                 SoftwareProductComponentImagesActionHelper.closeImageEditor(dispatch);
156                 if (data !== null && data.id) {
157                         // editor in edit mode
158                         return Promise.all([
159                                 saveImageQuestionnaire({softwareProductId, version, componentId, imageId: data.id, qdata}),
160                                 saveImage({softwareProductId, version, componentId, image: data}).then(() => {
161                                         return SoftwareProductComponentImagesActionHelper.fetchImagesList(dispatch, {softwareProductId, componentId, version});
162                                 })
163                         ]);
164                 } else {
165                         // editor in create mode
166                         createImage({softwareProductId, componentId, version, data}).then(() => {
167                                 return SoftwareProductComponentImagesActionHelper.fetchImagesList(dispatch, {softwareProductId, componentId, version});
168                         });
169                 }
170         }
171 };
172
173 export default SoftwareProductComponentImagesActionHelper;