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 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import {actionTypes, enums} from './OnboardingConstants.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 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 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 23 | import {catalogItemStatuses} from './onboard/onboardingCatalog/OnboardingCatalogConstants.js'; |
| 24 | import Configuration from 'sdc-app/config/Configuration.js'; |
| 25 | |
| 26 | const checkReadOnly = ({isCollaborator = true, inMerge = false, isCertified = false}) => !isCollaborator || inMerge || isCertified; |
| 27 | |
| 28 | const currentScreen = (state = { |
| 29 | forceBreadCrumbsUpdate: false, |
| 30 | screen: enums.SCREEN.ONBOARDING_CATALOG, |
| 31 | itemPermission: {}, |
| 32 | props: {} |
| 33 | }, action) => { |
| 34 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 35 | switch (action.type) { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 36 | |
| 37 | case actionTypes.SET_CURRENT_SCREEN: { |
| 38 | let itemPermission = {...state.itemPermission}; |
| 39 | let {currentScreen} = action; |
| 40 | |
| 41 | if (currentScreen.props.version) { |
| 42 | let {status} = currentScreen.props.version; |
| 43 | itemPermission.isCertified = itemPermission.isCertified && status === catalogItemStatuses.CERTIFIED; |
| 44 | } |
| 45 | |
| 46 | let isReadOnlyMode = checkReadOnly(itemPermission); |
| 47 | let props = {...currentScreen.props, isReadOnlyMode}; |
| 48 | |
| 49 | return { |
| 50 | ...state, |
| 51 | ...currentScreen, |
| 52 | itemPermission, |
| 53 | props |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | case actionTypes.UPDATE_CURRENT_SCREEN_PROPS: |
| 58 | return { |
| 59 | ...state, |
| 60 | props: { |
| 61 | ...state.props, |
| 62 | ...action.props, |
| 63 | isReadOnlyMode: checkReadOnly(state.itemPermission) |
| 64 | } |
| 65 | }; |
| 66 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 67 | case actionTypes.SET_CURRENT_SCREEN_VERSION: |
| 68 | return { |
| 69 | ...state, |
| 70 | props: { |
| 71 | ...state.props, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 72 | version: action.version, |
| 73 | isReadOnlyMode: checkReadOnly(state.itemPermission) |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 74 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | case licenseModelCreateActionTypes.LICENSE_MODEL_CREATED: |
| 78 | case softwareProductCreateActionTypes.SOFTWARE_PRODUCT_CREATED: |
| 79 | case versionCreateActionTypes.VERSION_CREATED: |
| 80 | return { |
| 81 | ...state, |
| 82 | itemPermission: { |
| 83 | isCollaborator: true, |
| 84 | inMerge: false, |
| 85 | isCertified: false |
| 86 | }, |
| 87 | props: { |
| 88 | ...state.props, |
| 89 | isReadOnlyMode: false |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | case permissionActionTypes.ITEM_USERS_LOADED: { |
| 94 | let userId = Configuration.get('UserID'); |
| 95 | let isCollaborator = false; |
| 96 | |
| 97 | if (userId === action.owner.userId) { |
| 98 | isCollaborator = true; |
| 99 | } else { |
| 100 | isCollaborator = action.contributors.reduce( |
| 101 | (foundUser, contributor) => foundUser || contributor.userId === userId, false |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | let itemPermission = {...state.itemPermission, isCollaborator}; |
| 106 | let isReadOnlyMode = checkReadOnly(itemPermission); |
| 107 | let props = {...state.props, isReadOnlyMode}; |
| 108 | |
| 109 | return { |
| 110 | ...state, |
| 111 | itemPermission, |
| 112 | props |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | case actionTypes.UPDATE_ITEM_STATUS: { |
| 117 | const {itemState: {synchronizationState, dirty}, itemStatus, updatedVersion} = action; |
| 118 | const inMerge = synchronizationState === SyncStates.MERGE; |
| 119 | const isOutOfSync = synchronizationState === SyncStates.OUT_OF_SYNC; |
| 120 | const isUpToDate = synchronizationState === SyncStates.UP_TO_DATE; |
| 121 | const isCertified = itemStatus === catalogItemStatuses.CERTIFIED; |
| 122 | const itemPermission = {...state.itemPermission, inMerge, isDirty: dirty, isOutOfSync, isUpToDate, isCertified}; |
| 123 | const isReadOnlyMode = checkReadOnly(itemPermission); |
| 124 | const props = {...state.props, isReadOnlyMode, version: {...state.props.version, ...updatedVersion}}; |
| 125 | |
| 126 | return { |
| 127 | ...state, |
| 128 | itemPermission, |
| 129 | props |
| 130 | }; |
| 131 | } |
| 132 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 133 | default: |
| 134 | return state; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 135 | |
| 136 | } |
| 137 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 138 | }; |
| 139 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 140 | export default currentScreen; |