Add collaboration feature
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / OnboardingReducers.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
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';
22
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
35         switch (action.type) {
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
67                 case actionTypes.SET_CURRENT_SCREEN_VERSION:
68                         return {
69                                 ...state,
70                                 props: {
71                                         ...state.props,
72                                         version: action.version,
73                                         isReadOnlyMode: checkReadOnly(state.itemPermission)
74                                 }
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
133                 default:
134                         return state;
135
136         }
137
138 };
139
140 export default currentScreen;