[SDC-29] rebase continue work to align source
[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
22 function baseUrl(softwareProductId, version) {
23         const versionId = version.id;
24         const restPrefix = Configuration.get('restPrefix');
25         return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/components`;
26 }
27
28 function fetchSoftwareProductComponents(softwareProductId, version) {
29         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}`);
30 }
31
32 function putSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId, vspComponent) {
33         return RestAPIUtil.put(`${baseUrl(softwareProductId, version)}/${vspComponentId}/questionnaire`, vspComponent);
34 }
35
36 function fetchSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId){
37         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}/${vspComponentId}/questionnaire`);
38 }
39
40 function fetchSoftwareProductComponent(softwareProductId, version, vspComponentId){
41         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}/${vspComponentId}`);
42 }
43
44 function putSoftwareProductComponent(softwareProductId, version, vspComponentId, vspComponent) {
45         return RestAPIUtil.put(`${baseUrl(softwareProductId, version)}/${vspComponentId}`, {
46                 name: vspComponent.name,
47                 displayName: vspComponent.displayName,
48                 vfcCode: vspComponent.vfcCode,
49                 description: vspComponent.description
50         });
51 }
52
53 const SoftwareProductComponentsActionHelper = {
54         fetchSoftwareProductComponents(dispatch, {softwareProductId, version}) {
55                 return fetchSoftwareProductComponents(softwareProductId, version).then(response => {
56                         dispatch({
57                                 type: actionTypes.COMPONENTS_LIST_UPDATE,
58                                 componentsList: response.results
59                         });
60                 });
61         },
62
63         updateSoftwareProductComponent(dispatch, {softwareProductId, version, vspComponentId, componentData, qdata}) {
64                 return Promise.all([
65                         SoftwareProductComponentsActionHelper.updateSoftwareProductComponentQuestionnaire(dispatch, {softwareProductId, version, vspComponentId, qdata}),
66                         SoftwareProductComponentsActionHelper.updateSoftwareProductComponentData(dispatch, {softwareProductId, version, vspComponentId, componentData})
67                 ]);
68         },
69
70         updateSoftwareProductComponentQuestionnaire(dispatch, {softwareProductId, version, vspComponentId, qdata}) {
71                 return putSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId, qdata);
72         },
73
74         updateSoftwareProductComponentData(dispatch, {softwareProductId, version, vspComponentId, componentData}) {
75                 return putSoftwareProductComponent(softwareProductId, version, vspComponentId, componentData).then(() => dispatch({
76                         type: actionTypes.COMPONENTS_LIST_EDIT,
77                         component: {
78                                 id: vspComponentId,
79                                 ...componentData
80                         }
81                 }));
82         },
83
84         fetchSoftwareProductComponentQuestionnaire(dispatch, {softwareProductId, version, vspComponentId}) {
85                 return fetchSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId).then(response => {
86                         ValidationHelper.qDataLoaded(dispatch, {qName: COMPONENTS_QUESTIONNAIRE, response: {qdata: response.data ? JSON.parse(response.data) : {},
87                                 qschema: JSON.parse(response.schema)}});
88                 });
89         },
90
91         fetchSoftwareProductComponent(dispatch, {softwareProductId, version, vspComponentId}) {
92                 return Promise.all([
93                         fetchSoftwareProductComponent(softwareProductId, version, vspComponentId).then(response => {
94                                 dispatch({
95                                         type: actionTypes.COMPONENT_LOAD,
96                                         component: response.data
97                                 });
98                                 return response;
99                         }),
100                         fetchSoftwareProductComponentQuestionnaire(softwareProductId, version, vspComponentId).then(response => {
101                                 ValidationHelper.qDataLoaded(dispatch, {qName: COMPONENTS_QUESTIONNAIRE, response: {qdata: response.data ? JSON.parse(response.data) : {},
102                                         qschema: JSON.parse(response.schema)}});
103                         })
104                 ]);
105         },
106
107
108         clearComponentsStore(dispatch) {
109                 dispatch({
110                         type: actionTypes.COMPONENTS_LIST_UPDATE,
111                         componentsList: []
112                 });
113         }
114 };
115
116 export default SoftwareProductComponentsActionHelper;