Skip to content

Sync feature refactor challengelist with develop #266

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 9 commits into from
Oct 4, 2020
Merged
7 changes: 4 additions & 3 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ Object {
"SUBMISSION_END_DATE": "submissionEndDate",
},
"default": undefined,
"getFilterUrl": [Function],
"getService": [Function],
"normalizeChallenge": [Function],
},
Expand Down Expand Up @@ -378,9 +379,9 @@ Object {
"DRAFT": "Draft",
},
"COMPETITION_TRACKS": Object {
"DATA_SCIENCE": "Data Science",
"DESIGN": "Design",
"DEVELOP": "Development",
"DES": "Design",
"DEV": "Development",
"DS": "Data Science",
"QA": "Quality Assurance",
},
"OLD_COMPETITION_TRACKS": Object {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"lint:js": "./node_modules/.bin/eslint --ext .js,.jsx .",
"test": "npm run lint && npm run jest"
},
"version": "1.0.7",
"version": "1000.24.1",
"dependencies": {
"auth0-js": "^6.8.4",
"config": "^3.2.0",
Expand Down
71 changes: 60 additions & 11 deletions src/services/challenges.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,40 @@ import { getApi } from './api';
import { getService as getMembersService } from './members';
import { getService as getSubmissionsService } from './submissions';

export function getFilterUrl(backendFilter, frontFilter) {
const ff = _.clone(frontFilter);
// eslint-disable-next-line object-curly-newline
const { tags, tracks, types, groups } = ff;
delete ff.tags;
delete ff.tracks;
delete ff.types;
delete ff.communityId;
delete ff.groups;

// console.log(ff);

let urlFilter = qs.stringify(_.reduce(ff, (result, value, key) => {
// eslint-disable-next-line no-param-reassign
if (value) result[key] = value;
return result;
}, {}));
// console.log(urlFilter);

const ftags = _.map(tags, val => `tags[]=${val}`).join('&');
const ftracks = _.map(_.reduce(tracks, (result, value, key) => {
// eslint-disable-next-line no-unused-expressions
tracks[key] && result.push(key);
return result;
}, []), val => `tracks[]=${val}`).join('&');
const ftypes = _.map(types, val => `types[]=${val}`).join('&');
const fgroups = _.map(groups, val => `groups[]=${val}`).join('&');
if (ftags.length > 0) urlFilter += `&${ftags}`;
if (ftracks.length > 0) urlFilter += `&${ftracks}`;
if (ftypes.length > 0) urlFilter += `&${ftypes}`;
if (fgroups.length > 9) urlFilter += `&${fgroups}`;
return urlFilter;
}

export const ORDER_BY = {
SUBMISSION_END_DATE: 'submissionEndDate',
};
Expand Down Expand Up @@ -133,14 +167,12 @@ class ChallengesService {
*/
const getChallenges = async (
endpoint,
filters = {},
params = {},
filter,
) => {
const query = {
...filters,
...params,
};
const url = `${endpoint}?${qs.stringify(query)}`;
// console.log(filter);
const query = getFilterUrl(filter.backendFilter, filter.frontFilter);
const url = `${endpoint}?${query}`;
// console.log(url);
const res = await this.private.apiV5.get(url).then(checkErrorV5);
return {
challenges: res.result || [],
Expand All @@ -154,6 +186,22 @@ class ChallengesService {
},
};
};

const getChallengeDetails = async (
endpoint,
legacyInfo,
) => {
let query = '';
if (legacyInfo) {
query = `legacyId=${legacyInfo.legacyId}`;
}
const url = `${endpoint}?${query}`;
const res = await this.private.apiV5.get(url).then(checkErrorV5);
return {
challenges: res.result || [],
};
};

/**
* Private function being re-used in all methods related to getting
* challenges. It handles query-related arguments in the uniform way:
Expand Down Expand Up @@ -189,6 +237,7 @@ class ChallengesService {
apiV2: getApi('V2', tokenV2),
apiV3: getApi('V3', tokenV3),
getChallenges,
getChallengeDetails,
getMemberChallenges,
tokenV2,
tokenV3,
Expand Down Expand Up @@ -327,10 +376,10 @@ class ChallengesService {
// condition based on ROUTE used for Review Opportunities, change if needed
if (/^[\d]{5,8}$/.test(challengeId)) {
isLegacyChallenge = true;
challenge = await this.private.getChallenges('/challenges/', { legacyId: challengeId })
challenge = await this.private.getChallengeDetails('/challenges/', { legacyId: challengeId })
.then(res => res.challenges[0] || {});
} else {
challenge = await this.private.getChallenges(`/challenges/${challengeId}`)
challenge = await this.private.getChallengeDetails(`/challenges/${challengeId}`)
.then(res => res.challenges);
}

Expand Down Expand Up @@ -464,8 +513,8 @@ class ChallengesService {
* @param {Object} params Optional.
* @return {Promise} Resolves to the api response.
*/
async getChallenges(filters, params) {
return this.private.getChallenges('/challenges/', filters, params)
async getChallenges(filter) {
return this.private.getChallenges('/challenges/', filter)
.then((res) => {
res.challenges.forEach(item => normalizeChallenge(item));
return res;
Expand Down
59 changes: 39 additions & 20 deletions src/utils/challenge/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function filterByStatus(challenge, state) {
function filterByTags(challenge, state) {
if (_.isEmpty(state.tags)) return true;
const { platforms, tags } = challenge;
const str = `${platforms} ${tags}`.toLowerCase();
const str = `${platforms.join(' ')} ${tags.join(' ')}`.toLowerCase();
return state.tags.some(tag => str.includes(tag.toLowerCase()));
}

Expand All @@ -158,19 +158,20 @@ function filterByEvents(challenge, state) {
}

function filterByText(challenge, state) {
if (!state.text) return true;
if (!state.name) return true;
const str = `${challenge.name} ${challenge.tags} ${challenge.platforms} ${challenge.tags}`
.toLowerCase();
return str.includes(state.text.toLowerCase());
return str.includes(state.name.toLowerCase());
}

function filterByTrack(challenge, state) {
if (!state.tracks) return true;
return _.keys(state.tracks).some(track => challenge.track === track);
// if (!state.tracks) return true;
// eslint-disable-next-line max-len
return state.tracks[challenge.track] === true;
}

function filterByTypes(challenge, state) {
if (!state.types) return true;
if (state.types.length === 0) return true;
return state.types.includes(challenge.typeId);
}

Expand All @@ -179,10 +180,10 @@ function filterByUpcoming(challenge, state) {
return moment().isBefore(challenge.registrationStartDate);
}

function filterByUsers(challenge, state) {
if (!state.userChallenges) return true;
return state.userChallenges.find(ch => challenge.id === ch);
}
// function filterByUsers(challenge, state) {
// if (!state.userChallenges) return true;
// return state.userChallenges.find(ch => challenge.id === ch);
// }

/**
* Returns clone of the state with the specified competition track added.
Expand Down Expand Up @@ -223,7 +224,7 @@ export function getFilterFunction(state) {
&& filterByTags(challenge, state)
&& filterByEvents(challenge, state)
&& filterByTypes(challenge, state)
&& filterByUsers(challenge, state)
// && filterByUsers(challenge, state)
&& filterByEndDate(challenge, state)
&& filterByStartDate(challenge, state)
&& filterByStarted(challenge, state)
Expand All @@ -247,7 +248,14 @@ export function getFilterFunction(state) {
*/
export function getReviewOpportunitiesFilterFunction(state, validTypes) {
return (opp) => {
const newType = _.find(validTypes, { name: opp.challenge.type }) || {};
const trackAbbr = {
DATA_SCIENCE: 'DS',
DEVELOP: 'Dev',
DESIGN: 'Des',
QA: 'QA',
};
// const newType = _.find(validTypes, { name: opp.challenge.type }) || {};
const newType = _.find(validTypes, { name: opp.challenge.subTrack === 'FIRST_2_FINISH' ? 'First2Finish' : 'Challenge' }) || {};

// Review Opportunity objects have a challenge field which
// is largely compatible with many of the existing filter functions
Expand All @@ -256,21 +264,32 @@ export function getReviewOpportunitiesFilterFunction(state, validTypes) {
...opp.challenge,
// This allows filterByText to search for Review Types and Challenge Titles
name: `${opp.challenge.title} ${REVIEW_OPPORTUNITY_TYPES[opp.type]}`,
registrationStartDate: opp.startDate, // startDate of Review, not Challenge
submissionEndDate: opp.startDate, // Currently uses startDate for both date comparisons
communities: new Set([ // Used to filter by Track, and communities at a future date
opp.challenge.track.toLowerCase(),
]),
typeId: newType.id,
// registrationStartDate: opp.startDate, // startDate of Review, not Challenge
// submissionEndDate: opp.startDate, // Currently uses startDate for both date comparisons
// communities: new Set([ // Used to filter by Track, and communities at a future date
// opp.challenge.track === 'QA' ? 'Dev' : trackAbbr[opp.challenge.track],
// ]),
track: trackAbbr[opp.challenge.track],
typeId: newType.abbreviation,
tags: opp.challenge.technologies || [],
platforms: opp.challenge.platforms || [],
};

/**
console.log(challenge);
console.log(`=====`);
console.log(`11111`);
console.log(filterByTrack(challenge, state));
console.log(filterByText(challenge, state));
console.log(filterByTags(challenge, state));
console.log(filterByEndDate(challenge, state));
console.log(filterByStartDate(challenge, state));
console.log(filterByReviewOpportunityType(opp, state));
*/
return (
filterByTrack(challenge, state)
&& filterByText(challenge, state)
&& filterByTags(challenge, state)
// && filterByTypes(challenge, state)
&& filterByTypes(challenge, state)
&& filterByEndDate(challenge, state)
&& filterByStartDate(challenge, state)
&& filterByReviewOpportunityType(opp, state)
Expand Down
6 changes: 3 additions & 3 deletions src/utils/tc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* uses upper-case literals to encode the tracks. At some point, we should
* update it in this code as well! */
export const COMPETITION_TRACKS = {
DATA_SCIENCE: 'Data Science',
DESIGN: 'Design',
DEVELOP: 'Development',
DS: 'Data Science',
DES: 'Design',
DEV: 'Development',
QA: 'Quality Assurance',
};

Expand Down