2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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';
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`;
31 function fetchImagesList({ softwareProductId, componentId, version }) {
32 return RestAPIUtil.fetch(
33 `${baseUrl(softwareProductId, version, componentId)}`
37 function fetchImage({ softwareProductId, componentId, imageId, version }) {
38 return RestAPIUtil.fetch(
39 `${baseUrl(softwareProductId, version, componentId)}/${imageId}`
43 function destroyImage({ softwareProductId, componentId, version, imageId }) {
44 return RestAPIUtil.destroy(
45 `${baseUrl(softwareProductId, version, componentId)}/${imageId}`
49 function createImage({ softwareProductId, componentId, version, data }) {
50 return RestAPIUtil.post(baseUrl(softwareProductId, version, componentId), {
51 fileName: data.fileName
55 function fetchImageQuestionnaire({
61 return RestAPIUtil.fetch(
66 )}/${imageId}/questionnaire`
74 image: { id, fileName }
76 return RestAPIUtil.put(
77 `${baseUrl(softwareProductId, version, componentId)}/${id}`,
84 function saveImageQuestionnaire({
91 return RestAPIUtil.put(
96 )}/${imageId}/questionnaire`,
101 const SoftwareProductComponentImagesActionHelper = {
102 fetchImagesList(dispatch, { softwareProductId, componentId, version }) {
104 type: actionTypes.IMAGES_LIST_UPDATE,
108 return fetchImagesList({
112 }).then(response => {
114 type: actionTypes.IMAGES_LIST_UPDATE,
115 response: response.results,
116 componentId: componentId
123 { softwareProductId, componentId, version, imageId }
125 return destroyImage({
131 return SoftwareProductComponentImagesActionHelper.fetchImagesList(
133 { softwareProductId, componentId, version }
138 loadImageData({ softwareProductId, componentId, imageId, version }) {
139 return fetchImage({ softwareProductId, componentId, imageId, version });
144 { image, softwareProductId, componentId, version, isReadOnlyMode }
146 return SoftwareProductComponentImagesActionHelper.loadImageData({
151 }).then(({ data }) => {
152 SoftwareProductComponentImagesActionHelper.loadImageQuestionnaire(
161 SoftwareProductComponentImagesActionHelper.openImageEditor(
188 let title = id ? i18n('Edit Image') : i18n('Create New Image');
189 let className = id ? 'image-modal-edit' : 'image-modal-new';
192 type: actionTypes.ImageEditor.OPEN,
193 image: { ...data, id }
197 type: modalActionTypes.GLOBAL_MODAL_SHOW,
200 modalContentMapper.SOFTWARE_PRODUCT_COMPONENT_IMAGE_EDITOR,
202 bodyClassName: className,
203 modalComponentProps: {
213 closeImageEditor(dispatch) {
215 type: modalActionTypes.GLOBAL_MODAL_CLOSE
219 type: actionTypes.ImageEditor.CLOSE
223 loadImageQuestionnaire(
225 { softwareProductId, componentId, imageId, version }
227 return fetchImageQuestionnaire({
232 }).then(response => {
233 ValidationHelper.qDataLoaded(dispatch, {
234 qName: IMAGE_QUESTIONNAIRE,
236 qdata: response.data ? JSON.parse(response.data) : {},
237 qschema: JSON.parse(response.schema)
243 saveImageDataAndQuestionnaire(
245 { softwareProductId, componentId, version, data, qdata }
247 SoftwareProductComponentImagesActionHelper.closeImageEditor(dispatch);
248 if (data !== null && data.id) {
249 // editor in edit mode
251 saveImageQuestionnaire({
264 return SoftwareProductComponentImagesActionHelper.fetchImagesList(
266 { softwareProductId, componentId, version }
271 // editor in create mode
272 createImage({ softwareProductId, componentId, version, data }).then(
274 return SoftwareProductComponentImagesActionHelper.fetchImagesList(
276 { softwareProductId, componentId, version }
284 export default SoftwareProductComponentImagesActionHelper;