Skip to content

Commit d7e336e

Browse files
Refactor challenge list
1 parent 69fecea commit d7e336e

File tree

3 files changed

+72
-26
lines changed

3 files changed

+72
-26
lines changed

src/services/challenges.js

+37-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,36 @@ import { getApi } from './api';
1515
import { getService as getMembersService } from './members';
1616
import { getService as getSubmissionsService } from './submissions';
1717

18+
export function getFilterUrl(backendFilter, frontFilter) {
19+
const ff = _.clone(frontFilter);
20+
const { tags, tracks, types } = ff;
21+
delete ff.tags;
22+
delete ff.tracks;
23+
delete ff.types;
24+
delete ff.communityId;
25+
26+
console.log(ff);
27+
28+
let urlFilter = qs.stringify(_.reduce(ff, (result, value, key) => {
29+
// eslint-disable-next-line no-param-reassign
30+
if (value) result[key] = value;
31+
return result;
32+
}, {}));
33+
console.log(urlFilter);
34+
35+
const ftags = _.map(tags, val => `tags[]=${val}`).join('&');
36+
const ftracks = _.map(_.reduce(tracks, (result, value, key) => {
37+
// eslint-disable-next-line no-unused-expressions
38+
tracks[key] && result.push(key);
39+
return result;
40+
}, []), val => `tracks[]=${val}`).join('&');
41+
const ftypes = _.map(types, val => `types[]=${val}`).join('&');
42+
if (ftags.length > 0) urlFilter += `&${ftags}`;
43+
if (ftracks.length > 0) urlFilter += `&${ftracks}`;
44+
if (ftypes.length > 0) urlFilter += `&${ftypes}`;
45+
return urlFilter;
46+
}
47+
1848
export const ORDER_BY = {
1949
SUBMISSION_END_DATE: 'submissionEndDate',
2050
};
@@ -133,14 +163,12 @@ class ChallengesService {
133163
*/
134164
const getChallenges = async (
135165
endpoint,
136-
filters = {},
137-
params = {},
166+
filter,
138167
) => {
139-
const query = {
140-
...filters,
141-
...params,
142-
};
143-
const url = `${endpoint}?${qs.stringify(query)}`;
168+
console.log(filter);
169+
const query = getFilterUrl(filter.backendFilter, filter.frontFilter);
170+
const url = `${endpoint}?${query}`;
171+
console.log(url);
144172
const res = await this.private.apiV5.get(url).then(checkErrorV5);
145173
return {
146174
challenges: res.result || [],
@@ -462,8 +490,8 @@ class ChallengesService {
462490
* @param {Object} params Optional.
463491
* @return {Promise} Resolves to the api response.
464492
*/
465-
async getChallenges(filters, params) {
466-
return this.private.getChallenges('/challenges/', filters, params)
493+
async getChallenges(filter) {
494+
return this.private.getChallenges('/challenges/', filter)
467495
.then((res) => {
468496
res.challenges.forEach(item => normalizeChallenge(item));
469497
return res;

src/utils/challenge/filter.js

+32-14
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,23 @@ function filterByStatus(challenge, state) {
144144
}
145145

146146
function filterByTags(challenge, state) {
147-
if (!state.tags) return true;
147+
if (!state.tags || state.tags.length === 0) return true;
148148
const { platforms, tags } = challenge;
149-
const str = `${platforms} ${tags}`.toLowerCase();
149+
const str = `${platforms.join(' ')} ${tags.join(' ')}`.toLowerCase();
150150
return state.tags.some(tag => str.includes(tag.toLowerCase()));
151151
}
152152

153153
function filterByText(challenge, state) {
154-
if (!state.text) return true;
154+
if (!state.name) return true;
155155
const str = `${challenge.name} ${challenge.tags} ${challenge.platforms} ${challenge.tags}`
156156
.toLowerCase();
157-
return str.includes(state.text.toLowerCase());
157+
return str.includes(state.name.toLowerCase());
158158
}
159159

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

165166
function filterByTypes(challenge, state) {
@@ -239,7 +240,13 @@ export function getFilterFunction(state) {
239240
*/
240241
export function getReviewOpportunitiesFilterFunction(state, validTypes) {
241242
return (opp) => {
242-
const newType = _.find(validTypes, { name: opp.challenge.type }) || {};
243+
const trackAbbr = {
244+
DATA_SCIENCE: 'DS',
245+
DEVELOP: 'Dev',
246+
DESIGN: 'Des',
247+
QA: 'QA',
248+
};
249+
// const newType = _.find(validTypes, { name: opp.challenge.type }) || {};
243250

244251
// Review Opportunity objects have a challenge field which
245252
// is largely compatible with many of the existing filter functions
@@ -248,16 +255,27 @@ export function getReviewOpportunitiesFilterFunction(state, validTypes) {
248255
...opp.challenge,
249256
// This allows filterByText to search for Review Types and Challenge Titles
250257
name: `${opp.challenge.title} ${REVIEW_OPPORTUNITY_TYPES[opp.type]}`,
251-
registrationStartDate: opp.startDate, // startDate of Review, not Challenge
252-
submissionEndDate: opp.startDate, // Currently uses startDate for both date comparisons
253-
communities: new Set([ // Used to filter by Track, and communities at a future date
254-
opp.challenge.track.toLowerCase(),
255-
]),
256-
typeId: newType.id,
258+
// registrationStartDate: opp.startDate, // startDate of Review, not Challenge
259+
// submissionEndDate: opp.startDate, // Currently uses startDate for both date comparisons
260+
// communities: new Set([ // Used to filter by Track, and communities at a future date
261+
// opp.challenge.track === 'QA' ? 'Dev' : trackAbbr[opp.challenge.track],
262+
// ]),
263+
track: trackAbbr[opp.challenge.track],
264+
// typeId: newType.id,
257265
tags: opp.challenge.technologies || [],
258266
platforms: opp.challenge.platforms || [],
259267
};
260-
268+
/**
269+
console.log(challenge);
270+
console.log(`=====`);
271+
console.log(`11111`);
272+
console.log(filterByTrack(challenge, state));
273+
console.log(filterByText(challenge, state));
274+
console.log(filterByTags(challenge, state));
275+
console.log(filterByEndDate(challenge, state));
276+
console.log(filterByStartDate(challenge, state));
277+
console.log(filterByReviewOpportunityType(opp, state));
278+
*/
261279
return (
262280
filterByTrack(challenge, state)
263281
&& filterByText(challenge, state)

src/utils/tc.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* uses upper-case literals to encode the tracks. At some point, we should
1212
* update it in this code as well! */
1313
export const COMPETITION_TRACKS = {
14-
DATA_SCIENCE: 'Data Science',
15-
DESIGN: 'Design',
16-
DEVELOP: 'Development',
14+
DS: 'Data Science',
15+
DES: 'Design',
16+
DEV: 'Development',
1717
QA: 'Quality Assurance',
1818
};
1919

0 commit comments

Comments
 (0)