Skip to content

add member stats history/distribution/activeChallenges #5

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
Jun 4, 2018
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
102 changes: 102 additions & 0 deletions src/actions/members.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { createActions } from 'redux-actions';
import { getService } from '../services/members';
import { getService as getUserService } from '../services/user';
import { getService as getChallengesService } from '../services/challenges';

/**
* @static
Expand Down Expand Up @@ -104,6 +105,101 @@ async function getStatsDone(handle, uuid, tokenV3) {
return { data, handle, uuid };
}

/**
* Payload creator for the action that inits the loading of member active challenges.
* @param {String} handle
* @param {String} uuid
* @returns {Object} Payload
*/
async function getActiveChallengesInit(handle, uuid) {
return { handle, uuid };
}

/**
* Payload creator for the action that loads the member active challenges.
* @param {String} handle
* @param {String} uuid
* @param {String} tokenV3
* @returns {Object} Payload
*/
async function getActiveChallengesDone(handle, uuid, tokenV3) {
const filter = { status: 'ACTIVE' };
const service = getChallengesService(tokenV3);
/* TODO: Reuse `getAll` from `actions/challenge-listing`
/* after it moved from `community-app` to here.
*/
function getAll(getter, page = 0, prev = null) {
const PAGE_SIZE = 50;
return getter({
limit: PAGE_SIZE,
offset: page * PAGE_SIZE,
}).then(({ challenges: chunk }) => {
if (!chunk.length) return prev || [];
return getAll(getter, 1 + page, prev ? prev.concat(chunk) : chunk);
});
}
const calls = [
getAll(params =>
service.getUserChallenges(handle, filter, params)),
getAll(params =>
service.getUserMarathonMatches(handle, filter, params)),
];

const [challenges] = await Promise.all(calls);

return { handle, challenges, uuid };
}

/**
* @static
* @desc Create an action that signals beginning of member stats distribution history.
* @param {String} handle Member handle.
* @param {String} uuid Operation UUID.
* @return {Action}
*/
async function getStatsHistoryInit(handle, uuid) {
return { handle, uuid };
}

/**
* @static
* @desc Create an action that loads the member stats history.
* @param {String} handle Member handle.
* @param {String} uuid Operation UUID.
* @param {String} tokenV3 v3 auth token.
* @return {Action}
*/
async function getStatsHistoryDone(handle, uuid, tokenV3) {
const data = await getService(tokenV3).getStatsHistory(handle);
return { data, handle, uuid };
}

/**
* @static
* @desc Create an action that signals beginning of member stats distribution loading.
* @param {String} handle Member handle.
* @param {String} uuid Operation UUID.
* @return {Action}
*/
async function getStatsDistributionInit(handle, uuid) {
return { handle, uuid };
}

/**
* @static
* @desc Create an action that loads the member stats distribution.
* @param {String} handle Member handle.
* @param {String} track Main track name.
* @param {String} subTrack Subtrack name.
* @param {String} uuid Operation UUID.
* @param {String} tokenV3 v3 auth token.
* @return {Action}
*/
async function getStatsDistributionDone(handle, track, subTrack, uuid, tokenV3) {
const data = await getService(tokenV3).getStatsDistribution(handle, track, subTrack);
return { data, handle, uuid };
}

