diff --git a/src/actions/members.js b/src/actions/members.js index c9f054d6..2fb8dfe8 100644 --- a/src/actions/members.js +++ b/src/actions/members.js @@ -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 @@ -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, @@ -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, }, }); diff --git a/src/reducers/members.js b/src/reducers/members.js index c0bf8370..0a297c6b 100644 --- a/src/reducers/members.js +++ b/src/reducers/members.js @@ -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. @@ -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); } diff --git a/src/services/members.js b/src/services/members.js index ba75e5c7..13b59d0f 100644 --- a/src/services/members.js +++ b/src/services/members.js @@ -4,6 +4,7 @@ * members via API V3. */ +import qs from 'qs'; import { getApiResponsePayloadV3 } from '../utils/tc'; import { getApiV3 } from './api'; @@ -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. * pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy