Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 1659a61

Browse files
committed
Issue #473 fixes.
1 parent ebffc77 commit 1659a61

File tree

2 files changed

+259
-4
lines changed

2 files changed

+259
-4
lines changed

client/src/pages/Search/Global.jsx

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ import api from "../../services/api";
1717
import staticData from "../../services/static-data";
1818

1919
import style from "./style.module.scss";
20+
import * as OrgService from "../../services/user-org";
21+
22+
let primaryAttributeIds = [
23+
config.STANDARD_USER_ATTRIBUTES.location,
24+
config.STANDARD_USER_ATTRIBUTES.isAvailable,
25+
config.STANDARD_USER_ATTRIBUTES.title,
26+
config.STANDARD_USER_ATTRIBUTES.company,
27+
];
2028

2129
const colorIterator = makeColorIterator(avatarColors);
2230

@@ -102,9 +110,85 @@ export default function SearchGlobal({ keyword }) {
102110
}
103111

104112
let isSubscribed = true;
113+
const source = axios.CancelToken.source();
105114

106115
(async () => {
107-
const companyAttrs = await getCompanyAttributes(apiClient);
116+
//const companyAttrs = await getCompanyAttributes(apiClient, source);
117+
// Code moved from company-attributes.js -- START
118+
// As cancel dispatch event wasn't working as expected.
119+
let response;
120+
let attributeGroups;
121+
let attributes = [];
122+
let errorMessage =
123+
"An error occurred when getting company attributes for the custom filter";
124+
const organizationId = OrgService.getSingleOrg();
125+
126+
// Get the attribute groups under the org
127+
let url = `${config.API_URL}/attributeGroups?organizationId=${organizationId}`;
128+
129+
try {
130+
response = await apiClient.get(url, {
131+
cancelToken: source.token,
132+
});
133+
} catch (error) {
134+
if (axios.isCancel(error)) {
135+
// request explicitly cancelled.
136+
console.log(error);
137+
return;
138+
}
139+
console.log(error);
140+
alert(errorMessage);
141+
// TODO - handle error
142+
return attributes;
143+
}
144+
145+
if (!response.data || response.data.length < 1) {
146+
alert(errorMessage);
147+
return attributes;
148+
}
149+
150+
attributeGroups = response.data;
151+
152+
// Now, for each attribute group, we will proceed to get the attributes
153+
for (let i = 0; i < attributeGroups.length; i++) {
154+
url = `${config.API_URL}/attributes?attributeGroupId=${attributeGroups[0].id}`;
155+
156+
try {
157+
response = await apiClient.get(url, {
158+
cancelToken: source.token,
159+
});
160+
} catch (error) {
161+
if (axios.isCancel(error)) {
162+
// request explicitly cancelled.
163+
console.log(error);
164+
return;
165+
}
166+
console.log(error);
167+
alert(errorMessage);
168+
// TODO - handle error
169+
return attributes;
170+
}
171+
172+
if (!response.data) {
173+
alert(errorMessage);
174+
return attributes;
175+
}
176+
177+
if (response.data.length > 0) {
178+
attributes = attributes.concat(response.data);
179+
}
180+
}
181+
182+
// Finally, we only need the company attributes
183+
attributes = attributes.filter((attribute) => {
184+
if (primaryAttributeIds.includes(attribute.id)) {
185+
return false;
186+
}
187+
188+
return true;
189+
});
190+
const companyAttrs = attributes;
191+
// END
108192
const filtersWithCompanyAttrs = { ...searchContext.filters };
109193
companyAttrs.forEach((companyAttr) => {
110194
filtersWithCompanyAttrs[companyAttr.id] = {
@@ -119,7 +203,10 @@ export default function SearchGlobal({ keyword }) {
119203
}
120204
})();
121205

122-
return () => (isSubscribed = false);
206+
return () => {
207+
isSubscribed = false;
208+
source.cancel("Cancelling in cleanup");
209+
};
123210
// eslint-disable-next-line react-hooks/exhaustive-deps
124211
}, [isLoading, isAuthenticated, auth0User]);
125212

client/src/pages/Search/Groups.jsx

Lines changed: 170 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from "react";
2+
import axios from "axios";
23
import GroupsSideMenu from "../../components/GroupsSideMenu";
34
import ProfileCardGroupWrapper from "../../components/ProfileCardGroupWrapper";
45
import Pagination from "../../components/Pagination";
@@ -10,6 +11,7 @@ import api from "../../services/api";
1011
import * as groupLib from "../../lib/groups";
1112

1213
import style from "./style.module.scss";
14+
import * as OrgService from "../../services/user-org";
1315

1416
const colorIterator = makeColorIterator(avatarColors);
1517

@@ -33,8 +35,171 @@ export default function SearchGroups() {
3335
let isSubscribed = true;
3436
setLoadingGroups(true);
3537

38+
const source = axios.CancelToken.source();
3639
(async () => {
37-
const groups = await groupLib.getGroups(apiClient, auth0User.nickname);
40+
//const groups = await groupLib.getGroups(apiClient, auth0User.nickname);
41+
// Moved the code from group.js START
42+
// Dispatch cancel event wasn't working as expected.
43+
const handle = auth0User.nickname;
44+
let myGroups = [];
45+
let otherGroups = [];
46+
let response;
47+
48+
// First, we get the userId of the current user
49+
try {
50+
response = await apiClient.get(
51+
`${config.API_URL}/users?handle=${handle}`,
52+
{
53+
cancelToken: source.token,
54+
}
55+
);
56+
} catch (error) {
57+
if (axios.isCancel(error)) {
58+
// request explicitly cancelled.
59+
console.log(error);
60+
return;
61+
}
62+
console.log(error);
63+
// TODO - handle error
64+
return { myGroups, otherGroups };
65+
}
66+
67+
if (!response.data || response.data.length !== 1) {
68+
alert(
69+
"Your user is not present in Ubahn. Cannot get your organization details, and thus the group details"
70+
);
71+
72+
return { myGroups, otherGroups };
73+
}
74+
75+
const userId = response.data[0].id;
76+
77+
// Now, get my groups first
78+
try {
79+
response = await apiClient.get(
80+
`${config.GROUPS_API_URL}?universalUID=${userId}&membershipType=user`,
81+
{
82+
cancelToken: source.token,
83+
}
84+
);
85+
} catch (error) {
86+
if (axios.isCancel(error)) {
87+
// request explicitly cancelled.
88+
console.log(error);
89+
return;
90+
}
91+
console.log(error);
92+
// TODO - handle error
93+
return { myGroups, otherGroups };
94+
}
95+
96+
myGroups = response.data
97+
.filter((g) => g.status === "active")
98+
.map((g) => ({
99+
...g,
100+
count: 0,
101+
}));
102+
103+
// Get other groups next
104+
// These are groups that belong to the org of the logged in user
105+
// but the user is not a part of them
106+
const organizationId = OrgService.getSingleOrg();
107+
108+
// Fetch all groups in the org
109+
try {
110+
response = await apiClient.get(
111+
`${config.GROUPS_API_URL}?organizationId=${organizationId}`,
112+
{
113+
cancelToken: source.token,
114+
}
115+
);
116+
} catch (error) {
117+
if (axios.isCancel(error)) {
118+
// request explicitly cancelled.
119+
console.log(error);
120+
return;
121+
}
122+
console.log(error);
123+
// TODO - handle error
124+
return { myGroups, otherGroups };
125+
}
126+
127+
if (!response.data || response.data.length === 0) {
128+
return { myGroups, otherGroups };
129+
}
130+
131+
otherGroups = response.data
132+
.filter((g) => g.status === "active")
133+
.filter((g) => {
134+
// Don't include groups part of my groups
135+
if (myGroups.findIndex((mygroup) => mygroup.id === g.id) === -1) {
136+
return true;
137+
}
138+
139+
return false;
140+
})
141+
.map((g) => ({
142+
...g,
143+
count: 0,
144+
}));
145+
146+
// Now, get member counts
147+
try {
148+
response = await apiClient.get(
149+
`${config.GROUPS_API_URL}/memberGroups/groupMembersCount?universalUID=${userId}`,
150+
{
151+
cancelToken: source.token,
152+
}
153+
);
154+
} catch (error) {
155+
if (axios.isCancel(error)) {
156+
// request explicitly cancelled.
157+
console.log(error);
158+
return;
159+
}
160+
console.log(error);
161+
// TODO - handle error
162+
return { myGroups, otherGroups };
163+
}
164+
165+
myGroups.forEach((m, i, arr) => {
166+
response.data.forEach((c) => {
167+
if (c.id === m.id) {
168+
arr[i].count = c.count;
169+
}
170+
});
171+
});
172+
173+
// Fetch all groups in the org
174+
try {
175+
response = await apiClient.get(
176+
`${config.GROUPS_API_URL}/memberGroups/groupMembersCount?organizationId=${organizationId}`,
177+
{
178+
cancelToken: source.token,
179+
}
180+
);
181+
} catch (error) {
182+
if (axios.isCancel(error)) {
183+
// request explicitly cancelled.
184+
console.log(error);
185+
return;
186+
}
187+
console.log(error);
188+
// TODO - handle error
189+
return { myGroups, otherGroups };
190+
}
191+
192+
otherGroups.forEach((o, i, arr) => {
193+
response.data.forEach((c) => {
194+
if (c.id === o.id) {
195+
arr[i].count = c.count;
196+
}
197+
});
198+
});
199+
200+
const groups = { myGroups, otherGroups };
201+
202+
// END
38203

39204
if (isSubscribed) {
40205
setMyGroups(groups.myGroups);
@@ -43,7 +208,10 @@ export default function SearchGroups() {
43208
}
44209
})();
45210

46-
return () => (isSubscribed = false);
211+
return () => {
212+
isSubscribed = false;
213+
source.cancel("Cancelling for cleanup");
214+
};
47215
// eslint-disable-next-line react-hooks/exhaustive-deps
48216
}, []);
49217

0 commit comments

Comments
 (0)