Skip to content

fix(challenge-listing): my challenges bucket/filter #4801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ exports[`Matches shallow shapshot 1 shapshot 1 1`] = `
setFilterState={[MockFunction]}
setSort={[MockFunction]}
sorts={Object {}}
userChallenges={Array []}
/>
<div
className="src-shared-components-challenge-listing-___style__sidebar-container-desktop___h0bz6"
Expand Down
36 changes: 11 additions & 25 deletions src/shared/actions/challenge-listing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createActions } from 'redux-actions';
import { decodeToken } from 'tc-accounts';
import 'isomorphic-fetch';
import { processSRM } from 'utils/tc';
import { mapUserWithChallenges } from 'utils/challenge-listing/helper';
import { errors, services } from 'topcoder-react-lib';

const { fireErrorMessage } = errors;
Expand Down Expand Up @@ -104,27 +105,12 @@ function getAllActiveChallengesWithUsersDone(uuid, tokenV3, filter, page = 0) {
calls.push(getAll(params => service.getUserChallenges(user, newFilter, params)
.catch(() => ({ challenges: [] }))), page);
}
return Promise.all(calls).then(([ch, uch]) => {
/* uch array contains challenges where the user is participating in
@@ -111,8 +124,8 @@ function getAllActiveChallengesDone(uuid, tokenV3) {
* challenges in an efficient way. */
if (uch) {
const map = {};
uch.forEach((item) => { map[item.id] = item; });
ch.forEach((item) => {
if (map[item.id]) {
/* It is fine to reassing, as the array we modifying is created just
* above within the same function. */
/* eslint-disable no-param-reassign */
item.users[user] = true;
item.userDetails = map[item.id].userDetails;
/* eslint-enable no-param-reassign */
}
});
}

return { uuid, challenges: ch, ...filter };
});
// uch array contains challenges where the user is participating in
return Promise.all(calls).then(([ch, uch]) => ({
uuid,
challenges: mapUserWithChallenges(user, ch, uch),
...filter,
}));
}

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

