6
6
import { createActions } from 'redux-actions' ;
7
7
import { getService } from '../services/members' ;
8
8
import { getService as getUserService } from '../services/user' ;
9
+ import { getService as getChallengesService } from '../services/challenges' ;
9
10
10
11
/**
11
12
* @static
@@ -104,6 +105,101 @@ async function getStatsDone(handle, uuid, tokenV3) {
104
105
return { data, handle, uuid } ;
105
106
}
106
107
108
+ /**
109
+ * Payload creator for the action that inits the loading of member active challenges.
110
+ * @param {String } handle
111
+ * @param {String } uuid
112
+ * @returns {Object } Payload
113
+ */
114
+ async function getActiveChallengesInit ( handle , uuid ) {
115
+ return { handle, uuid } ;
116
+ }
117
+
118
+ /**
119
+ * Payload creator for the action that loads the member active challenges.
120
+ * @param {String } handle
121
+ * @param {String } uuid
122
+ * @param {String } tokenV3
123
+ * @returns {Object } Payload
124
+ */
125
+ async function getActiveChallengesDone ( handle , uuid , tokenV3 ) {
126
+ const filter = { status : 'ACTIVE' } ;
127
+ const service = getChallengesService ( tokenV3 ) ;
128
+ /* TODO: Reuse `getAll` from `actions/challenge-listing`
129
+ /* after it moved from `community-app` to here.
130
+ */
131
+ function getAll ( getter , page = 0 , prev = null ) {
132
+ const PAGE_SIZE = 50 ;
133
+ return getter ( {
134
+ limit : PAGE_SIZE ,
135
+ offset : page * PAGE_SIZE ,
136
+ } ) . then ( ( { challenges : chunk } ) => {
137
+ if ( ! chunk . length ) return prev || [ ] ;
138
+ return getAll ( getter , 1 + page , prev ? prev . concat ( chunk ) : chunk ) ;
139
+ } ) ;
140
+ }
141
+ const calls = [
142
+ getAll ( params =>
143
+ service . getUserChallenges ( handle , filter , params ) ) ,
144
+ getAll ( params =>
145
+ service . getUserMarathonMatches ( handle , filter , params ) ) ,
146
+ ] ;
147
+
148
+ const [ challenges ] = await Promise . all ( calls ) ;
149
+
150
+ return { handle, challenges, uuid } ;
151
+ }
152
+
153
+ /**
154
+ * @static
155
+ * @desc Create an action that signals beginning of member stats distribution history.
156
+ * @param {String } handle Member handle.
157
+ * @param {String } uuid Operation UUID.
158
+ * @return {Action }
159
+ */
160
+ async function getStatsHistoryInit ( handle , uuid ) {
161
+ return { handle, uuid } ;
162
+ }
163
+
164
+ /**
165
+ * @static
166
+ * @desc Create an action that loads the member stats history.
167
+ * @param {String } handle Member handle.
168
+ * @param {String } uuid Operation UUID.
169
+ * @param {String } tokenV3 v3 auth token.
170
+ * @return {Action }
171
+ */
172
+ async function getStatsHistoryDone ( handle , uuid , tokenV3 ) {
173
+ const data = await getService ( tokenV3 ) . getStatsHistory ( handle ) ;
174
+ return { data, handle, uuid } ;
175
+ }
176
+
177
+ /**
178
+ * @static
179
+ * @desc Create an action that signals beginning of member stats distribution loading.
180
+ * @param {String } handle Member handle.
181
+ * @param {String } uuid Operation UUID.
182
+ * @return {Action }
183
+ */
184
+ async function getStatsDistributionInit ( handle , uuid ) {
185
+ return { handle, uuid } ;
186
+ }
187
+
188
+ /**
189
+ * @static
190
+ * @desc Create an action that loads the member stats distribution.
191
+ * @param {String } handle Member handle.
192
+ * @param {String } track Main track name.
193
+ * @param {String } subTrack Subtrack name.
194
+ * @param {String } uuid Operation UUID.
195
+ * @param {String } tokenV3 v3 auth token.
196
+ * @return {Action }
197
+ */
198
+ async function getStatsDistributionDone ( handle , track , subTrack , uuid , tokenV3 ) {
199
+ const data = await getService ( tokenV3 ) . getStatsDistribution ( handle , track , subTrack ) ;
200
+ return { data, handle, uuid } ;
201
+ }
202
+
107
203
export default createActions ( {
108
204
MEMBERS : {
109
205
DROP : drop ,
@@ -114,5 +210,11 @@ export default createActions({
114
210
GET_FINANCES_DONE : getFinancesDone ,
115
211
GET_STATS_INIT : getStatsInit ,
116
212
GET_STATS_DONE : getStatsDone ,
213
+ GET_STATS_HISTORY_INIT : getStatsHistoryInit ,
214
+ GET_STATS_HISTORY_DONE : getStatsHistoryDone ,
215
+ GET_STATS_DISTRIBUTION_INIT : getStatsDistributionInit ,
216
+ GET_STATS_DISTRIBUTION_DONE : getStatsDistributionDone ,
217
+ GET_ACTIVE_CHALLENGES_INIT : getActiveChallengesInit ,
218
+ GET_ACTIVE_CHALLENGES_DONE : getActiveChallengesDone ,
117
219
} ,
118
220
} ) ;
0 commit comments