AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 16 | import { actionTypes, enums } from './OnboardingConstants.js'; |
| 17 | import { actionTypes as permissionActionTypes } from './permissions/PermissionsConstants.js'; |
| 18 | import { actionTypes as licenseModelCreateActionTypes } from './licenseModel/creation/LicenseModelCreationConstants.js'; |
| 19 | import { actionTypes as softwareProductCreateActionTypes } from './softwareProduct/creation/SoftwareProductCreationConstants.js'; |
| 20 | import { actionTypes as versionCreateActionTypes } from './versionsPage/creation/VersionsPageCreationConstants.js'; |
| 21 | import { SyncStates } from 'sdc-app/common/merge/MergeEditorConstants.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 22 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 23 | import { catalogItemStatuses } from './onboard/onboardingCatalog/OnboardingCatalogConstants.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 24 | import Configuration from 'sdc-app/config/Configuration.js'; |
| 25 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 26 | const checkReadOnly = ({ |
| 27 | isCollaborator = true, |
| 28 | inMerge = false, |
| 29 | isCertified = false, |
| 30 | isArchived = false |
| 31 | }) => !isCollaborator || inMerge || isCertified || isArchived; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 32 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 33 | const currentScreen = ( |
| 34 | state = { |
| 35 | forceBreadCrumbsUpdate: false, |
| 36 | screen: enums.SCREEN.ONBOARDING_CATALOG, |
| 37 | itemPermission: {}, |
| 38 | props: {} |
| 39 | }, |
| 40 | action |
| 41 | ) => { |
| 42 | switch (action.type) { |
| 43 | case actionTypes.SET_CURRENT_SCREEN: { |
| 44 | let itemPermission = { ...state.itemPermission }; |
| 45 | let { currentScreen } = action; |
| 46 | itemPermission.isArchived = |
| 47 | currentScreen.props.status === catalogItemStatuses.ARCHIVED; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 48 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 49 | if (currentScreen.props.version) { |
| 50 | let { status } = currentScreen.props.version; |
| 51 | itemPermission.isCertified = |
| 52 | itemPermission.isCertified && |
| 53 | status === catalogItemStatuses.CERTIFIED; |
| 54 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 55 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 56 | let isReadOnlyMode = checkReadOnly(itemPermission); |
| 57 | let props = { ...currentScreen.props, isReadOnlyMode }; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 58 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 59 | return { |
| 60 | ...state, |
| 61 | ...currentScreen, |
| 62 | itemPermission, |
| 63 | props |
| 64 | }; |
| 65 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 66 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 67 | case actionTypes.UPDATE_CURRENT_SCREEN_PROPS: |
| 68 | return { |
| 69 | ...state, |
| 70 | props: { |
| 71 | ...state.props, |
| 72 | ...action.props, |
| 73 | isReadOnlyMode: checkReadOnly(state.itemPermission) |
| 74 | } |
| 75 | }; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 76 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 77 | case actionTypes.SET_CURRENT_SCREEN_VERSION: |
| 78 | return { |
| 79 | ...state, |
| 80 | props: { |
| 81 | ...state.props, |
| 82 | version: action.version, |
| 83 | isReadOnlyMode: checkReadOnly({ |
| 84 | ...state.itemPermission, |
| 85 | itemStatus: state.props.status |
| 86 | }) |
| 87 | } |
| 88 | }; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 89 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 90 | case licenseModelCreateActionTypes.LICENSE_MODEL_CREATED: |
| 91 | case softwareProductCreateActionTypes.SOFTWARE_PRODUCT_CREATED: |
| 92 | case versionCreateActionTypes.VERSION_CREATED: |
| 93 | return { |
| 94 | ...state, |
| 95 | itemPermission: { |
| 96 | isCollaborator: true, |
| 97 | inMerge: false, |
| 98 | isCertified: false |
| 99 | }, |
| 100 | props: { |
| 101 | ...state.props, |
| 102 | isReadOnlyMode: false |
| 103 | } |
| 104 | }; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 105 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 106 | case permissionActionTypes.ITEM_USERS_LOADED: { |
| 107 | let userId = Configuration.get('UserID'); |
| 108 | let isCollaborator = false; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 109 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 110 | if (userId === action.owner.userId) { |
| 111 | isCollaborator = true; |
| 112 | } else { |
| 113 | isCollaborator = action.contributors.reduce( |
| 114 | (foundUser, contributor) => |
| 115 | foundUser || contributor.userId === userId, |
| 116 | false |
| 117 | ); |
| 118 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 119 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 120 | let itemPermission = { ...state.itemPermission, isCollaborator }; |
| 121 | let isReadOnlyMode = checkReadOnly(itemPermission); |
| 122 | let props = { ...state.props, isReadOnlyMode }; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 123 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 124 | return { |
| 125 | ...state, |
| 126 | itemPermission, |
| 127 | props |
| 128 | }; |
| 129 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 130 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 131 | case actionTypes.UPDATE_ITEM_STATUS: { |
| 132 | const { |
| 133 | itemState: { synchronizationState, dirty }, |
| 134 | itemStatus, |
| 135 | updatedVersion, |
| 136 | archivedStatus |
| 137 | } = action; |
| 138 | const inMerge = synchronizationState === SyncStates.MERGE; |
| 139 | const isOutOfSync = synchronizationState === SyncStates.OUT_OF_SYNC; |
| 140 | const isUpToDate = synchronizationState === SyncStates.UP_TO_DATE; |
| 141 | const isCertified = itemStatus === catalogItemStatuses.CERTIFIED; |
| 142 | const isArchived = archivedStatus === catalogItemStatuses.ARCHIVED; |
| 143 | const itemPermission = { |
| 144 | ...state.itemPermission, |
| 145 | inMerge, |
| 146 | isDirty: dirty, |
| 147 | isOutOfSync, |
| 148 | isUpToDate, |
| 149 | isCertified, |
| 150 | isArchived |
| 151 | }; |
| 152 | const isReadOnlyMode = checkReadOnly(itemPermission); |
| 153 | const props = { |
| 154 | ...state.props, |
| 155 | isReadOnlyMode, |
| 156 | version: { ...state.props.version, ...updatedVersion } |
| 157 | }; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 158 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 159 | return { |
| 160 | ...state, |
| 161 | itemPermission, |
| 162 | props |
| 163 | }; |
| 164 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 165 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 166 | default: |
| 167 | return state; |
| 168 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 169 | }; |
| 170 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 171 | export default currentScreen; |