export default createActions({
MEMBERS: {
DROP: drop,
Expand All @@ -114,5 +210,11 @@ export default createActions({
GET_FINANCES_DONE: getFinancesDone,
GET_STATS_INIT: getStatsInit,
GET_STATS_DONE: getStatsDone,
GET_STATS_HISTORY_INIT: getStatsHistoryInit,
GET_STATS_HISTORY_DONE: getStatsHistoryDone,
GET_STATS_DISTRIBUTION_INIT: getStatsDistributionInit,
GET_STATS_DISTRIBUTION_DONE: getStatsDistributionDone,
GET_ACTIVE_CHALLENGES_INIT: getActiveChallengesInit,
GET_ACTIVE_CHALLENGES_DONE: getActiveChallengesDone,
},
});
126 changes: 126 additions & 0 deletions src/reducers/members.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,126 @@ function onGetStatsDone(state, { error, payload }) {
};
}

/**
* Inits the loading of member stats history.
* @param {Object} state
* @param {Object} action
* @return {Object} New state.
*/
function onGetStatsHistoryInit(state, action) {
const { handle, uuid } = action.payload;
let res = state[handle];
res = res ? _.clone(res) : {};
res.statsHistory = { loadingUuid: uuid };
return {
...state,
[handle]: res,
};
}

/**
* Finalizes the loading of member stats history.
* @param {Object} state
* @param {Object} action
* @return {Object} New state.
*/
function onGetStatsHistoryDone(state, { error, payload }) {
if (error) {
logger.error('Failed to get member statsHistory', payload);
fireErrorMessage('Failed to get member statsHistory', '');
return state;
}

const { data, handle, uuid } = payload;
if (uuid !== _.get(state[handle], 'statsHistory.loadingUuid')) return state;
return {
...state,
[handle]: {
...state[handle],
statsHistory: { data, timestamp: Date.now() },
},
};
}

/**
* Inits the loading of member stats distribution.
* @param {Object} state
* @param {Object} action
* @return {Object} New state.
*/
function onGetStatsDistributionInit(state, action) {
const { handle, uuid } = action.payload;
let res = state[handle];
res = res ? _.clone(res) : {};
res.statsDistribution = { loadingUuid: uuid };
return {
...state,
[handle]: res,
};
}

/**
* Finalizes the loading of member stats distribution.
* @param {Object} state
* @param {Object} action
* @return {Object} New state.
*/
function onGetStatsDistributionDone(state, { error, payload }) {
if (error) {
logger.error('Failed to get member statsDistribution', payload);
fireErrorMessage('Failed to get member statsDistribution', '');
return state;
}

const { data, handle, uuid } = payload;
if (uuid !== _.get(state[handle], 'statsDistribution.loadingUuid')) return state;
return {
...state,
[handle]: {
...state[handle],
statsDistribution: { data, timestamp: Date.now() },
},
};
}

/**
* Inits the loading of member active challenges.
* @param {Object} state
* @param {Object} action
* @return {Object} New state.
*/
function onGetActiveChallengesInit(state, action) {
const { handle } = action.payload;
return {
...state,
[handle]: { ...state[handle], activeChallengesCount: null },
};
}

/**
* Finalizes the loading of member active challenges.
* @param {Object} state
* @param {Object} action
* @return {Object} New state.
*/
function onGetActiveChallengesDone(state, { error, payload }) {
if (error) {
logger.error('Failed to get member active challenges', payload);
fireErrorMessage('Failed to get member active challenges', '');
return state;
}

const { handle, challenges } = payload;

return {
...state,
[handle]: {
...state[handle],
activeChallengesCount: challenges.length,
},
};
}

/**
* Creates a new Members reducer with the specified initial state.
* @param {Object} initialState Optional. Initial state.
Expand All @@ -178,6 +298,12 @@ function create(initialState = {}) {
[a.getFinancesDone]: onGetFinancesDone,
[a.getStatsInit]: onGetStatsInit,
[a.getStatsDone]: onGetStatsDone,
[a.getStatsHistoryInit]: onGetStatsHistoryInit,
[a.getStatsHistoryDone]: onGetStatsHistoryDone,
[a.getStatsDistributionInit]: onGetStatsDistributionInit,
[a.getStatsDistributionDone]: onGetStatsDistributionDone,
[a.getActiveChallengesInit]: onGetActiveChallengesInit,
[a.getActiveChallengesDone]: onGetActiveChallengesDone,
}, initialState);
}

Expand Down
26 changes: 26 additions & 0 deletions src/services/members.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* members via API V3.
*/

import qs from 'qs';
import { getApiResponsePayloadV3 } from '../utils/tc';
import { getApiV3 } from './api';

Expand Down Expand Up @@ -84,6 +85,31 @@ class MembersService {
return getApiResponsePayloadV3(res);
}

/**
* Gets member statistics history
* @param {String} handle
* @return {Promise} Resolves to the stats object.
*/
async getStatsHistory(handle) {
const res = await this.private.api.get(`/members/${handle}/stats/history`);
return getApiResponsePayloadV3(res);
}

/**
* Gets member statistics distribution
* @param {String} handle
* @param {String} track
* @param {String} subTrack
* @return {Promise} Resolves to the stats object.
*/
async getStatsDistribution(handle, track, subTrack) {
const res = await this.private.api.get(`/members/stats/distribution?filter=${encodeURIComponent(qs.stringify({
track,
subTrack,
}))}`);
return getApiResponsePayloadV3(res);
}

/**
* Gets a list of suggested member names for the supplied partial.
*
Expand Down