Skip to content

Commit 24ec5f2

Browse files
authored
Merge pull request #4563 from nursoltan-s/issue-4559
fix #4559
2 parents 4b8883b + 70dbcfa commit 24ec5f2

File tree

9 files changed

+95
-7
lines changed

9 files changed

+95
-7
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
@@ -92,6 +92,7 @@ exports[`Matches shallow shapshot 1 shapshot 1 1`] = `
9292
setFilterState={[MockFunction]}
9393
setSort={[MockFunction]}
9494
sorts={Object {}}
95+
userChallenges={Array []}
9596
/>
9697
<div
9798
className="src-shared-components-challenge-listing-___style__sidebar-container-desktop___h0bz6"

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,31 @@ function getSrmsDone(uuid, handle, params, tokenV3) {
319319
});
320320
}
321321

322+
/**
323+
* Payload creator for the action that initialize user registered challenges.
324+
* @param {String} uuid
325+
* @return {String}
326+
*/
327+
function getUserChallengesInit(uuid) {
328+
return { uuid };
329+
}
330+
331+
/**
332+
* Payload creator for the action that loads user registered challenges.
333+
* @param {String} userId
334+
* @return {String}
335+
*/
336+
function getUserChallengesDone(userId, tokenV3) {
337+
const service = getService(tokenV3);
338+
339+
return service.getUserResources(userId)
340+
.then(item => item)
341+
.catch((error) => {
342+
fireErrorMessage('Error Getting User Challenges', error.content || error);
343+
return Promise.reject(error);
344+
});
345+
}
346+
322347
export default createActions({
323348
CHALLENGE_LISTING: {
324349
DROP_CHALLENGES: _.noop,
@@ -353,6 +378,9 @@ export default createActions({
353378
GET_SRMS_INIT: getSrmsInit,
354379
GET_SRMS_DONE: getSrmsDone,
355380

381+
GET_USER_CHALLENGES_INIT: getUserChallengesInit,
382+
GET_USER_CHALLENGES_DONE: getUserChallengesDone,
383+
356384
EXPAND_TAG: id => id,
357385

358386
/* Pass in community ID. */

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,
@@ -45,7 +46,7 @@ function Listing({
4546
expandTag,
4647
pastSearchTimestamp,
4748
}) {
48-
const buckets = getBuckets(_.get(auth.user, 'userId'));
49+
const buckets = getBuckets(userChallenges);
4950
const isChallengesAvailable = (bucket) => {
5051
const filter = Filter.getFilterFunction(buckets[bucket].filter);
5152
const clonedChallenges = _.clone(challenges);
@@ -176,6 +177,7 @@ Listing.defaultProps = {
176177
// onExpandFilterResult: _.noop,
177178
openChallengesInNewTabs: false,
178179
pastSearchTimestamp: 0,
180+
userChallenges: [],
179181
};
180182

181183
Listing.propTypes = {
@@ -211,6 +213,7 @@ Listing.propTypes = {
211213
setSort: PT.func.isRequired,
212214
sorts: PT.shape().isRequired,
213215
pastSearchTimestamp: PT.number,
216+
userChallenges: PT.arrayOf(PT.shape()),
214217
};
215218

216219
const mapStateToProps = (state) => {

src/shared/components/challenge-listing/Sidebar/BucketSelector/Bucket/index.jsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ function Bucket({
2323
let countEl;
2424
if (!bucket.hideCount && !disabled) {
2525
const filter = Filter.getFilterFunction(bucket.filter);
26+
const clonedChallenges = _.clone(challenges);
27+
const filteredChallenges = [];
28+
for (let i = 0; i < clonedChallenges.length; i += 1) {
29+
if (filter(clonedChallenges[i])) {
30+
filteredChallenges.push(clonedChallenges[i]);
31+
}
32+
}
2633
let count;
2734
if (allActiveChallengesLoaded) {
2835
count = challenges.filter(filter).length;
@@ -32,7 +39,7 @@ function Bucket({
3239
count = meta.allChallengesCount;
3340
break;
3441
case 'My Challenges':
35-
count = meta.myChallengesCount;
42+
count = filteredChallenges.length;
3643
break;
3744
case 'Open for registration':
3845
count = meta.openChallengesCount;

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
/>
119120
);
120121
}
@@ -200,6 +201,7 @@ ChallengeListing.defaultProps = {
200201
expandTag: null,
201202
loadMoreActive: null,
202203
isBucketSwitching: false,
204+
userChallenges: [],
203205
};
204206

205207
ChallengeListing.propTypes = {
@@ -236,4 +238,5 @@ ChallengeListing.propTypes = {
236238
auth: PT.shape(),
237239
loadMoreActive: PT.func,
238240
isBucketSwitching: PT.bool,
241+
userChallenges: PT.arrayOf(PT.string),
239242
};

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export class ListingContainer extends React.Component {
148148
auth,
149149
getActiveChallenges,
150150
lastRequestedPageOfActiveChallenges,
151+
getUserChallenges,
151152
} = this.props;
152153
const f = this.getBackendFilter();
153154
getActiveChallenges(
@@ -156,6 +157,10 @@ export class ListingContainer extends React.Component {
156157
auth.tokenV3,
157158
f.front,
158159
);
160+
if (auth) {
161+
const userId = _.get(auth.user, 'userId');
162+
getUserChallenges(userId, auth.tokenV3);
163+
}
159164
}
160165

161166
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;
@@ -305,6 +311,7 @@ export class ListingContainer extends React.Component {
305311
groupIds={groupIds}
306312
auth={auth}
307313
isBucketSwitching={isBucketSwitching}
314+
userChallenges={userChallenges}
308315
/>
309316
</div>
310317
);
@@ -331,6 +338,7 @@ ListingContainer.defaultProps = {
331338
queryBucket: BUCKETS.ALL,
332339
meta: {},
333340
isBucketSwitching: false,
341+
userChallenges: [],
334342
};
335343

336344
ListingContainer.propTypes = {
@@ -401,6 +409,8 @@ ListingContainer.propTypes = {
401409
meta: PT.shape(),
402410
isBucketSwitching: PT.bool,
403411
selectBucketDone: PT.func.isRequired,
412+
userChallenges: PT.arrayOf(PT.string),
413+
getUserChallenges: PT.func.isRequired,
404414
};
405415

406416
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
@@ -59,10 +59,10 @@ export class SidebarContainer extends React.Component {
5959
tokenV2,
6060
updateAllSavedFilters,
6161
updateSavedFilter,
62-
user,
62+
userChallenges,
6363
} = this.props;
6464

65-
const buckets = getBuckets(user && user.userId);
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/reducers/challenge-listing/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,31 @@ function onGetSrmsDone(state, { error, payload }) {
479479
};
480480
}
481481

482+
/**
483+
* Handles CHALLENGE_LISTING/GET_USER_CHALLENGES_INIT action
484+
* @param {Object} state
485+
* @return {Object} New state.
486+
*/
487+
function onGetUserChallengesInit(state) {
488+
return {
489+
...state,
490+
userChallenges: [],
491+
};
492+
}
493+
494+
/**
495+
* Handles CHALLENGE_LISTING/GET_USER_CHALLENGES_DONE action
496+
* @param {Object} state
497+
* @param {Object} payload
498+
* @return {Object} New state.
499+
*/
500+
function onGetUserChallengesDone(state, { payload }) {
501+
return {
502+
...state,
503+
userChallenges: payload,
504+
};
505+
}
506+
482507
/**
483508
* Creates a new Challenge Listing reducer with the specified initial state.
484509
* @param {Object} initialState Optional. Initial state.
@@ -556,6 +581,9 @@ function create(initialState) {
556581
[a.getSrmsInit]: onGetSrmsInit,
557582
[a.getSrmsDone]: onGetSrmsDone,
558583

584+
[a.getUserChallengesInit]: onGetUserChallengesInit,
585+
[a.getUserChallengesDone]: onGetUserChallengesDone,
586+
559587
[a.selectCommunity]: onSelectCommunity,
560588

561589
[a.setFilter]: onSetFilter,

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const BUCKET_DATA = {
2929
},
3030
[BUCKETS.MY]: {
3131
filter: {
32-
started: true,
3332
status: ['Active'],
3433
// users: [userHandle],
3534
},
@@ -134,9 +133,9 @@ export const NO_LIVE_CHALLENGES_CONFIG = {
134133
* @param {String} userId id of the authenticated
135134
* user to filter out My Challenges.
136135
*/
137-
export function getBuckets(userId) {
136+
export function getBuckets(userChallenges) {
138137
const res = _.cloneDeep(BUCKET_DATA);
139-
res[BUCKETS.MY].filter.users = [userId];
138+
res[BUCKETS.MY].filter.userChallenges = userChallenges;
140139
return res;
141140
}
142141

0 commit comments

Comments
 (0)