chache result in onboarding
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / onboard / Onboard.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
17 import { connect } from 'react-redux';
18 import OnboardView from './OnboardView.jsx';
19 import OnboardingCatalogActionHelper from './onboardingCatalog/OnboardingCatalogActionHelper.js';
20 import OnboardActionHelper from './OnboardActionHelper.js';
21 import LicenseModelCreationActionHelper from '../licenseModel/creation/LicenseModelCreationActionHelper.js';
22 import SoftwareProductCreationActionHelper from '../softwareProduct/creation/SoftwareProductCreationActionHelper.js';
23 import sortByStringProperty from 'nfvo-utils/sortByStringProperty.js';
24 import { tabsMapping } from './onboardingCatalog/OnboardingCatalogConstants.js';
25 import { tabsMapping as onboardTabsMapping } from './OnboardConstants';
26 import { itemStatus } from 'sdc-app/common/helpers/ItemsHelperConstants.js';
27 import { catalogItemStatuses } from './onboardingCatalog/OnboardingCatalogConstants.js';
28
29 export const mapStateToProps = ({
30     onboard: { onboardingCatalog, activeTab, searchValue, filter },
31     licenseModelList,
32     users,
33     archivedLicenseModelList,
34     archivedSoftwareProductList,
35     finalizedLicenseModelList,
36     softwareProductList,
37     finalizedSoftwareProductList,
38     filteredItems
39 }) => {
40     const activeTabName = Object.keys(onboardTabsMapping).filter(item => {
41         return onboardTabsMapping[item] === activeTab;
42     })[0];
43     const fullSoftwareProducts = softwareProductList
44         .filter(
45             vsp =>
46                 !finalizedSoftwareProductList.find(fvsp => fvsp.id === vsp.id)
47         )
48         .concat(finalizedSoftwareProductList);
49
50     const reduceLicenseModelList = (accum, vlm) => {
51         let currentSoftwareProductList = sortByStringProperty(
52             fullSoftwareProducts.filter(vsp => vsp.vendorId === vlm.id),
53             'name'
54         );
55         accum.push({ ...vlm, softwareProductList: currentSoftwareProductList });
56         return accum;
57     };
58
59     const reduceFilteredLicenseModelList = (accum, vlm) => {
60         let currentSoftwareProductList = sortByStringProperty(
61             filteredItems.vspList.filter(vsp => vsp.vendorId === vlm.id),
62             'name'
63         );
64         accum.push({ ...vlm, softwareProductList: currentSoftwareProductList });
65         return accum;
66     };
67
68     const updatedFilteredItems = {
69         vspList: [...filteredItems.vspList],
70         vlmList: sortByStringProperty(
71             filteredItems.vlmList.reduce(reduceFilteredLicenseModelList, []),
72             'name'
73         )
74     };
75
76     licenseModelList = sortByStringProperty(
77         licenseModelList.reduce(reduceLicenseModelList, []),
78         'name'
79     );
80
81     finalizedLicenseModelList = sortByStringProperty(
82         finalizedLicenseModelList.reduce(reduceLicenseModelList, []),
83         'name'
84     );
85
86     const fullLicenseModelList = licenseModelList
87         .filter(
88             vlm => !finalizedLicenseModelList.find(fvlm => fvlm.id === vlm.id)
89         )
90         .concat(finalizedLicenseModelList);
91
92     let {
93         activeTab: catalogActiveTab,
94         vendorCatalog: { vspOverlay, selectedVendor }
95     } = onboardingCatalog;
96     if (filter.byVendorView) {
97         catalogActiveTab = tabsMapping.BY_VENDOR;
98     } else if (filter.itemStatus && filter.itemStatus === itemStatus.ARCHIVED) {
99         catalogActiveTab = tabsMapping.ARCHIVE;
100     }
101
102     return {
103         isArchived: filter.itemStatus === catalogItemStatuses.ARCHIVED,
104         finalizedLicenseModelList,
105         finalizedSoftwareProductList,
106         licenseModelList,
107         softwareProductList,
108         archivedLicenseModelList,
109         archivedSoftwareProductList,
110         fullLicenseModelList,
111         activeTabName,
112         activeTab,
113         catalogActiveTab,
114         searchValue,
115         vspOverlay,
116         selectedVendor,
117         users: users.usersList,
118         filteredItems: updatedFilteredItems
119     };
120 };
121
122 const mapActionsToProps = dispatch => {
123     return {
124         onSelectLicenseModel({ id: licenseModelId, name }, users, tab) {
125             OnboardActionHelper.loadVLMScreen(
126                 dispatch,
127                 { id: licenseModelId, name },
128                 users,
129                 tab
130             );
131         },
132         onSelectSoftwareProduct(softwareProduct, users, tab) {
133             OnboardActionHelper.loadVSPScreen(
134                 dispatch,
135                 softwareProduct,
136                 users,
137                 tab
138             );
139         },
140         onAddSoftwareProductClick: vendorId =>
141             SoftwareProductCreationActionHelper.open(dispatch, vendorId),
142         onAddLicenseModelClick: () =>
143             LicenseModelCreationActionHelper.open(dispatch),
144         onVspOverlayChange: vendor =>
145             OnboardingCatalogActionHelper.changeVspOverlay(dispatch, vendor),
146         closeVspOverlay: () =>
147             OnboardingCatalogActionHelper.closeVspOverlay(dispatch),
148         onCatalogTabClick: tab =>
149             OnboardingCatalogActionHelper.changeActiveTab(dispatch, tab),
150         onTabClick: tab => OnboardActionHelper.changeActiveTab(dispatch, tab),
151         onSearch: (searchValue, activeTab) =>
152             OnboardActionHelper.changeSearchValue(
153                 dispatch,
154                 searchValue,
155                 activeTab
156             ),
157         onVendorSelect: vendor =>
158             OnboardingCatalogActionHelper.onVendorSelect(dispatch, { vendor }),
159         onMigrate: ({ softwareProduct }) =>
160             OnboardingCatalogActionHelper.onMigrate(dispatch, softwareProduct)
161     };
162 };
163
164 export default connect(mapStateToProps, mapActionsToProps)(OnboardView);