Add collaboration feature
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / SoftwareProductComponentsActionHelper.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 Configuration from 'sdc-app/config/Configuration.js';
18
19 import {actionTypes, COMPONENTS_QUESTIONNAIRE} from './SoftwareProductComponentsConstants.js';
20 import ValidationHelper from 'sdc-app/common/helpers/ValidationHelper.js';
21 import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
22
23 function baseUrl(softwareProductId, version) {
24         const versionId = version.id;
25         const restPrefix = Configuration.get('restPrefix');
26         return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/components`;
27 }
28
29 function fetchSoftwareProductComponents(softwareProductId, version) {
30         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}`);
31 }
32
33 function putSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId, vspComponent) {
34         return RestAPIUtil.put(`${baseUrl(softwareProductId, version)}/${vspComponentId}/questionnaire`, vspComponent);
35 }
36
37 function fetchSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId){
38         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}/${vspComponentId}/questionnaire`);
39 }
40
41 function fetchSoftwareProductComponent(softwareProductId, version, vspComponentId){
42         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}/${vspComponentId}`);
43 }
44
45 function putSoftwareProductComponent(softwareProductId, version, vspComponentId, vspComponent) {
46         return RestAPIUtil.put(`${baseUrl(softwareProductId, version)}/${vspComponentId}`, {
47                 name: vspComponent.name,
48                 displayName: vspComponent.displayName,
49                 vfcCode: vspComponent.vfcCode,
50                 nfcFunction: vspComponent.nfcFunction,
51                 description: vspComponent.description
52         });
53 }
54
55 function deleteSoftwareProductComponent(softwareProductId, componentId, version) {
56         return RestAPIUtil.destroy(`${baseUrl(softwareProductId, version)}/${componentId}`,);
57 }
58
59
60 function postSoftwareProductComponent(softwareProductId, vspComponent, version) {
61
62         return RestAPIUtil.post(`${baseUrl(softwareProductId, version)}`, {
63                 name: vspComponent.displayName,
64                 displayName: vspComponent.displayName,
65                 description: vspComponent.description
66         });
67 }
68
69
70 const SoftwareProductComponentsActionHelper = {
71         fetchSoftwareProductComponents(dispatch, {softwareProductId, version}) {
72                 return fetchSoftwareProductComponents(softwareProductId, version).then(response => {
73                         dispatch({
74                                 type: actionTypes.COMPONENTS_LIST_UPDATE,
75                                 componentsList: response.results
76                         });
77                         return response;
78                 });
79         },
80
81         updateSoftwareProductComponent(dispatch, {softwareProductId, version, vspComponentId, componentData, qdata}) {
82                 return Promise.all([
83                         SoftwareProductComponentsActionHelper.updateSoftwareProductComponentQuestionnaire(dispatch, {softwareProductId, version, vspComponentId, qdata}),
84                         SoftwareProductComponentsActionHelper.updateSoftwareProductComponentData(dispatch, {softwareProductId, version, vspComponentId, componentData})
85                 ]);
86         },
87
88         updateSoftwareProductComponentQuestionnaire(dispatch, {softwareProductId, version, vspComponentId, qdata}) {
89                 return putSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId, qdata);
90         },
91
92         updateSoftwareProductComponentData(dispatch, {softwareProductId, version, vspComponentId, componentData}) {
93                 return putSoftwareProductComponent(softwareProductId, version, vspComponentId, componentData).then(() => dispatch({
94                         type: actionTypes.COMPONENTS_LIST_EDIT,
95                         component: {
96                                 id: vspComponentId,
97                                 ...componentData
98                         }
99                 }));
100         },
101
102         fetchSoftwareProductComponentQuestionnaire(dispatch, {softwareProductId, version, vspComponentId}) {
103                 return fetchSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId).then(response => {
104                         ValidationHelper.qDataLoaded(dispatch, {qName: COMPONENTS_QUESTIONNAIRE, response: {qdata: response.data ? JSON.parse(response.data) : {},
105                                 qschema: JSON.parse(response.schema)}});
106                 });
107         },
108
109         fetchSoftwareProductComponent(dispatch, {softwareProductId, version, vspComponentId}) {
110                 return Promise.all([
111                         fetchSoftwareProductComponent(softwareProductId, version, vspComponentId).then(response => {
112                                 dispatch({
113                                         type: actionTypes.COMPONENT_LOAD,
114                                         component: response.data
115                                 });
116                                 return response;
117                         }),
118                         fetchSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId).then(response => {
119                                 ValidationHelper.qDataLoaded(dispatch, {qName: COMPONENTS_QUESTIONNAIRE, response: {qdata: response.data ? JSON.parse(response.data) : {},
120                                         qschema: JSON.parse(response.schema)}});
121                         })
122                 ]);
123         },
124
125
126         clearComponentsStore(dispatch) {
127                 dispatch({
128                         type: actionTypes.COMPONENTS_LIST_UPDATE,
129                         componentsList: []
130                 });
131         },
132
133         createSoftwareProductComponent(dispatch,{softwareProductId, componentData, version}) {
134                 SoftwareProductComponentsActionHelper.closeComponentCreationModal(dispatch);
135                 /* for mock only */
136
137                 dispatch({
138                         type: actionTypes.COMPONENTS_LIST_UPDATE,
139                         componentsList: [{id: '123', ...componentData}]
140                 });
141
142                 postSoftwareProductComponent(softwareProductId, componentData, version).then(() => {
143                         SoftwareProductComponentsActionHelper.fetchSoftwareProductComponents(dispatch, {softwareProductId, version});
144                 });
145         },
146
147         clearComponentCreationData(dispatch) {
148                 dispatch({
149                         type: actionTypes.COMPONENT_DATA_CLEAR
150                 });
151         },
152
153         closeComponentCreationModal(dispatch) {
154                 dispatch({
155                         type: modalActionTypes.GLOBAL_MODAL_CLOSE
156                 });
157                 SoftwareProductComponentsActionHelper.clearComponentCreationData(dispatch);
158         },
159
160         deleteComponent(dispatch, {softwareProductId, componentId, version}) {
161                 deleteSoftwareProductComponent(softwareProductId, componentId, version);
162                 dispatch({
163                         type: actionTypes.COMPONENT_DELETE,
164                         componentId: componentId
165                 });
166         },
167
168
169
170 };
171
172 export default SoftwareProductComponentsActionHelper;