react 16 upgrade
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / licenseAgreement / LicenseAgreementActionHelper.js
1 /*
2  * Copyright © 2016-2018 European Support Limited
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 or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import { actionTypes as licenseAgreementActionTypes } from './LicenseAgreementConstants.js';
19 import FeatureGroupsActionHelper from 'sdc-app/onboarding/licenseModel/featureGroups/FeatureGroupsActionHelper.js';
20 import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
21 import {
22     actionTypes as modalActionTypes,
23     modalSizes
24 } from 'nfvo-components/modal/GlobalModalConstants.js';
25 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
26 import i18n from 'nfvo-utils/i18n/i18n.js';
27
28 function baseUrl(licenseModelId, version) {
29     const restPrefix = Configuration.get('restPrefix');
30     const { id: versionId } = version;
31     return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/license-agreements`;
32 }
33
34 function fetchLicenseAgreementList(licenseModelId, version) {
35     return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
36 }
37
38 function fetchLicenseAgreement(licenseModelId, licenseAgreementId, version) {
39     return RestAPIUtil.fetch(
40         `${baseUrl(licenseModelId, version)}/${licenseAgreementId}`
41     );
42 }
43
44 function postLicenseAgreement(licenseModelId, licenseAgreement, version) {
45     return RestAPIUtil.post(baseUrl(licenseModelId, version), {
46         name: licenseAgreement.name,
47         description: licenseAgreement.description,
48         licenseTerm: licenseAgreement.licenseTerm,
49         addedFeatureGroupsIds: licenseAgreement.featureGroupsIds
50     });
51 }
52
53 function putLicenseAgreement(
54     licenseModelId,
55     previousLicenseAgreement,
56     licenseAgreement,
57     version
58 ) {
59     const { featureGroupsIds = [] } = licenseAgreement;
60     const {
61         featureGroupsIds: prevFeatureGroupsIds = []
62     } = previousLicenseAgreement;
63     return RestAPIUtil.put(
64         `${baseUrl(licenseModelId, version)}/${licenseAgreement.id}`,
65         {
66             name: licenseAgreement.name,
67             description: licenseAgreement.description,
68             licenseTerm: licenseAgreement.licenseTerm,
69             addedFeatureGroupsIds: featureGroupsIds.filter(
70                 featureGroupId =>
71                     prevFeatureGroupsIds.indexOf(featureGroupId) === -1
72             ),
73             removedFeatureGroupsIds: prevFeatureGroupsIds.filter(
74                 prevFeatureGroupsId =>
75                     featureGroupsIds.indexOf(prevFeatureGroupsId) === -1
76             )
77         }
78     );
79 }
80
81 function deleteLicenseAgreement(licenseModelId, licenseAgreementId, version) {
82     return RestAPIUtil.destroy(
83         `${baseUrl(licenseModelId, version)}/${licenseAgreementId}`
84     );
85 }
86
87 export default {
88     fetchLicenseAgreementList(dispatch, { licenseModelId, version }) {
89         return fetchLicenseAgreementList(licenseModelId, version).then(
90             response =>
91                 dispatch({
92                     type:
93                         licenseAgreementActionTypes.LICENSE_AGREEMENT_LIST_LOADED,
94                     response
95                 })
96         );
97     },
98
99     fetchLicenseAgreement(
100         dispatch,
101         { licenseModelId, licenseAgreementId, version }
102     ) {
103         return fetchLicenseAgreement(
104             licenseModelId,
105             licenseAgreementId,
106             version
107         );
108     },
109
110     openLicenseAgreementEditor(
111         dispatch,
112         { licenseModelId, licenseAgreement, version, isReadOnlyMode }
113     ) {
114         FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
115             licenseModelId,
116             version
117         });
118         dispatch({
119             type: licenseAgreementActionTypes.licenseAgreementEditor.OPEN,
120             licenseAgreement
121         });
122         dispatch({
123             type: modalActionTypes.GLOBAL_MODAL_SHOW,
124             data: {
125                 modalComponentName: modalContentMapper.LA_EDITOR,
126                 modalComponentProps: {
127                     version,
128                     licenseModelId,
129                     isReadOnlyMode,
130                     size: modalSizes.LARGE
131                 },
132                 title:
133                     licenseModelId && version && licenseAgreement
134                         ? i18n('Edit License Agreement')
135                         : i18n('Create New License Agreement')
136             }
137         });
138     },
139
140     closeLicenseAgreementEditor(dispatch) {
141         dispatch({
142             type: licenseAgreementActionTypes.licenseAgreementEditor.CLOSE
143         });
144         dispatch({
145             type: modalActionTypes.GLOBAL_MODAL_CLOSE
146         });
147     },
148
149     async saveLicenseAgreement(
150         dispatch,
151         { licenseModelId, previousLicenseAgreement, licenseAgreement, version }
152     ) {
153         if (previousLicenseAgreement) {
154             await putLicenseAgreement(
155                 licenseModelId,
156                 previousLicenseAgreement,
157                 licenseAgreement,
158                 version
159             );
160         } else {
161             await postLicenseAgreement(
162                 licenseModelId,
163                 licenseAgreement,
164                 version
165             );
166         }
167         await this.fetchLicenseAgreementList(dispatch, {
168             licenseModelId,
169             version
170         });
171         await FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
172             licenseModelId,
173             version
174         });
175
176         return ItemsHelper.checkItemStatus(dispatch, {
177             itemId: licenseModelId,
178             versionId: version.id
179         });
180     },
181
182     deleteLicenseAgreement(
183         dispatch,
184         { licenseModelId, licenseAgreementId, version }
185     ) {
186         return deleteLicenseAgreement(
187             licenseModelId,
188             licenseAgreementId,
189             version
190         ).then(() => {
191             dispatch({
192                 type: licenseAgreementActionTypes.DELETE_LICENSE_AGREEMENT,
193                 licenseAgreementId
194             });
195             return ItemsHelper.checkItemStatus(dispatch, {
196                 itemId: licenseModelId,
197                 versionId: version.id
198             });
199         });
200     },
201
202     selectLicenseAgreementEditorTab(dispatch, { tab }) {
203         dispatch({
204             type: licenseAgreementActionTypes.licenseAgreementEditor.SELECT_TAB,
205             tab
206         });
207     }
208 };