Skip to content

Commit cd80771

Browse files
committed
fix(challenge-listing): my challenges bucket/filter
Drop out the old `resource-api` call to get user challenges. Use `GET /challenges?memberId=<userId>&status=Active` instead. Add `challenge.users[userId]` property for user challenges. Remove `userChallenges` array from filter, use `challenge.users[userId]` instead. Addresses #4782
1 parent f8c7b4c commit cd80771

File tree

7 files changed

+39
-55
lines changed

7 files changed

+39
-55
lines changed

src/shared/actions/challenge-listing/index.js

+11-25
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createActions } from 'redux-actions';
77
import { decodeToken } from 'tc-accounts';
88
import 'isomorphic-fetch';
99
import { processSRM } from 'utils/tc';
10+
import { mapUserWithChallenges } from 'utils/challenge-listing/helper';
1011
import { errors, services } from 'topcoder-react-lib';
1112

1213
const { fireErrorMessage } = errors;
@@ -104,27 +105,12 @@ function getAllActiveChallengesWithUsersDone(uuid, tokenV3, filter, page = 0) {
104105
calls.push(getAll(params => service.getUserChallenges(user, newFilter, params)
105106
.catch(() => ({ challenges: [] }))), page);
106107
}
107-
return Promise.all(calls).then(([ch, uch]) => {
108-
/* uch array contains challenges where the user is participating in
109-
@@ -111,8 +124,8 @@ function getAllActiveChallengesDone(uuid, tokenV3) {
110-
* challenges in an efficient way. */
111-
if (uch) {
112-
const map = {};
113-
uch.forEach((item) => { map[item.id] = item; });
114-
ch.forEach((item) => {
115-
if (map[item.id]) {
116-
/* It is fine to reassing, as the array we modifying is created just
117-
* above within the same function. */
118-
/* eslint-disable no-param-reassign */
119-
item.users[user] = true;
120-
item.userDetails = map[item.id].userDetails;
121-
/* eslint-enable no-param-reassign */
122-
}
123-
});
124-
}
125-
126-
return { uuid, challenges: ch, ...filter };
127-
});
108+
// uch array contains challenges where the user is participating in
109+
return Promise.all(calls).then(([ch, uch]) => ({
110+
uuid,
111+
challenges: mapUserWithChallenges(user, ch, uch),
112+
...filter,
113+
}));
128114
}
129115

