Skip to content

Commit 53bd448

Browse files
Add support for fetching challenge statistics
1 parent 4a65856 commit 53bd448

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

src/actions/challenge.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,25 @@ function getSubmissionInformationDone(challengeId, submissionId, tokenV3) {
392392
});
393393
}
394394

395+
/**
396+
* @static
397+
* @desc Creates an action that signals beginning of fetching challenge statistics
398+
* @return {Action}
399+
*/
400+
function fetchChallengeStatisticsInit() {}
401+
402+
/**
403+
* @static
404+
* @desc Creates an action that gets challenge statistics from the backend.
405+
* @param {String} challengeId The challenge id
406+
* @param {String} tokenV3 Topcoder auth token v3.
407+
* @return {Action}
408+
*/
409+
function fetchChallengeStatisticsDone(challengeId, tokenV3) {
410+
const challengeService = getChallengesService(tokenV3);
411+
return challengeService.getChallengeStatistics(challengeId)
412+
}
413+
395414
export default createActions({
396415
CHALLENGE: {
397416
DROP_CHECKPOINTS: dropCheckpoints,
@@ -417,5 +436,7 @@ export default createActions({
417436
GET_MM_SUBMISSIONS_DONE: getMMSubmissionsDone,
418437
GET_SUBMISSION_INFORMATION_INIT: getSubmissionInformationInit,
419438
GET_SUBMISSION_INFORMATION_DONE: getSubmissionInformationDone,
439+
GET_CHALLENGE_STATISTICS_INIT: fetchChallengeStatisticsInit,
440+
GET_CHALLENGE_STATISTICS_DONE: fetchChallengeStatisticsDone,
420441
},
421442
});

src/reducers/challenge.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,39 @@ function onGetSubmissionInformationDone(state, action) {
368368
};
369369
}
370370

371+
/**
372+
* Handles CHALLENGE/GET_CHALLENGE_STATISTICS_INIT action.
373+
* @param {Object} state
374+
* @param {Object} action
375+
* @return {Object} New state.
376+
*/
377+
function onFetchChallengeStatisticsInit(state, action) {
378+
return {
379+
...state,
380+
statisticsData: [],
381+
};
382+
}
383+
384+
/**
385+
* Handles CHALLENGE/GET_CHALLENGE_STATISTICS_DONE action.
386+
* @param {Object} state Previous state.
387+
* @param {Object} action Action.
388+
*/
389+
function onFetchChallengeStatisticsDone(state, action) {
390+
if (action.error) {
391+
logger.error('Failed to get challenge statistics', action.payload);
392+
return {
393+
...state,
394+
statisticsData: [],
395+
};
396+
}
397+
398+
return {
399+
...state,
400+
statisticsData: action.payload,
401+
};
402+
}
403+
371404
/**
372405
* Creates a new Challenge reducer with the specified initial state.
373406
* @param {Object} initialState Optional. Initial state.
@@ -411,6 +444,8 @@ function create(initialState) {
411444
[a.getActiveChallengesCountDone]: onGetActiveChallengesCountDone,
412445
[a.getSubmissionInformationInit]: onGetSubmissionInformationInit,
413446
[a.getSubmissionInformationDone]: onGetSubmissionInformationDone,
447+
[a.fetchChallengeStatisticsInit]: onFetchChallengeStatisticsInit,
448+
[a.fetchChallengeStatisticsDone]: onFetchChallengeStatisticsDone,
414449
}, _.defaults(initialState, {
415450
details: null,
416451
loadingCheckpoints: false,
@@ -427,6 +462,7 @@ function create(initialState) {
427462
updatingChallengeUuid: '',
428463
mmSubmissions: [],
429464
submissionInformation: null,
465+
statisticsData: []
430466
}));
431467
}
432468

src/services/challenges.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,21 @@ class ChallengesService {
253253
};
254254
}
255255

256+
/**
257+
* Gets challenge statistics.
258+
* @param {Number} challengeId
259+
* @return {Promise} The array of statistics
260+
*/
261+
async getChallengeStatistics (challengeId) {
262+
return this.private.apiV5.get(`/challenges/${challengeId}/statistics`)
263+
.then(res => (res.ok ? res.json() : new Error(res.statusText)))
264+
.then(res => (
265+
res.message
266+
? new Error(res.message)
267+
: res
268+
));
269+
}
270+
256271
/**
257272
* Activates the specified challenge.
258273
* @param {Number} challengeId

0 commit comments

Comments
 (0)