2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13 * or implied. See the License for the specific language governing
14 * permissions and 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(`${baseUrl(softwareProductId, version, componentId)}`);
35 function fetchImage({softwareProductId, componentId, imageId, version}) {
36 return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version, componentId)}/${imageId}`);
39 function destroyImage({softwareProductId, componentId, version, imageId}) {
40 return RestAPIUtil.destroy(`${baseUrl(softwareProductId, version, componentId)}/${imageId}`);
43 function createImage({softwareProductId, componentId, version, data}) {
44 return RestAPIUtil.post(baseUrl(softwareProductId, version, componentId), {
45 fileName: data.fileName
49 function fetchImageQuestionnaire({softwareProductId, componentId, imageId, version}) {
50 return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version, componentId)}/${imageId}/questionnaire`);
53 function saveImage({softwareProductId, version, componentId, image: {id, fileName}}) {
54 return RestAPIUtil.put(`${baseUrl(softwareProductId, version, componentId)}/${id}`,{
60 function saveImageQuestionnaire({softwareProductId, componentId, version, imageId, qdata}) {
61 return RestAPIUtil.put(`${baseUrl(softwareProductId, version, componentId)}/${imageId}/questionnaire`, qdata);
64 const SoftwareProductComponentImagesActionHelper = {
65 fetchImagesList(dispatch, {softwareProductId, componentId, version}) {
67 type: actionTypes.IMAGES_LIST_UPDATE,
71 return fetchImagesList({softwareProductId, componentId, version}).then((response) => {
73 type: actionTypes.IMAGES_LIST_UPDATE,
74 response: response.results,
75 componentId : componentId
80 deleteImage(dispatch, {softwareProductId, componentId, version, imageId}) {
81 return destroyImage({softwareProductId, componentId, version, imageId}).then(() => {
82 return SoftwareProductComponentImagesActionHelper.fetchImagesList(dispatch, {softwareProductId, componentId, version});
86 loadImageData({softwareProductId, componentId, imageId, version}) {
87 return fetchImage({softwareProductId, componentId, imageId, version});
90 openEditImageEditor(dispatch, {image, softwareProductId, componentId, version, isReadOnlyMode, modalClassName}) {
91 return SoftwareProductComponentImagesActionHelper.loadImageData({softwareProductId, componentId, imageId: image.id, version}).then(({data}) => {
92 SoftwareProductComponentImagesActionHelper.loadImageQuestionnaire(dispatch, {
98 SoftwareProductComponentImagesActionHelper.openImageEditor(dispatch, {
111 openImageEditor(dispatch, {image = {}, data = {}, softwareProductId, componentId, version, isReadOnlyMode}) {
113 let title = (image && image.id) ? i18n('Edit Image') : i18n('Create New Image');
114 let className = (image && image.id) ? 'image-edit-editor-model' : 'image-new-editor-modal';
117 type: actionTypes.ImageEditor.OPEN,
118 image: {...data, id: image.id}
122 type: modalActionTypes.GLOBAL_MODAL_SHOW,
124 modalComponentName: modalContentMapper.SOFTWARE_PRODUCT_COMPONENT_IMAGE_EDITOR,
126 modalComponentProps: {softwareProductId, componentId, version, isReadOnlyMode, dialogClassName:className}
131 closeImageEditor(dispatch) {
134 type: modalActionTypes.GLOBAL_MODAL_CLOSE
138 type: actionTypes.ImageEditor.CLOSE
142 loadImageQuestionnaire(dispatch, {softwareProductId, componentId, imageId, version}) {
143 return fetchImageQuestionnaire({softwareProductId, componentId, imageId, version}).then((response) => {
144 ValidationHelper.qDataLoaded(dispatch, {qName: IMAGE_QUESTIONNAIRE ,response: {
145 qdata: response.data ? JSON.parse(response.data) : {},
146 qschema: JSON.parse(response.schema)
151 saveImageDataAndQuestionnaire(dispatch, {softwareProductId, componentId, version, data, qdata}) {
152 SoftwareProductComponentImagesActionHelper.closeImageEditor(dispatch);
153 if (data !== null && data.id) {
154 // editor in edit mode
156 saveImageQuestionnaire({softwareProductId, version, componentId, imageId: data.id, qdata}),
157 saveImage({softwareProductId, version, componentId, image: data}).then(() => {
158 return SoftwareProductComponentImagesActionHelper.fetchImagesList(dispatch, {softwareProductId, componentId, version});
162 // editor in create mode
163 createImage({softwareProductId, componentId, version, data}).then(() => {
164 return SoftwareProductComponentImagesActionHelper.fetchImagesList(dispatch, {softwareProductId, componentId, version});
169 export default SoftwareProductComponentImagesActionHelper;