Skip to content

Commit 147e687

Browse files
authored
Merge pull request #4825 from cagdas001/fix-4782-workaround
fix: my challenges bucket
2 parents 221643b + 0ab0d18 commit 147e687

File tree

8 files changed

+57
-40
lines changed

8 files changed

+57
-40
lines changed

__tests__/shared/components/challenge-listing/__snapshots__/index.jsx.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ exports[`Matches shallow shapshot 1 shapshot 1 1`] = `
4343
setFilterState={[MockFunction]}
4444
setSort={[MockFunction]}
4545
sorts={Object {}}
46+
userChallenges={Array []}
4647
/>
4748
<div
4849
className="src-shared-components-challenge-listing-___style__sidebar-container-desktop___h0bz6"

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ 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';
1110
import { errors, services } from 'topcoder-react-lib';
1211

1312
const { fireErrorMessage } = errors;
@@ -105,12 +104,27 @@ function getAllActiveChallengesWithUsersDone(uuid, tokenV3, filter, page = 0) {
105104
calls.push(getAll(params => service.getUserChallenges(user, newFilter, params)
106105
.catch(() => ({ challenges: [] }))), page);
107106
}
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-
}));
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+
});
114128
}
115129

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

169183
// Handle any errors on this endpoint so that the non-user specific challenges
170184
// will still be loaded.
171-
calls.push(getAll(params => service.getUserChallenges(user, filter, params)
172-
.catch(() => ({ challenges: [] }))));
185+
calls.push(service.getUserChallenges(user, filter, {})
186+
.catch(() => ({ challenges: [] })));
173187
}
174-
return Promise.all(calls).then(([ch, uch]) => ({
188+
return Promise.all(calls).then(([ch]) => ({
175189
uuid,
176-
challenges: mapUserWithChallenges(user, ch.challenges, uch),
190+
challenges: ch.challenges,
177191
meta: ch.meta,
178192
frontFilter,
179193
}));
@@ -325,7 +339,7 @@ function getUserChallengesInit(uuid) {
325339
function getUserChallengesDone(userId, tokenV3) {
326340
const service = getService(tokenV3);
327341

328-
return service.getUserResources(userId)
342+
return service.getUserResources(userId, 1, 10000)
329343
.then(item => item)
330344
.catch((error) => {
331345
fireErrorMessage('Error Getting User Challenges', error.content || error);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function Listing({
2121
auth,
2222
challenges,
2323
challengeTypes,
24+
userChallenges,
2425
challengesUrl,
2526
communityName,
2627
extraBucket,
@@ -46,7 +47,7 @@ function Listing({
4647
pastSearchTimestamp,
4748
isLoggedIn,
4849
}) {
49-
const buckets = getBuckets(_.get(auth, 'user.userId'));
50+
const buckets = getBuckets(userChallenges);
5051
const isChallengesAvailable = (bucket) => {
5152
const filter = Filter.getFilterFunction(buckets[bucket].filter);
5253
const clonedChallenges = _.clone(challenges);
@@ -179,6 +180,7 @@ Listing.defaultProps = {
179180
// onExpandFilterResult: _.noop,
180181
openChallengesInNewTabs: false,
181182
pastSearchTimestamp: 0,
183+
userChallenges: [],
182184
};
183185

184186
Listing.propTypes = {
@@ -214,6 +216,7 @@ Listing.propTypes = {
214216
setSort: PT.func.isRequired,
215217
sorts: PT.shape().isRequired,
216218
pastSearchTimestamp: PT.number,
219+
userChallenges: PT.arrayOf(PT.string),
217220
isLoggedIn: PT.bool.isRequired,
218221
};
219222

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export default function ChallengeListing(props) {
115115
sorts={props.sorts}
116116
loadMoreActive={props.loadMoreActive}
117117
loadingActiveChallenges={props.loadingChallenges}
118+
userChallenges={props.userChallenges}
118119
isLoggedIn={isLoggedIn}
119120
/>
120121
);
@@ -169,6 +170,7 @@ ChallengeListing.defaultProps = {
169170
expandTag: null,
170171
loadMoreActive: null,
171172
isBucketSwitching: false,
173+
userChallenges: [],
172174
};
173175

174176
ChallengeListing.propTypes = {
@@ -205,5 +207,6 @@ ChallengeListing.propTypes = {
205207
auth: PT.shape(),
206208
loadMoreActive: PT.func,
207209
isBucketSwitching: PT.bool,
210+
userChallenges: PT.arrayOf(PT.string),
208211
isLoggedIn: PT.bool.isRequired,
209212
};

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export class ListingContainer extends React.Component {
149149
auth,
150150
getActiveChallenges,
151151
lastRequestedPageOfActiveChallenges,
152+
getUserChallenges,
152153
} = this.props;
153154
const f = this.getBackendFilter();
154155
getActiveChallenges(
@@ -157,6 +158,10 @@ export class ListingContainer extends React.Component {
157158
auth.tokenV3,
158159
f.front,
159160
);
161+
if (auth.tokenV3) {
162+
const userId = _.get(auth.user, 'userId');
163+
getUserChallenges(userId, auth.tokenV3);
164+
}
160165
}
161166

162167
render() {
@@ -204,6 +209,7 @@ export class ListingContainer extends React.Component {
204209
sorts,
205210
hideTcLinksInSidebarFooter,
206211
isBucketSwitching,
212+
userChallenges,
207213
} = this.props;
208214

209215
const { tokenV3 } = auth;
@@ -306,6 +312,7 @@ export class ListingContainer extends React.Component {
306312
groupIds={groupIds}
307313
auth={auth}
308314
isBucketSwitching={isBucketSwitching}
315+
userChallenges={userChallenges}
309316
isLoggedIn={isLoggedIn}
310317
/>
311318
</div>
@@ -333,6 +340,7 @@ ListingContainer.defaultProps = {
333340
queryBucket: BUCKETS.ALL,
334341
meta: {},
335342
isBucketSwitching: false,
343+
userChallenges: [],
336344
};
337345

338346
ListingContainer.propTypes = {
@@ -402,6 +410,8 @@ ListingContainer.propTypes = {
402410
meta: PT.shape(),
403411
isBucketSwitching: PT.bool,
404412
selectBucketDone: PT.func.isRequired,
413+
userChallenges: PT.arrayOf(PT.string),
414+
getUserChallenges: PT.func.isRequired,
405415
};
406416

407417
const mapStateToProps = (state, ownProps) => {
@@ -445,6 +455,7 @@ const mapStateToProps = (state, ownProps) => {
445455
isBucketSwitching: cl.sidebar.isBucketSwitching,
446456
expandedTags: cl.expandedTags,
447457
meta: cl.meta,
458+
userChallenges: cl.userChallenges,
448459
};
449460
};
450461

@@ -491,6 +502,11 @@ function mapDispatchToProps(dispatch) {
491502
setSort: (bucket, sort) => dispatch(a.setSort(bucket, sort)),
492503
markHeaderMenu: () => dispatch(ah.setCurrentNav('Compete', 'All Challenges')),
493504
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+
},
494510
};
495511
}
496512

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

Lines changed: 5 additions & 2 deletions
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,
6160
updateAllSavedFilters,
6261
updateSavedFilter,
62+
userChallenges,
6363
} = this.props;
6464

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

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

120121
SidebarContainer.propTypes = {
@@ -135,6 +136,7 @@ SidebarContainer.propTypes = {
135136
updateAllSavedFilters: PT.func.isRequired,
136137
updateSavedFilter: PT.func.isRequired,
137138
user: PT.shape(),
139+
userChallenges: PT.arrayOf(PT.string),
138140
};
139141

140142
function mapDispatchToProps(dispatch) {
@@ -167,6 +169,7 @@ function mapStateToProps(state, ownProps) {
167169
selectedCommunityId: state.challengeListing.selectedCommunityId,
168170
tokenV2: state.auth.tokenV2,
169171
user: state.auth.user,
172+
userChallenges: state.challengeListing.userChallenges,
170173
};
171174
}
172175

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

Lines changed: 2 additions & 2 deletions
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(userId) {
137+
export function getBuckets(userChallenges) {
138138
const res = _.cloneDeep(BUCKET_DATA);
139-
res[BUCKETS.MY].filter.userId = userId;
139+
res[BUCKETS.MY].filter.userChallenges = userChallenges;
140140
return res;
141141
}
142142

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,3 @@ 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)