Skip to content

Commit 68cbbd5

Browse files
committed
fetch users and refactoring
1 parent 29223c6 commit 68cbbd5

File tree

3 files changed

+71
-54
lines changed

3 files changed

+71
-54
lines changed

services/static-webserver/client/source/class/osparc/dashboard/CardBase.js

Lines changed: 44 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -148,54 +148,56 @@ qx.Class.define("osparc.dashboard.CardBase", {
148148
return false;
149149
},
150150

151-
// groups -> [orgMembs, orgs, [productEveryone], [everyone]];
152-
setIconAndTooltip: function(shareIcon, accessRights, groups) {
153-
shareIcon.setSource(osparc.dashboard.CardBase.SHARE_ICON);
154-
if (osparc.data.model.Study.canIWrite(accessRights)) {
155-
shareIcon.set({
156-
toolTipText: qx.locale.Manager.tr("Share")
157-
});
151+
populateShareIcon: async function(shareIcon, accessRights) {
152+
const gids = Object.keys(accessRights).map(key => parseInt(key));
153+
154+
const groupsStore = osparc.store.Groups.getInstance();
155+
156+
// Icon
157+
const groupEveryone = groupsStore.getEveryoneGroup();
158+
const groupProductEveryone = groupsStore.getEveryoneProductGroup();
159+
const organizations = groupsStore.getOrganizations();
160+
const organizationIds = Object.keys(organizations).map(key => parseInt(key));
161+
if (gids.includes(groupEveryone.getGroupId()) || gids.includes(groupProductEveryone.getGroupId())) {
162+
shareIcon.setSource(osparc.dashboard.CardBase.SHARED_ALL);
163+
} else if (organizationIds.filter(value => gids.includes(value)).length) { // find intersection
164+
shareIcon.setSource(osparc.dashboard.CardBase.SHARED_ORGS);
165+
} else if (gids.length === 1) {
166+
shareIcon.setSource(osparc.dashboard.CardBase.SHARE_ICON);
167+
} else {
168+
shareIcon.setSource(osparc.dashboard.CardBase.SHARED_USER);
158169
}
159-
let sharedGrps = [];
160-
const myGroupId = osparc.auth.Data.getInstance().getGroupId();
161-
for (let i=0; i<groups.length; i++) {
162-
if (groups[i].length === 0) {
163-
// user has no read access to the productEveryone
164-
continue;
170+
171+
// Tooltip
172+
const sharedGrps = [];
173+
const groups = [];
174+
groups.push(groupEveryone);
175+
groups.push(groupProductEveryone);
176+
groups.push(...Object.values(organizations));
177+
groups.forEach(group => {
178+
const idx = gids.indexOf(group.getGroupId());
179+
if (idx > -1) {
180+
sharedGrps.push(group);
181+
gids.splice(idx, 1);
165182
}
166-
const sharedGrp = [];
167-
const gids = Object.keys(accessRights);
168-
for (let j=0; j<gids.length; j++) {
169-
const gid = parseInt(gids[j]);
170-
if (gid === myGroupId) {
171-
continue;
172-
}
173-
const grp = groups[i].find(group => group.getGroupId() === gid);
174-
if (grp) {
175-
sharedGrp.push(grp);
183+
});
184+
// once the groups were removed, the remaining group ids are users' primary groups ids
185+
const usersStore = osparc.store.Users.getInstance();
186+
const myGroupId = osparc.auth.Data.getInstance().getGroupId();
187+
for (let i=0; i<gids.length; i++) {
188+
const gid = gids[i];
189+
if (myGroupId !== gid) {
190+
const user = await usersStore.getUser(gid);
191+
if (user) {
192+
sharedGrps.push(user);
176193
}
177194
}
178-
if (sharedGrp.length === 0) {
179-
continue;
180-
} else {
181-
sharedGrps = sharedGrps.concat(sharedGrp);
182-
}
183-
switch (i) {
184-
case 0:
185-
shareIcon.setSource(osparc.dashboard.CardBase.SHARED_USER);
186-
break;
187-
case 1:
188-
shareIcon.setSource(osparc.dashboard.CardBase.SHARED_ORGS);
189-
break;
190-
case 2:
191-
case 3:
192-
shareIcon.setSource(osparc.dashboard.CardBase.SHARED_ALL);
193-
break;
194-
}
195195
}
196196

197-
// tooltip
198-
if (sharedGrps.length === 0) {
197+
if (sharedGrps.length === 0 && osparc.data.model.Study.canIWrite(accessRights)) {
198+
shareIcon.set({
199+
toolTipText: qx.locale.Manager.tr("Share")
200+
});
199201
return;
200202
}
201203
const sharedGrpLabels = [];
@@ -215,18 +217,6 @@ qx.Class.define("osparc.dashboard.CardBase", {
215217
shareIcon.addListener("mouseover", () => hint.show(), this);
216218
shareIcon.addListener("mouseout", () => hint.exclude(), this);
217219
},
218-
219-
// groups -> [users, orgs, [productEveryone], [everyone]];
220-
populateShareIcon: function(shareIcon, accessRights) {
221-
const groupsStore = osparc.store.Groups.getInstance();
222-
const usersStore = osparc.store.Users.getInstance();
223-
const users = usersStore.getUsers();
224-
const orgs = Object.values(groupsStore.getOrganizations());
225-
const productEveryone = [groupsStore.getEveryoneProductGroup()];
226-
const everyone = [groupsStore.getEveryoneGroup()];
227-
const groups = [users, orgs, productEveryone, everyone];
228-
osparc.dashboard.CardBase.setIconAndTooltip(shareIcon, accessRights, groups);
229-
},
230220
},
231221

232222
properties: {

services/static-webserver/client/source/class/osparc/data/Resources.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,10 @@ qx.Class.define("osparc.data.Resources", {
884884
"users": {
885885
useCache: false, // osparc.store.Groups handles the cache
886886
endpoints: {
887+
get: {
888+
method: "GET",
889+
url: statics.API + "/groups/{gid}/users"
890+
},
887891
search: {
888892
method: "POST",
889893
url: statics.API + "/users:search"

services/static-webserver/client/source/class/osparc/store/Users.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,29 @@ qx.Class.define("osparc.store.Users", {
2828
},
2929

3030
members: {
31+
fetchUser: function(groupId) {
32+
const params = {
33+
url: {
34+
gid: groupId
35+
}
36+
};
37+
return osparc.data.Resources.fetch("users", "get", params)
38+
.then(userData => {
39+
const user = this.addUser(userData[0]);
40+
return user;
41+
});
42+
},
43+
44+
getUser: function(groupId, fetchIfNotFound = true) {
45+
const userFound = this.getUsers().find(user => user.getGroupId() === groupId);
46+
if (userFound) {
47+
return new Promise(resolve => resolve(userFound));
48+
} else if (fetchIfNotFound) {
49+
return this.fetchUser(groupId);
50+
}
51+
return new Promise(reject => reject());
52+
},
53+
3154
addUser: function(userData) {
3255
const user = new osparc.data.model.User(userData);
3356
const userFound = this.getUsers().find(usr => usr.getGroupId() === user.getGroupId());

0 commit comments

Comments
 (0)