blob: 9abae2d1c1aa035cfd534ea594fc00b13a3641b8 [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 */
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020016import { actionTypes, enums } from './OnboardingConstants.js';
17import { 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
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020023import { catalogItemStatuses } from './onboard/onboardingCatalog/OnboardingCatalogConstants.js';
talig8e9c0652017-12-20 14:30:43 +020024import Configuration from 'sdc-app/config/Configuration.js';
25
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020026const checkReadOnly = ({
27 isCollaborator = true,
28 inMerge = false,
29 isCertified = false,
30 isArchived = false
31}) => !isCollaborator || inMerge || isCertified || isArchived;
talig8e9c0652017-12-20 14:30:43 +020032
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020033const 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;
talig8e9c0652017-12-20 14:30:43 +020048
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020049 if (currentScreen.props.version) {
50 let { status } = currentScreen.props.version;
51 itemPermission.isCertified =
52 itemPermission.isCertified &&
53 status === catalogItemStatuses.CERTIFIED;
54 }
talig8e9c0652017-12-20 14:30:43 +020055
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020056 let isReadOnlyMode = checkReadOnly(itemPermission);
57 let props = { ...currentScreen.props, isReadOnlyMode };
talig8e9c0652017-12-20 14:30:43 +020058
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020059 return {
60 ...state,
61 ...currentScreen,
62 itemPermission,
63 props
64 };
65 }
talig8e9c0652017-12-20 14:30:43 +020066
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020067 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 };
talig8e9c0652017-12-20 14:30:43 +020076
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020077 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 };
talig8e9c0652017-12-20 14:30:43 +020089
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020090 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 };
talig8e9c0652017-12-20 14:30:43 +0200105
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200106 case permissionActionTypes.ITEM_USERS_LOADED: {
107 let userId = Configuration.get('UserID');
108 let isCollaborator = false;
talig8e9c0652017-12-20 14:30:43 +0200109
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200110 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 }
talig8e9c0652017-12-20 14:30:43 +0200119
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200120 let itemPermission = { ...state.itemPermission, isCollaborator };
121 let isReadOnlyMode = checkReadOnly(itemPermission);
122 let props = { ...state.props, isReadOnlyMode };
talig8e9c0652017-12-20 14:30:43 +0200123
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200124 return {
125 ...state,
126 itemPermission,
127 props
128 };
129 }
talig8e9c0652017-12-20 14:30:43 +0200130
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200131 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 };
talig8e9c0652017-12-20 14:30:43 +0200158
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200159 return {
160 ...state,
161 itemPermission,
162 props
163 };
164 }
talig8e9c0652017-12-20 14:30:43 +0200165
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200166 default:
167 return state;
168 }
Michael Landoefa037d2017-02-19 12:57:33 +0200169};
170
talig8e9c0652017-12-20 14:30:43 +0200171export default currentScreen;