Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-braun committed Jan 11, 2018
2 parents 2d89f7c + fd4a975 commit bb4c8b1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chayns-components",
"version": "2.0.0",
"version": "2.0.1",
"description": "some standalone react components",
"main": "lib/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/react-chayns-modeswitch/component/ModeSwitch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ModeSwitchHelper from './ModeSwitchHelper';
class ModeSwitch extends React.Component {
static propTypes = {
groups: PropTypes.arrayOf(
PropTypes.oneOf([PropTypes.number, PropTypes.object])
PropTypes.oneOfType([PropTypes.number, PropTypes.object])
),
save: PropTypes.bool,
onChange: PropTypes.func,
Expand Down
122 changes: 70 additions & 52 deletions src/react-chayns-modeswitch/component/ModeSwitchHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ function callCallbacks(data) {
}

function getChangeListener() {
return function (data) {
return (data) => {
callCallbacks(data);

currentMode = data;
};
}

function setDefaultGroup() {
function setDefaultGroup(mode = 0) {
initialized = true;

getChangeListener()({
id: 0
id: mode
});

window.chayns.ui.modeSwitch.changeMode(0);
Expand Down Expand Up @@ -92,6 +92,7 @@ export default class ModeSwitchHelper {
}

const allowedGroups = [];
let isChaynsIdAdmin = false;

const groups = [];
if (options.groups) {
Expand All @@ -107,81 +108,88 @@ export default class ModeSwitchHelper {
});
}

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

const managerGroup = ModeSwitchHelper.findManagerGroup(groups);

let savedModeId = null;
let changeGroupIndex = 0;
if(options.save) {
savedModeId = getSavedMode();
}
if(managerGroup && data && data.AppUser.AdminMode) {
groupObject = getGroupObject(managerGroup.id, managerGroup.name, managerGroup.uacIds);
isChaynsIdAdmin = true;
} else {
groupObject = getGroupObject(0, window.chayns.env.user.name, [0]);
groupObject.default = true;
}

if(savedModeId === null && options.defaultMode) {
savedModeId = options.defaultMode;
}
allowedGroups.push(groupObject);

let changeGroup = false;
let changeGroupValue = null;

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

if (addGroupObject.id === savedModeId) {
changeGroup = true;
changeGroupIndex = allowedGroups.length - 1;
changeGroupValue = addGroupObject;
}
} else {
const uacIds = getUacIds(groups[i]);
const allowedUacs = getAllowedUacIdsFromArray(uacIds);
if(savedModeId === null && options.defaultMode) {
savedModeId = options.defaultMode;
}

let changeGroup = false;
let changeGroupValue = null;

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

if (addGroupObject.id === savedModeId) {
changeGroup = true;
changeGroupIndex = allowedGroups.length - 1;
changeGroupValue = addGroupObject;
}
} else {
const uacIds = getUacIds(groups[i]);
const allowedUacs = getAllowedUacIdsFromArray(uacIds);

if (allowedUacs.length > 0 && !(allowedUacs.find(uac => uac === 1))) {
const addGroupObject = getGroupObject(groups[i].id, groups[i].name, allowedUacs);
allowedGroups.push(addGroupObject);

if (addGroupObject.id === savedModeId) {
changeGroup = true;
changeGroupIndex = allowedGroups.length - 1;
changeGroupValue = addGroupObject;
}
}
}
}
}

if (allowedGroups.length > 1) {
window.chayns.ui.modeSwitch.init({
items: allowedGroups,
callback: getChangeListener()
});
if (allowedGroups.length > 1) {
window.chayns.ui.modeSwitch.init({
items: allowedGroups,
callback: getChangeListener()
});

initialized = true;

initialized = true;
if (changeGroup) {
getChangeListener()(changeGroupValue);

if (changeGroup) {
getChangeListener()(changeGroupValue);
window.chayns.ui.modeSwitch.changeMode(changeGroupIndex);
} else {
setDefaultGroup(isChaynsIdAdmin && managerGroup ? managerGroup.id : 0);
}

window.chayns.ui.modeSwitch.changeMode(changeGroupIndex);
// if (changeGroup) { window.setTimeout(() => { window.chayns.ui.modeSwitch.changeMode(changeGroupIndex); }, 0); }
} else {
setDefaultGroup();
}


if (changeGroup) {
window.setTimeout(() => {
window.chayns.ui.modeSwitch.changeMode(changeGroupIndex);
}, 0);
setDefaultGroup(isChaynsIdAdmin && managerGroup ? managerGroup.id : 0);
}
} else {
setDefaultGroup();
}
} else {
setDefaultGroup();
}
});
} else {
console.warn('No groups specified');
}
Expand Down Expand Up @@ -231,6 +239,16 @@ export default class ModeSwitchHelper {
});
}

static findManagerGroup(groups) {
if(!window.chayns.env.user.isAuthenticated) return false;

return groups.find((uac) => {
return uac.uacIds && uac.uacIds.length === 1 && uac.uacIds[0] === 1;
}) || groups.find((uac) => {
return uac.uacIds && uac.uacIds.find(id => id === 1);
});
}

static isChaynsManager() {
return this.isUserInGroup(1);
}
Expand Down

0 comments on commit bb4c8b1

Please sign in to comment.