Skip to content

Commit bb4c8b1

Browse files
committed
Merge branch 'develop'
2 parents 2d89f7c + fd4a975 commit bb4c8b1

File tree

3 files changed

+72
-54
lines changed

3 files changed

+72
-54
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chayns-components",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "some standalone react components",
55
"main": "lib/index.js",
66
"scripts": {

src/react-chayns-modeswitch/component/ModeSwitch.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ModeSwitchHelper from './ModeSwitchHelper';
66
class ModeSwitch extends React.Component {
77
static propTypes = {
88
groups: PropTypes.arrayOf(
9-
PropTypes.oneOf([PropTypes.number, PropTypes.object])
9+
PropTypes.oneOfType([PropTypes.number, PropTypes.object])
1010
),
1111
save: PropTypes.bool,
1212
onChange: PropTypes.func,

src/react-chayns-modeswitch/component/ModeSwitchHelper.js

+70-52
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ function callCallbacks(data) {
1313
}
1414

1515
function getChangeListener() {
16-
return function (data) {
16+
return (data) => {
1717
callCallbacks(data);
1818

1919
currentMode = data;
2020
};
2121
}
2222

23-
function setDefaultGroup() {
23+
function setDefaultGroup(mode = 0) {
2424
initialized = true;
2525

2626
getChangeListener()({
27-
id: 0
27+
id: mode
2828
});
2929

3030
window.chayns.ui.modeSwitch.changeMode(0);
@@ -92,6 +92,7 @@ export default class ModeSwitchHelper {
9292
}
9393

9494
const allowedGroups = [];
95+
let isChaynsIdAdmin = false;
9596

9697
const groups = [];
9798
if (options.groups) {
@@ -107,81 +108,88 @@ export default class ModeSwitchHelper {
107108
});
108109
}
109110

110-
if (window.chayns.env.user.isAuthenticated) {
111-
const groupObject = getGroupObject(0, window.chayns.env.user.name, [0]);
112-
groupObject.default = true;
113-
allowedGroups.push(groupObject);
111+
chayns.ready.then((data) => {
112+
if (window.chayns.env.user.isAuthenticated) {
113+
// Condition if adminMode ChaynsId
114+
let groupObject;
114115

116+
const managerGroup = ModeSwitchHelper.findManagerGroup(groups);
115117

116-
let savedModeId = null;
117-
let changeGroupIndex = 0;
118-
if(options.save) {
119-
savedModeId = getSavedMode();
120-
}
118+
if(managerGroup && data && data.AppUser.AdminMode) {
119+
groupObject = getGroupObject(managerGroup.id, managerGroup.name, managerGroup.uacIds);
120+
isChaynsIdAdmin = true;
121+
} else {
122+
groupObject = getGroupObject(0, window.chayns.env.user.name, [0]);
123+
groupObject.default = true;
124+
}
121125

122-
if(savedModeId === null && options.defaultMode) {
123-
savedModeId = options.defaultMode;
124-
}
126+
allowedGroups.push(groupObject);
125127

126-
let changeGroup = false;
127-
let changeGroupValue = null;
128128

129-
for (let i = 0, x = groups.length; i < x; i += 1) {
130-
if (!groups[i].uacId && !groups[i].uacIds) {
131-
const addGroupObject = getGroupObject(groups[i].id, groups[i].name, [0]);
132-
allowedGroups.push(addGroupObject);
129+
let savedModeId = null;
130+
let changeGroupIndex = 0;
131+
if(options.save) {
132+
savedModeId = getSavedMode();
133+
}
133134

134-
if (addGroupObject.id === savedModeId) {
135-
changeGroup = true;
136-
changeGroupIndex = allowedGroups.length - 1;
137-
changeGroupValue = addGroupObject;
138-
}
139-
} else {
140-
const uacIds = getUacIds(groups[i]);
141-
const allowedUacs = getAllowedUacIdsFromArray(uacIds);
135+
if(savedModeId === null && options.defaultMode) {
136+
savedModeId = options.defaultMode;
137+
}
138+
139+
let changeGroup = false;
140+
let changeGroupValue = null;
142141

143-
if (allowedUacs.length > 0) {
144-
const addGroupObject = getGroupObject(groups[i].id, groups[i].name, allowedUacs);
142+
for (let i = 0, x = groups.length; i < x; i += 1) {
143+
if (!groups[i].uacId && !groups[i].uacIds) {
144+
const addGroupObject = getGroupObject(groups[i].id, groups[i].name, [0]);
145145
allowedGroups.push(addGroupObject);
146146

147147
if (addGroupObject.id === savedModeId) {
148148
changeGroup = true;
149149
changeGroupIndex = allowedGroups.length - 1;
150150
changeGroupValue = addGroupObject;
151151
}
152+
} else {
153+
const uacIds = getUacIds(groups[i]);
154+
const allowedUacs = getAllowedUacIdsFromArray(uacIds);
155+
156+
if (allowedUacs.length > 0 && !(allowedUacs.find(uac => uac === 1))) {
157+
const addGroupObject = getGroupObject(groups[i].id, groups[i].name, allowedUacs);
158+
allowedGroups.push(addGroupObject);
159+
160+
if (addGroupObject.id === savedModeId) {
161+
changeGroup = true;
162+
changeGroupIndex = allowedGroups.length - 1;
163+
changeGroupValue = addGroupObject;
164+
}
165+
}
152166
}
153167
}
154-
}
155168

156-
if (allowedGroups.length > 1) {
157-
window.chayns.ui.modeSwitch.init({
158-
items: allowedGroups,
159-
callback: getChangeListener()
160-
});
169+
if (allowedGroups.length > 1) {
170+
window.chayns.ui.modeSwitch.init({
171+
items: allowedGroups,
172+
callback: getChangeListener()
173+
});
161174

175+
initialized = true;
162176

163-
initialized = true;
177+
if (changeGroup) {
178+
getChangeListener()(changeGroupValue);
164179

165-
if (changeGroup) {
166-
getChangeListener()(changeGroupValue);
180+
window.chayns.ui.modeSwitch.changeMode(changeGroupIndex);
181+
} else {
182+
setDefaultGroup(isChaynsIdAdmin && managerGroup ? managerGroup.id : 0);
183+
}
167184

168-
window.chayns.ui.modeSwitch.changeMode(changeGroupIndex);
185+
// if (changeGroup) { window.setTimeout(() => { window.chayns.ui.modeSwitch.changeMode(changeGroupIndex); }, 0); }
169186
} else {
170-
setDefaultGroup();
171-
}
172-
173-
174-
if (changeGroup) {
175-
window.setTimeout(() => {
176-
window.chayns.ui.modeSwitch.changeMode(changeGroupIndex);
177-
}, 0);
187+
setDefaultGroup(isChaynsIdAdmin && managerGroup ? managerGroup.id : 0);
178188
}
179189
} else {
180190
setDefaultGroup();
181191
}
182-
} else {
183-
setDefaultGroup();
184-
}
192+
});
185193
} else {
186194
console.warn('No groups specified');
187195
}
@@ -231,6 +239,16 @@ export default class ModeSwitchHelper {
231239
});
232240
}
233241

242+
static findManagerGroup(groups) {
243+
if(!window.chayns.env.user.isAuthenticated) return false;
244+
245+
return groups.find((uac) => {
246+
return uac.uacIds && uac.uacIds.length === 1 && uac.uacIds[0] === 1;
247+
}) || groups.find((uac) => {
248+
return uac.uacIds && uac.uacIds.find(id => id === 1);
249+
});
250+
}
251+
234252
static isChaynsManager() {
235253
return this.isUserInGroup(1);
236254
}

0 commit comments

Comments
 (0)