1
1
import React from "react" ;
2
+ import axios from "axios" ;
2
3
import GroupsSideMenu from "../../components/GroupsSideMenu" ;
3
4
import ProfileCardGroupWrapper from "../../components/ProfileCardGroupWrapper" ;
4
5
import Pagination from "../../components/Pagination" ;
@@ -10,6 +11,7 @@ import api from "../../services/api";
10
11
import * as groupLib from "../../lib/groups" ;
11
12
12
13
import style from "./style.module.scss" ;
14
+ import * as OrgService from "../../services/user-org" ;
13
15
14
16
const colorIterator = makeColorIterator ( avatarColors ) ;
15
17
@@ -33,8 +35,171 @@ export default function SearchGroups() {
33
35
let isSubscribed = true ;
34
36
setLoadingGroups ( true ) ;
35
37
38
+ const source = axios . CancelToken . source ( ) ;
36
39
( 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
38
203
39
204
if ( isSubscribed ) {
40
205
setMyGroups ( groups . myGroups ) ;
@@ -43,7 +208,10 @@ export default function SearchGroups() {
43
208
}
44
209
} ) ( ) ;
45
210
46
- return ( ) => ( isSubscribed = false ) ;
211
+ return ( ) => {
212
+ isSubscribed = false ;
213
+ source . cancel ( "Cancelling for cleanup" ) ;
214
+ } ;
47
215
// eslint-disable-next-line react-hooks/exhaustive-deps
48
216
} , [ ] ) ;
49
217
0 commit comments