svishnev | c72587e | 2018-09-12 13:25:42 +0300 | [diff] [blame] | 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 | */ |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 16 | import { connect } from 'react-redux'; |
svishnev | c72587e | 2018-09-12 13:25:42 +0300 | [diff] [blame] | 17 | import sortByStringProperty from 'nfvo-utils/sortByStringProperty.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 18 | import LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 19 | import LicenseModelOverviewView from './LicenseModelOverviewView.jsx'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 20 | import { |
| 21 | overviewEditorHeaders, |
| 22 | selectedButton |
| 23 | } from './LicenseModelOverviewConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 24 | import licenseModelOverviewActionHelper from './licenseModelOverviewActionHelper.js'; |
svishnev | c72587e | 2018-09-12 13:25:42 +0300 | [diff] [blame] | 25 | import { SORTING_PROPERTY_NAME } from 'sdc-app/onboarding/licenseModel/LicenseModelConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 26 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 27 | export const mapStateToProps = ({ |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 28 | licenseModel: { |
| 29 | licenseModelEditor, |
| 30 | entitlementPool, |
| 31 | licenseAgreement, |
| 32 | featureGroup, |
| 33 | licenseKeyGroup, |
| 34 | licenseModelOverview |
| 35 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 36 | }) => { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 37 | let modalHeader, licensingDataList; |
| 38 | let isDisplayModal = false; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 39 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 40 | const reduceLicenseKeyGroups = (accum, licenseKeyGroupId) => { |
| 41 | let curLicenseKeyGroup = licenseKeyGroup.licenseKeyGroupsList.find( |
| 42 | item => { |
| 43 | return item.id === licenseKeyGroupId; |
| 44 | } |
| 45 | ); |
| 46 | if (curLicenseKeyGroup) { |
| 47 | accum.push({ |
| 48 | ...curLicenseKeyGroup, |
| 49 | itemType: overviewEditorHeaders.LICENSE_KEY_GROUP |
| 50 | }); |
| 51 | } |
| 52 | return accum; |
| 53 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 54 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 55 | const reduceEntitlementPools = (accum, entitlementPoolId) => { |
| 56 | let curEntitlementPool = entitlementPool.entitlementPoolsList.find( |
| 57 | item => { |
| 58 | return item.id === entitlementPoolId; |
| 59 | } |
| 60 | ); |
| 61 | if (curEntitlementPool) { |
| 62 | accum.push({ |
| 63 | ...curEntitlementPool, |
| 64 | itemType: overviewEditorHeaders.ENTITLEMENT_POOL |
| 65 | }); |
| 66 | } |
| 67 | return accum; |
| 68 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 69 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 70 | const reduceFeatureGroups = (accum, featureGroupId) => { |
| 71 | let curFeatureGroup = featureGroup.featureGroupsList.find(item => { |
| 72 | return item.id === featureGroupId; |
| 73 | }); |
| 74 | if (curFeatureGroup) { |
| 75 | let { |
| 76 | entitlementPoolsIds = [], |
| 77 | licenseKeyGroupsIds = [] |
| 78 | } = curFeatureGroup; |
| 79 | accum.push({ |
| 80 | ...curFeatureGroup, |
| 81 | itemType: overviewEditorHeaders.FEATURE_GROUP, |
| 82 | children: [ |
svishnev | c72587e | 2018-09-12 13:25:42 +0300 | [diff] [blame] | 83 | ...sortByStringProperty( |
| 84 | entitlementPoolsIds.length |
| 85 | ? entitlementPoolsIds.reduce( |
| 86 | reduceEntitlementPools, |
| 87 | [] |
| 88 | ) |
| 89 | : [], |
| 90 | SORTING_PROPERTY_NAME |
| 91 | ), |
| 92 | ...sortByStringProperty( |
| 93 | licenseKeyGroupsIds.length |
| 94 | ? licenseKeyGroupsIds.reduce( |
| 95 | reduceLicenseKeyGroups, |
| 96 | [] |
| 97 | ) |
| 98 | : [], |
| 99 | SORTING_PROPERTY_NAME |
| 100 | ) |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 101 | ] |
| 102 | }); |
| 103 | } |
| 104 | return accum; |
| 105 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 106 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 107 | const checkEP = (accum, elem) => { |
| 108 | if ( |
| 109 | !elem.referencingFeatureGroups || |
| 110 | !elem.referencingFeatureGroups.length |
| 111 | ) { |
| 112 | accum.push({ |
| 113 | ...elem, |
| 114 | itemType: overviewEditorHeaders.ENTITLEMENT_POOL |
| 115 | }); |
| 116 | } |
| 117 | return accum; |
| 118 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 119 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 120 | const checkLG = (accum, elem) => { |
| 121 | if ( |
| 122 | !elem.referencingFeatureGroups || |
| 123 | !elem.referencingFeatureGroups.length |
| 124 | ) { |
| 125 | accum.push({ |
| 126 | ...elem, |
| 127 | itemType: overviewEditorHeaders.LICENSE_KEY_GROUP |
| 128 | }); |
| 129 | } |
| 130 | return accum; |
| 131 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 132 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 133 | const checkFG = (accum, elem) => { |
| 134 | if ( |
| 135 | !elem.referencingLicenseAgreements || |
| 136 | !elem.referencingLicenseAgreements.length |
| 137 | ) { |
| 138 | let { entitlementPoolsIds = [], licenseKeyGroupsIds = [] } = elem; |
| 139 | accum.push({ |
| 140 | ...elem, |
| 141 | itemType: overviewEditorHeaders.FEATURE_GROUP, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 142 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 143 | children: [ |
| 144 | ...(entitlementPoolsIds.length |
| 145 | ? entitlementPoolsIds.reduce(reduceEntitlementPools, []) |
| 146 | : []), |
| 147 | ...(licenseKeyGroupsIds.length |
| 148 | ? licenseKeyGroupsIds.reduce(reduceLicenseKeyGroups, []) |
| 149 | : []) |
| 150 | ] |
| 151 | }); |
| 152 | } |
| 153 | return accum; |
| 154 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 155 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 156 | const mapLicenseAgreementData = licenseAgreement => { |
| 157 | let { featureGroupsIds = [] } = licenseAgreement; |
| 158 | return { |
| 159 | ...licenseAgreement, |
| 160 | itemType: overviewEditorHeaders.LICENSE_AGREEMENT, |
| 161 | children: featureGroupsIds.length |
svishnev | c72587e | 2018-09-12 13:25:42 +0300 | [diff] [blame] | 162 | ? sortByStringProperty( |
| 163 | featureGroupsIds.reduce(reduceFeatureGroups, []), |
| 164 | SORTING_PROPERTY_NAME |
| 165 | ) |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 166 | : [] |
| 167 | }; |
| 168 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 169 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 170 | if ( |
| 171 | entitlementPool.entitlementPoolEditor && |
| 172 | entitlementPool.entitlementPoolEditor.data |
| 173 | ) { |
| 174 | modalHeader = overviewEditorHeaders.ENTITLEMENT_POOL; |
| 175 | isDisplayModal = true; |
| 176 | } else if ( |
| 177 | licenseAgreement.licenseAgreementEditor && |
| 178 | licenseAgreement.licenseAgreementEditor.data |
| 179 | ) { |
| 180 | modalHeader = overviewEditorHeaders.LICENSE_AGREEMENT; |
| 181 | isDisplayModal = true; |
| 182 | } else if ( |
| 183 | featureGroup.featureGroupEditor && |
| 184 | featureGroup.featureGroupEditor.data |
| 185 | ) { |
| 186 | modalHeader = overviewEditorHeaders.FEATURE_GROUP; |
| 187 | isDisplayModal = true; |
| 188 | } else if ( |
| 189 | licenseKeyGroup.licenseKeyGroupsEditor && |
| 190 | licenseKeyGroup.licenseKeyGroupsEditor.data |
| 191 | ) { |
| 192 | modalHeader = overviewEditorHeaders.LICENSE_KEY_GROUP; |
| 193 | isDisplayModal = true; |
| 194 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 195 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 196 | let orphanDataList = [ |
svishnev | c72587e | 2018-09-12 13:25:42 +0300 | [diff] [blame] | 197 | ...sortByStringProperty( |
| 198 | featureGroup.featureGroupsList.reduce(checkFG, []), |
| 199 | SORTING_PROPERTY_NAME |
| 200 | ), |
| 201 | ...sortByStringProperty( |
| 202 | entitlementPool.entitlementPoolsList.reduce(checkEP, []), |
| 203 | SORTING_PROPERTY_NAME |
| 204 | ), |
| 205 | ...sortByStringProperty( |
| 206 | licenseKeyGroup.licenseKeyGroupsList.reduce(checkLG, []), |
| 207 | SORTING_PROPERTY_NAME |
| 208 | ) |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 209 | ]; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 210 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 211 | licensingDataList = |
| 212 | licenseAgreement.licenseAgreementList && |
| 213 | licenseAgreement.licenseAgreementList.length |
svishnev | c72587e | 2018-09-12 13:25:42 +0300 | [diff] [blame] | 214 | ? sortByStringProperty( |
| 215 | licenseAgreement.licenseAgreementList.map( |
| 216 | mapLicenseAgreementData |
| 217 | ), |
| 218 | SORTING_PROPERTY_NAME |
| 219 | ) |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 220 | : []; |
| 221 | let selectedTab = licenseModelOverview.selectedTab; |
| 222 | // on first entry, we will decide what tab to open depending on data. if there are no connections, we will open the orphans |
| 223 | if (selectedTab === null) { |
| 224 | selectedTab = licensingDataList.length |
| 225 | ? selectedButton.VLM_LIST_VIEW |
| 226 | : selectedButton.NOT_IN_USE; |
| 227 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 228 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 229 | return { |
| 230 | isDisplayModal, |
| 231 | modalHeader, |
| 232 | licenseModelId: licenseModelEditor.data.id, |
| 233 | version: licenseModelEditor.data.version, |
| 234 | licensingDataList, |
| 235 | orphanDataList, |
| 236 | selectedTab |
| 237 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 238 | }; |
| 239 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 240 | const mapActionsToProps = (dispatch, { licenseModelId }) => { |
| 241 | return { |
| 242 | onCallVCAction: action => { |
| 243 | LicenseModelActionHelper.performVCAction(dispatch, { |
| 244 | licenseModelId, |
| 245 | action |
| 246 | }); |
| 247 | }, |
| 248 | onTabSelect: buttonTab => |
| 249 | licenseModelOverviewActionHelper.selectVLMListView(dispatch, { |
| 250 | buttonTab |
| 251 | }) |
| 252 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 253 | }; |
| 254 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 255 | export default connect(mapStateToProps, mapActionsToProps)( |
| 256 | LicenseModelOverviewView |
| 257 | ); |