Add new code new version
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / SoftwareProductComponentsActionHelper.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
22 import Configuration from 'sdc-app/config/Configuration.js';
23
24 import {actionTypes} from './SoftwareProductComponentsConstants.js';
25
26 function baseUrl(softwareProductId) {
27         const restPrefix = Configuration.get('restPrefix');
28         return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/components`;
29 }
30
31 function fetchSoftwareProductComponents(softwareProductId, version) {
32         let versionQuery = version ? `?version=${version}` : '';
33         return RestAPIUtil.fetch(`${baseUrl(softwareProductId)}${versionQuery}`);
34 }
35
36 function putSoftwareProductComponentQuestionnaire(softwareProductId, vspComponentId, vspComponent) {
37         return RestAPIUtil.save(`${baseUrl(softwareProductId)}/${vspComponentId}/questionnaire`, vspComponent);
38 }
39
40 function fetchSoftwareProductComponentQuestionnaire(softwareProductId, vspComponentId, version){
41         let versionQuery = version ? `?version=${version}` : '';
42         return RestAPIUtil.fetch(`${baseUrl(softwareProductId)}/${vspComponentId}/questionnaire${versionQuery}`);
43 }
44
45 function fetchSoftwareProductComponent(softwareProductId, vspComponentId, version){
46         let versionQuery = version ? `?version=${version}` : '';
47         return RestAPIUtil.fetch(`${baseUrl(softwareProductId)}/${vspComponentId}${versionQuery}`);
48 }
49
50 function putSoftwareProductComponent(softwareProductId, vspComponentId, vspComponent) {
51         return RestAPIUtil.save(`${baseUrl(softwareProductId)}/${vspComponentId}`, {
52                 name: vspComponent.name,
53                 displayName: vspComponent.displayName,
54                 description: vspComponent.description
55         });
56 }
57
58 const SoftwareProductComponentsActionHelper = {
59         fetchSoftwareProductComponents(dispatch, {softwareProductId, version}) {
60                 return fetchSoftwareProductComponents(softwareProductId, version).then(response => {
61                         dispatch({
62                                 type: actionTypes.COMPONENTS_LIST_UPDATE,
63                                 componentsList: response.results
64                         });
65                 });
66         },
67
68         componentDataChanged(dispatch, {deltaData}) {
69                 dispatch({
70                         type: actionTypes.COMPONENT_DATA_CHANGED,
71                         deltaData
72                 });
73         },
74
75
76         updateSoftwareProductComponent(dispatch, {softwareProductId, vspComponentId, componentData, qdata}) {
77                 return Promise.all([
78                         SoftwareProductComponentsActionHelper.updateSoftwareProductComponentQuestionnaire(dispatch, {softwareProductId, vspComponentId, qdata}),
79                         SoftwareProductComponentsActionHelper.updateSoftwareProductComponentData(dispatch, {softwareProductId, vspComponentId, componentData})
80                 ]);
81         },
82
83         updateSoftwareProductComponentQuestionnaire(dispatch, {softwareProductId, vspComponentId, qdata}) {
84                 return putSoftwareProductComponentQuestionnaire(softwareProductId, vspComponentId, qdata);
85         },
86
87         updateSoftwareProductComponentData(dispatch, {softwareProductId, vspComponentId, componentData}) {
88                 return putSoftwareProductComponent(softwareProductId, vspComponentId, componentData).then(() => dispatch({
89                         type: actionTypes.COMPONENTS_LIST_EDIT,
90                         component: {
91                                 id: vspComponentId,
92                                 ...componentData
93                         }
94                 }));
95         },
96
97
98         fetchSoftwareProductComponentQuestionnaire(dispatch, {softwareProductId, vspComponentId, version}) {
99                 return fetchSoftwareProductComponentQuestionnaire(softwareProductId, vspComponentId, version).then(response => {
100                         dispatch({
101                                 type: actionTypes.COMPONENT_QUESTIONNAIRE_UPDATE,
102                                 payload: {
103                                         qdata: response.data ? JSON.parse(response.data) : {},
104                                         qschema: JSON.parse(response.schema)
105                                 }
106                         });
107                 });
108         },
109
110         fetchSoftwareProductComponent(dispatch, {softwareProductId, vspComponentId, version}) {
111                 return fetchSoftwareProductComponent(softwareProductId, vspComponentId, version).then(response => {
112                         dispatch({
113                                 type: actionTypes.COMPONENT_UPDATE,
114                                 component: response.data
115                         });
116                 });
117         },
118
119         componentQuestionnaireUpdated(dispatch, {data}) {
120                 dispatch({
121                         type: actionTypes.COMPONENT_QUESTIONNAIRE_UPDATE,
122                         payload: {
123                                 qdata: data
124                         }
125                 });
126         },
127 };
128
129 export default SoftwareProductComponentsActionHelper;