// Handle any errors on this endpoint so that the non-user specific challenges
// will still be loaded.
calls.push(service.getUserChallenges(user, filter, {})
.catch(() => ({ challenges: [] })));
calls.push(getAll(params => service.getUserChallenges(user, filter, params)
.catch(() => ({ challenges: [] }))));
}
return Promise.all(calls).then(([ch]) => ({
return Promise.all(calls).then(([ch, uch]) => ({
uuid,
challenges: ch.challenges,
challenges: mapUserWithChallenges(user, ch.challenges, uch),
meta: ch.meta,
frontFilter,
}));
Expand Down
5 changes: 1 addition & 4 deletions src/shared/components/challenge-listing/Listing/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ function Listing({
auth,
challenges,
challengeTypes,
userChallenges,
challengesUrl,
communityName,
extraBucket,
Expand All @@ -47,7 +46,7 @@ function Listing({
pastSearchTimestamp,
isLoggedIn,
}) {
const buckets = getBuckets(userChallenges);
const buckets = getBuckets(_.get(auth, 'user.userId'));
const isChallengesAvailable = (bucket) => {
const filter = Filter.getFilterFunction(buckets[bucket].filter);
const clonedChallenges = _.clone(challenges);
Expand Down Expand Up @@ -180,7 +179,6 @@ Listing.defaultProps = {
// onExpandFilterResult: _.noop,
openChallengesInNewTabs: false,
pastSearchTimestamp: 0,
userChallenges: [],
};

Listing.propTypes = {
Expand Down Expand Up @@ -216,7 +214,6 @@ Listing.propTypes = {
setSort: PT.func.isRequired,
sorts: PT.shape().isRequired,
pastSearchTimestamp: PT.number,
userChallenges: PT.arrayOf(PT.string),
isLoggedIn: PT.bool.isRequired,
};

Expand Down
3 changes: 0 additions & 3 deletions src/shared/components/challenge-listing/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export default function ChallengeListing(props) {
sorts={props.sorts}
loadMoreActive={props.loadMoreActive}
loadingActiveChallenges={props.loadingChallenges}
userChallenges={props.userChallenges}
isLoggedIn={isLoggedIn}
/>
);
Expand Down Expand Up @@ -170,7 +169,6 @@ ChallengeListing.defaultProps = {
expandTag: null,
loadMoreActive: null,
isBucketSwitching: false,
userChallenges: [],
};

ChallengeListing.propTypes = {
Expand Down Expand Up @@ -207,6 +205,5 @@ ChallengeListing.propTypes = {
auth: PT.shape(),
loadMoreActive: PT.func,
isBucketSwitching: PT.bool,
userChallenges: PT.arrayOf(PT.string),
isLoggedIn: PT.bool.isRequired,
};
16 changes: 0 additions & 16 deletions src/shared/containers/challenge-listing/Listing/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ export class ListingContainer extends React.Component {
auth,
getActiveChallenges,
lastRequestedPageOfActiveChallenges,
getUserChallenges,
} = this.props;
const f = this.getBackendFilter();
getActiveChallenges(
Expand All @@ -158,10 +157,6 @@ export class ListingContainer extends React.Component {
auth.tokenV3,
f.front,
);
if (auth.tokenV3) {
const userId = _.get(auth.user, 'userId');
getUserChallenges(userId, auth.tokenV3);
}
}

render() {
Expand Down Expand Up @@ -209,7 +204,6 @@ export class ListingContainer extends React.Component {
sorts,
hideTcLinksInSidebarFooter,
isBucketSwitching,
userChallenges,
} = this.props;

const { tokenV3 } = auth;
Expand Down Expand Up @@ -312,7 +306,6 @@ export class ListingContainer extends React.Component {
groupIds={groupIds}
auth={auth}
isBucketSwitching={isBucketSwitching}
userChallenges={userChallenges}
isLoggedIn={isLoggedIn}
/>
</div>
Expand Down Expand Up @@ -340,7 +333,6 @@ ListingContainer.defaultProps = {
queryBucket: BUCKETS.ALL,
meta: {},
isBucketSwitching: false,
userChallenges: [],
};

ListingContainer.propTypes = {
Expand Down Expand Up @@ -410,8 +402,6 @@ ListingContainer.propTypes = {
meta: PT.shape(),
isBucketSwitching: PT.bool,
selectBucketDone: PT.func.isRequired,
userChallenges: PT.arrayOf(PT.string),
getUserChallenges: PT.func.isRequired,
};

const mapStateToProps = (state, ownProps) => {
Expand Down Expand Up @@ -455,7 +445,6 @@ const mapStateToProps = (state, ownProps) => {
isBucketSwitching: cl.sidebar.isBucketSwitching,
expandedTags: cl.expandedTags,
meta: cl.meta,
userChallenges: cl.userChallenges,
};
};

Expand Down Expand Up @@ -502,11 +491,6 @@ function mapDispatchToProps(dispatch) {
setSort: (bucket, sort) => dispatch(a.setSort(bucket, sort)),
markHeaderMenu: () => dispatch(ah.setCurrentNav('Compete', 'All Challenges')),
expandTag: id => dispatch(a.expandTag(id)),
getUserChallenges: (userId, tokenV3) => {
const uuid = shortId();
dispatch(a.getUserChallengesInit(uuid));
dispatch(a.getUserChallengesDone(userId, tokenV3));
},
};
}

Expand Down
7 changes: 2 additions & 5 deletions src/shared/containers/challenge-listing/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ export class SidebarContainer extends React.Component {
setFilter,
setSearchText,
tokenV2,
user,
updateAllSavedFilters,
updateSavedFilter,
userChallenges,
} = this.props;

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

if (extraBucket) {
buckets[extraBucket.name] = extraBucket;
Expand Down Expand Up @@ -115,7 +115,6 @@ SidebarContainer.defaultProps = {
selectedCommunityId: '',
tokenV2: null,
user: null,
userChallenges: [],
};

SidebarContainer.propTypes = {
Expand All @@ -136,7 +135,6 @@ SidebarContainer.propTypes = {
updateAllSavedFilters: PT.func.isRequired,
updateSavedFilter: PT.func.isRequired,
user: PT.shape(),
userChallenges: PT.arrayOf(PT.string),
};

function mapDispatchToProps(dispatch) {
Expand Down Expand Up @@ -169,7 +167,6 @@ function mapStateToProps(state, ownProps) {
selectedCommunityId: state.challengeListing.selectedCommunityId,
tokenV2: state.auth.tokenV2,
user: state.auth.user,
userChallenges: state.challengeListing.userChallenges,
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/shared/utils/challenge-listing/buckets.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ export const NO_LIVE_CHALLENGES_CONFIG = {
* @param {String} userId id of the authenticated
* user to filter out My Challenges.
*/
export function getBuckets(userChallenges) {
export function getBuckets(userId) {
const res = _.cloneDeep(BUCKET_DATA);
res[BUCKETS.MY].filter.userChallenges = userChallenges;
res[BUCKETS.MY].filter.userId = userId;
return res;
}

Expand Down
23 changes: 23 additions & 0 deletions src/shared/utils/challenge-listing/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,26 @@ export function phaseStartDate(phase) {
// For all other cases, take the `actualStartDate` as phase is already started
return new Date(phase.actualStartDate);
}

/**
* Map user details with challenges
*
* @param {String} userId user id
* @param {Array} challenges all challenges
* @param {Array} userChallenges challenges user is participating in
*/
export function mapUserWithChallenges(userId, challenges, userChallenges) {
if (userChallenges) {
const map = {};
userChallenges.forEach((item) => { map[item.id] = item; });
challenges.forEach((item) => {
if (map[item.id]) {
/* eslint-disable no-param-reassign */
item.users[userId] = true;
item.userDetails = map[item.id].userDetails;
/* eslint-enable no-param-reassign */
}
});
}
return challenges;
}