130116
/** TODO: Inspect if the 2 actions bellow can be removed?
@@ -182,12 +168,12 @@ function getActiveChallengesDone(uuid, page, backendFilter, tokenV3, frontFilter
182168

183169
// Handle any errors on this endpoint so that the non-user specific challenges
184170
// will still be loaded.
185-
calls.push(service.getUserChallenges(user, filter, {})
186-
.catch(() => ({ challenges: [] })));
171+
calls.push(getAll(params => service.getUserChallenges(user, filter, params)
172+
.catch(() => ({ challenges: [] }))));
187173
}
188-
return Promise.all(calls).then(([ch]) => ({
174+
return Promise.all(calls).then(([ch, uch]) => ({
189175
uuid,
190-
challenges: ch.challenges,
176+
challenges: mapUserWithChallenges(user, ch.challenges, uch),
191177
meta: ch.meta,
192178
frontFilter,
193179
}));

src/shared/components/challenge-listing/Listing/index.jsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ function Listing({
2121
auth,
2222
challenges,
2323
challengeTypes,
24-
userChallenges,
2524
challengesUrl,
2625
communityName,
2726
extraBucket,
@@ -47,7 +46,7 @@ function Listing({
4746
pastSearchTimestamp,
4847
isLoggedIn,
4948
}) {
50-
const buckets = getBuckets(userChallenges);
49+
const buckets = getBuckets(_.get(auth, 'user.userId'));
5150
const isChallengesAvailable = (bucket) => {
5251
const filter = Filter.getFilterFunction(buckets[bucket].filter);
5352
const clonedChallenges = _.clone(challenges);
@@ -180,7 +179,6 @@ Listing.defaultProps = {
180179
// onExpandFilterResult: _.noop,
181180
openChallengesInNewTabs: false,
182181
pastSearchTimestamp: 0,
183-
userChallenges: [],
184182
};
185183

186184
Listing.propTypes = {
@@ -216,7 +214,6 @@ Listing.propTypes = {
216214
setSort: PT.func.isRequired,
217215
sorts: PT.shape().isRequired,
218216
pastSearchTimestamp: PT.number,
219-
userChallenges: PT.arrayOf(PT.string),
220217
isLoggedIn: PT.bool.isRequired,
221218
};
222219

src/shared/components/challenge-listing/index.jsx

-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ export default function ChallengeListing(props) {
115115
sorts={props.sorts}
116116
loadMoreActive={props.loadMoreActive}
117117
loadingActiveChallenges={props.loadingChallenges}
118-
userChallenges={props.userChallenges}
119118
isLoggedIn={isLoggedIn}
120119
/>
121120
);
@@ -170,7 +169,6 @@ ChallengeListing.defaultProps = {
170169
expandTag: null,
171170
loadMoreActive: null,
172171
isBucketSwitching: false,
173-
userChallenges: [],
174172
};
175173

176174
ChallengeListing.propTypes = {
@@ -207,6 +205,5 @@ ChallengeListing.propTypes = {
207205
auth: PT.shape(),
208206
loadMoreActive: PT.func,
209207
isBucketSwitching: PT.bool,
210-
userChallenges: PT.arrayOf(PT.string),
211208
isLoggedIn: PT.bool.isRequired,
212209
};

src/shared/containers/challenge-listing/Listing/index.jsx

-16
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ export class ListingContainer extends React.Component {
149149
auth,
150150
getActiveChallenges,
151151
lastRequestedPageOfActiveChallenges,
152-
getUserChallenges,
153152
} = this.props;
154153
const f = this.getBackendFilter();
155154
getActiveChallenges(
@@ -158,10 +157,6 @@ export class ListingContainer extends React.Component {
158157
auth.tokenV3,
159158
f.front,
160159
);
161-
if (auth.tokenV3) {
162-
const userId = _.get(auth.user, 'userId');
163-
getUserChallenges(userId, auth.tokenV3);
164-
}
165160
}
166161

167162
render() {
@@ -209,7 +204,6 @@ export class ListingContainer extends React.Component {
209204
sorts,
210205
hideTcLinksInSidebarFooter,
211206
isBucketSwitching,
212-
userChallenges,
213207
} = this.props;
214208

215209
const { tokenV3 } = auth;
@@ -312,7 +306,6 @@ export class ListingContainer extends React.Component {
312306
groupIds={groupIds}
313307
auth={auth}
314308
isBucketSwitching={isBucketSwitching}
315-
userChallenges={userChallenges}
316309
isLoggedIn={isLoggedIn}
317310
/>
318311
</div>
@@ -340,7 +333,6 @@ ListingContainer.defaultProps = {
340333
queryBucket: BUCKETS.ALL,
341334
meta: {},
342335
isBucketSwitching: false,
343-
userChallenges: [],
344336
};
345337

346338
ListingContainer.propTypes = {
@@ -410,8 +402,6 @@ ListingContainer.propTypes = {
410402
meta: PT.shape(),
411403
isBucketSwitching: PT.bool,
412404
selectBucketDone: PT.func.isRequired,
413-
userChallenges: PT.arrayOf(PT.string),
414-
getUserChallenges: PT.func.isRequired,
415405
};
416406

417407
const mapStateToProps = (state, ownProps) => {
@@ -455,7 +445,6 @@ const mapStateToProps = (state, ownProps) => {
455445
isBucketSwitching: cl.sidebar.isBucketSwitching,
456446
expandedTags: cl.expandedTags,
457447
meta: cl.meta,
458-
userChallenges: cl.userChallenges,
459448
};
460449
};
461450

@@ -502,11 +491,6 @@ function mapDispatchToProps(dispatch) {
502491
setSort: (bucket, sort) => dispatch(a.setSort(bucket, sort)),
503492
markHeaderMenu: () => dispatch(ah.setCurrentNav('Compete', 'All Challenges')),
504493
expandTag: id => dispatch(a.expandTag(id)),
505-
getUserChallenges: (userId, tokenV3) => {
506-
const uuid = shortId();
507-
dispatch(a.getUserChallengesInit(uuid));
508-
dispatch(a.getUserChallengesDone(userId, tokenV3));
509-
},
510494
};
511495
}
512496

src/shared/containers/challenge-listing/Sidebar.jsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ export class SidebarContainer extends React.Component {
5757
setFilter,
5858
setSearchText,
5959
tokenV2,
60+
user,
6061
updateAllSavedFilters,
6162
updateSavedFilter,
62-
userChallenges,
6363
} = this.props;
6464

65-
const buckets = getBuckets(userChallenges);
65+
const buckets = getBuckets(_.get(user, 'userId', null));
6666

6767
if (extraBucket) {
6868
buckets[extraBucket.name] = extraBucket;
@@ -115,7 +115,6 @@ SidebarContainer.defaultProps = {
115115
selectedCommunityId: '',
116116
tokenV2: null,
117117
user: null,
118-
userChallenges: [],
119118
};
120119

121120
SidebarContainer.propTypes = {
@@ -136,7 +135,6 @@ SidebarContainer.propTypes = {
136135
updateAllSavedFilters: PT.func.isRequired,
137136
updateSavedFilter: PT.func.isRequired,
138137
user: PT.shape(),
139-
userChallenges: PT.arrayOf(PT.string),
140138
};
141139

142140
function mapDispatchToProps(dispatch) {
@@ -169,7 +167,6 @@ function mapStateToProps(state, ownProps) {
169167
selectedCommunityId: state.challengeListing.selectedCommunityId,
170168
tokenV2: state.auth.tokenV2,
171169
user: state.auth.user,
172-
userChallenges: state.challengeListing.userChallenges,
173170
};
174171
}
175172

src/shared/utils/challenge-listing/buckets.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ export const NO_LIVE_CHALLENGES_CONFIG = {
134134
* @param {String} userId id of the authenticated
135135
* user to filter out My Challenges.
136136
*/
137-
export function getBuckets(userChallenges) {
137+
export function getBuckets(userId) {
138138
const res = _.cloneDeep(BUCKET_DATA);
139-
res[BUCKETS.MY].filter.userChallenges = userChallenges;
139+
res[BUCKETS.MY].filter.userId = userId;
140140
return res;
141141
}
142142

src/shared/utils/challenge-listing/helper.js

+23
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,26 @@ export function phaseStartDate(phase) {
2929
// For all other cases, take the `actualStartDate` as phase is already started
3030
return new Date(phase.actualStartDate);
3131
}
32+
33+
/**
34+
* Map user details with challenges
35+
*
36+
* @param {String} userId user id
37+
* @param {Array} challenges all challenges
38+
* @param {Array} userChallenges challenges user is participating in
39+
*/
40+
export function mapUserWithChallenges(userId, challenges, userChallenges) {
41+
if (userChallenges) {
42+
const map = {};
43+
userChallenges.forEach((item) => { map[item.id] = item; });
44+
challenges.forEach((item) => {
45+
if (map[item.id]) {
46+
/* eslint-disable no-param-reassign */
47+
item.users[userId] = true;
48+
item.userDetails = map[item.id].userDetails;
49+
/* eslint-enable no-param-reassign */
50+
}
51+
});
52+
}
53+
return challenges;
54+
}

0 commit comments

Comments
 (0)