Skip to content

Fixed issues of server-side filtering on challenge listings #68

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
merged 1 commit into from
May 27, 2019
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
30 changes: 17 additions & 13 deletions src/actions/challenge-listing.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ function getAllActiveChallengesDone(uuid, tokenV3) {
});
}

return { uuid, challenges: ch.challenges };
return {
uuid,
challenges: ch.challenges,
handle: user,
};
});
}

Expand Down Expand Up @@ -197,27 +201,27 @@ function getActiveChallengesDone(
}).catch(() => ({ challenges: [] })));
}
return Promise.all(calls).then(([ch, uch]) => {
let fullCH = ch;
// let fullCH = ch;
/* uch array contains challenges where the user is participating in
* some role. The same challenge are already listed in res array, but they
* are not attributed to the user there. This block of code marks user
* challenges in an efficient way. */
if (uch) {
const map = {};
uch.challenges.forEach((item) => {
map[item.id] = item;
/* eslint-disable no-param-reassign */
item.users[user] = true;
item.userDetails = map[item.id].userDetails;
/* eslint-enable no-param-reassign */
uch.challenges.forEach((item) => { map[item.id] = item; });
ch.challenges.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 */
}
});
}

if (uch) {
fullCH = uch;
}
let { challenges } = fullCH;
let { meta } = ch;
let { challenges, meta } = ch;
// filter by date range and re-compute meta
// we can safely remove the next two lines when backend support date range
challenges = filterUtil.filterByDate(challenges, frontFilter);
Expand Down
23 changes: 18 additions & 5 deletions src/reducers/challenge-listing.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,33 @@ function onGetAllActiveChallengesDone(state, { error, payload }) {
logger.error(payload);
return state;
}
const { uuid, challenges: loaded } = payload;
const {
uuid, challenges: loaded, handle,
} = payload;
if (uuid !== state.loadingActiveChallengesUUID) return state;
/* Once all active challenges are fetched from the API, we remove from the
* store any active challenges stored there previously, and also any
* challenges with IDs matching any challenges loaded now as active. */
const ids = new Set();
loaded.forEach(item => ids.add(item.id));
const challenges = state.challenges
.filter(item => item.status !== 'ACTIVE' && !ids.has(item.id))
.concat(loaded);

const filter = item => !ids.has(item.id) && item.status !== 'ACTIVE';
// BUCKET.MY
const my = processBucketData(
handle, state.challenges, loaded, BUCKETS.MY, null, null, filter, {},
);
// BUCKET.ALL
const all = processBucketData(
handle, state.challenges, loaded, BUCKETS.ALL, null, null, filter, {},
);

const newChallenges = _.cloneDeep(state.challenges);
newChallenges[BUCKETS.ALL] = all;
newChallenges[BUCKETS.MY] = my;

return {
...state,
challenges,
challenges: newChallenges,
lastUpdateOfActiveChallenges: Date.now(),
loadingActiveChallengesUUID: '',
};
Expand Down