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

Commit 3baddbf

Browse files
Merge branch 'issues/timor/89' into develop
2 parents 6cf9adb + a51087c commit 3baddbf

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

client/src/components/AddToGroupModal/index.jsx

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { useEffect } from "react";
22
import PT from "prop-types";
33

44
import Button from "../Button";
@@ -10,6 +10,7 @@ import { useAuth0 } from "../../react-auth0-spa";
1010
import * as groupLib from "../../lib/groups";
1111

1212
import style from "./style.module.scss";
13+
import Axios from "axios";
1314

1415
export default function AddToGroupModal({ onCancel, updateUser, user }) {
1516
const apiClient = api();
@@ -21,34 +22,50 @@ export default function AddToGroupModal({ onCancel, updateUser, user }) {
2122
const [updatingGroups, setUpdatingGroups] = React.useState(false);
2223
const [userGroups, setUserGroups] = React.useState(user.groups);
2324
const [creatingGroup, setCreatingGroup] = React.useState(false);
25+
const cancelTokenSource = Axios.CancelToken.source();
26+
27+
/**
28+
* Component unmount trigger
29+
*/
30+
useEffect(() => {
31+
return () => {
32+
cancelTokenSource.cancel();
33+
};
34+
});
2435

2536
React.useEffect(() => {
2637
if (isLoading || !isAuthenticated) {
2738
return;
2839
}
2940

3041
(async () => {
31-
const groups = await groupLib.getGroups(apiClient, auth0User.nickname);
32-
33-
groups.myGroups.forEach((g, i, a) => {
34-
userGroups.forEach((ug, iug, aug) => {
35-
if (g.id === ug.id && !ug.isDeleted) {
36-
a[i] = { ...g, isSelected: true };
37-
}
42+
const groups = await groupLib.getGroups(
43+
apiClient,
44+
auth0User.nickname,
45+
cancelTokenSource.token
46+
);
47+
48+
if (groups) {
49+
groups.myGroups.forEach((g, i, a) => {
50+
userGroups.forEach((ug, iug, aug) => {
51+
if (g.id === ug.id && !ug.isDeleted) {
52+
a[i] = { ...g, isSelected: true };
53+
}
54+
});
3855
});
39-
});
4056

41-
groups.otherGroups.forEach((g, i, a) => {
42-
userGroups.forEach((ug, iug, aug) => {
43-
if (g.id === ug.id && !ug.isDeleted) {
44-
a[i] = { ...g, isSelected: true };
45-
}
57+
groups.otherGroups.forEach((g, i, a) => {
58+
userGroups.forEach((ug, iug, aug) => {
59+
if (g.id === ug.id && !ug.isDeleted) {
60+
a[i] = { ...g, isSelected: true };
61+
}
62+
});
4663
});
47-
});
4864

49-
setMyGroups(groups.myGroups);
50-
setOtherGroups(groups.otherGroups);
51-
setIsLoadingGroups(false);
65+
setMyGroups(groups.myGroups);
66+
setOtherGroups(groups.otherGroups);
67+
setIsLoadingGroups(false);
68+
}
5269
})();
5370
// eslint-disable-next-line react-hooks/exhaustive-deps
5471
}, [isLoading, isAuthenticated, auth0User]);

client/src/lib/groups.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import config from "../config";
22
import * as OrgService from "../services/user-org";
3+
import Axios from "axios";
34

45
/**
56
* Returns the groups for the logged in user
67
* @param {Object} apiClient The api client
78
* @param {Object} handle The logged in user's handle
89
*/
9-
export async function getGroups(apiClient, handle) {
10+
export async function getGroups(apiClient, handle, cancelToken) {
1011
let myGroups = [];
1112
let otherGroups = [];
1213
let response;
1314

1415
// First, we get the userId of the current user
1516
try {
16-
response = await apiClient.get(`${config.API_URL}/users?handle=${handle}`);
17+
response = await apiClient.get(`${config.API_URL}/users?handle=${handle}`, {
18+
cancelToken,
19+
});
1720
} catch (error) {
21+
if (Axios.isCancel(error)) {
22+
return undefined;
23+
}
1824
console.log(error);
1925
// TODO - handle error
2026
return { myGroups, otherGroups };
@@ -33,9 +39,13 @@ export async function getGroups(apiClient, handle) {
3339
// Now, get my groups first
3440
try {
3541
response = await apiClient.get(
36-
`${config.GROUPS_API_URL}?universalUID=${userId}&membershipType=user`
42+
`${config.GROUPS_API_URL}?universalUID=${userId}&membershipType=user`,
43+
{ cancelToken }
3744
);
3845
} catch (error) {
46+
if (Axios.isCancel(error)) {
47+
return undefined;
48+
}
3949
console.log(error);
4050
// TODO - handle error
4151
return { myGroups, otherGroups };
@@ -56,9 +66,13 @@ export async function getGroups(apiClient, handle) {
5666
// Fetch all groups in the org
5767
try {
5868
response = await apiClient.get(
59-
`${config.GROUPS_API_URL}?organizationId=${organizationId}`
69+
`${config.GROUPS_API_URL}?organizationId=${organizationId}`,
70+
{ cancelToken }
6071
);
6172
} catch (error) {
73+
if (Axios.isCancel(error)) {
74+
return undefined;
75+
}
6276
console.log(error);
6377
// TODO - handle error
6478
return { myGroups, otherGroups };
@@ -86,9 +100,13 @@ export async function getGroups(apiClient, handle) {
86100
// Now, get member counts
87101
try {
88102
response = await apiClient.get(
89-
`${config.GROUPS_API_URL}/memberGroups/groupMembersCount?universalUID=${userId}`
103+
`${config.GROUPS_API_URL}/memberGroups/groupMembersCount?universalUID=${userId}`,
104+
{ cancelToken }
90105
);
91106
} catch (error) {
107+
if (Axios.isCancel(error)) {
108+
return undefined;
109+
}
92110
console.log(error);
93111
// TODO - handle error
94112
return { myGroups, otherGroups };
@@ -105,9 +123,13 @@ export async function getGroups(apiClient, handle) {
105123
// Fetch all groups in the org
106124
try {
107125
response = await apiClient.get(
108-
`${config.GROUPS_API_URL}/memberGroups/groupMembersCount?organizationId=${organizationId}`
126+
`${config.GROUPS_API_URL}/memberGroups/groupMembersCount?organizationId=${organizationId}`,
127+
{ cancelToken }
109128
);
110129
} catch (error) {
130+
if (Axios.isCancel(error)) {
131+
return undefined;
132+
}
111133
console.log(error);
112134
// TODO - handle error
113135
return { myGroups, otherGroups };

0 commit comments

Comments
 (0)