blob: 2eb455f9ddfe8ad8261575651a2abde9a3a3ef06 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Michael Landoefa037d2017-02-19 12:57:33 +02002 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * 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 Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import {actionTypes, enums} from './OnboardingConstants.js';
talig8e9c0652017-12-20 14:30:43 +020017import {actionTypes as permissionActionTypes} from './permissions/PermissionsConstants.js';
18import {actionTypes as licenseModelCreateActionTypes} from './licenseModel/creation/LicenseModelCreationConstants.js';
19import {actionTypes as softwareProductCreateActionTypes} from './softwareProduct/creation/SoftwareProductCreationConstants.js';
20import {actionTypes as versionCreateActionTypes} from './versionsPage/creation/VersionsPageCreationConstants.js';
21import {SyncStates} from 'sdc-app/common/merge/MergeEditorConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020022
talig8e9c0652017-12-20 14:30:43 +020023import {catalogItemStatuses} from './onboard/onboardingCatalog/OnboardingCatalogConstants.js';
24import Configuration from 'sdc-app/config/Configuration.js';
25
26const checkReadOnly = ({isCollaborator = true, inMerge = false, isCertified = false}) => !isCollaborator || inMerge || isCertified;
27
28const currentScreen = (state = {
29 forceBreadCrumbsUpdate: false,
30 screen: enums.SCREEN.ONBOARDING_CATALOG,
31 itemPermission: {},
32 props: {}
33}, action) => {
34
az2497644017c2017-08-10 17:49:40 +030035 switch (action.type) {
talig8e9c0652017-12-20 14:30:43 +020036
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
az2497644017c2017-08-10 17:49:40 +030067 case actionTypes.SET_CURRENT_SCREEN_VERSION:
68 return {
69 ...state,
70 props: {
71 ...state.props,
talig8e9c0652017-12-20 14:30:43 +020072 version: action.version,
73 isReadOnlyMode: checkReadOnly(state.itemPermission)
az2497644017c2017-08-10 17:49:40 +030074 }
talig8e9c0652017-12-20 14:30:43 +020075 };
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
az2497644017c2017-08-10 17:49:40 +0300133 default:
134 return state;
talig8e9c0652017-12-20 14:30:43 +0200135
136 }
137
Michael Landoefa037d2017-02-19 12:57:33 +0200138};
139
talig8e9c0652017-12-20 14:30:43 +0200140export default currentScreen;