Skip to content

Commit 7c762d8

Browse files
committed
Fix: correct counting of sub-community members
1 parent 52fbf1c commit 7c762d8

File tree

2 files changed

+14
-49
lines changed

2 files changed

+14
-49
lines changed

src/shared/actions/stats.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { getService as getGroupService } from 'services/groups';
1717
* related to private groups.
1818
* @return {Promise} Resolves to the loaded data.
1919
*/
20+
/* TODO: This code should be moved to a dedicated service. */
2021
function getCommunityStats(community, challenges, token) {
2122
/* TODO: At the moment, this component loads challenge objects to calculate
2223
* the number of challenges and the total prize. Probably in future, we'll
@@ -33,13 +34,18 @@ function getCommunityStats(community, challenges, token) {
3334
},
3435
};
3536
if (community.groupIds && community.groupIds.length) {
36-
/* TODO: Should be properly updated to count members from all groups
37-
* without double-counting. */
38-
return groupService.getMembers(community.groupIds[0])
39-
.then((members) => {
40-
result.stats.numMembers = members.length;
41-
return result;
42-
}).catch(() => result);
37+
const members = new Set();
38+
return Promise.all(
39+
community.groupIds.map(id =>
40+
groupService.getMembers(id)
41+
.then(res => res.forEach((member) => {
42+
if (member.membershipType === 'user') members.add(member);
43+
})).catch(),
44+
),
45+
).then(() => {
46+
result.stats.numMembers = members.size;
47+
return result;
48+
});
4349
}
4450
return result;
4551
}

src/shared/reducers/groups.js

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919

2020
import _ from 'lodash';
2121
import actions from 'actions/groups';
22-
import { getService as getCommunitiesService } from 'services/communities';
2322
import { handleActions } from 'redux-actions';
24-
import { getCommunityId } from 'routes/subdomains';
25-
import { toFSA } from 'utils/redux';
26-
import { getAuthTokens } from 'utils/tc';
2723

2824
/**
2925
* Private. Given two user group maps, it adds to "dst" the root group from
@@ -102,50 +98,13 @@ function create(state) {
10298
}));
10399
}
104100

105-
/**
106-
* Loads into the state detailed information on the groups related to the
107-
* specified community.
108-
*
109-
* NOTE: This function is intended for the internal use only, it modifies
110-
* "state" argument!
111-
*
112-
* @param {String} communityId
113-
* @param {String} tokenV3
114-
* @param {Object} state
115-
* @return {Promise} Resolves to the resulting state.
116-
*/
117-
function loadCommunityGroups(communityId, tokenV3, state) {
118-
let res = _.defaults(state, { groups: {}, loading: {} });
119-
return getCommunitiesService(tokenV3).getMetadata(communityId).then((data) => {
120-
let ids = data.authorizedGroupIds || [];
121-
if (data.groupIds) ids = ids.concat(data.groupIds);
122-
res = onGetGroupsInit(res, { payload: ids });
123-
return toFSA(actions.groups.getGroupsDone(ids, tokenV3))
124-
.then((action) => { res = onGetGroupsDone(res, action); })
125-
.then(() => res);
126-
});
127-
}
128-
129101
/**
130102
* Reducer factory.
131103
* @param {Object} req Optional. ExpressJS HTTP request. If provided, the
132104
* intial state of the reducer will be tailored to the request.
133105
* @return {Promise} Resolves to the reducer.
134106
*/
135-
export function factory(req) {
136-
if (req) {
137-
/* For any location within any TC community we should load detailed
138-
* information about any related user groups. */
139-
let communityId = getCommunityId(req.subdomains);
140-
if (!communityId && req.url.startsWith('/community')) {
141-
communityId = req.url.split('/')[2];
142-
}
143-
if (communityId) {
144-
const tokenV3 = getAuthTokens(req).tokenV3;
145-
return loadCommunityGroups(communityId, tokenV3, {})
146-
.then(res => create(res));
147-
}
148-
}
107+
export function factory(/* req */) {
149108
return Promise.resolve(create());
150109
}
151110

0 commit comments

Comments
 (0)