|
1 | 1 | /**
|
2 | 2 | * Challenge listing actions.
|
| 3 | + * |
| 4 | + * In the Redux state we keep an array of challenge objects loaded into the |
| 5 | + * listing(s), and a set of UUIDs of pending requests to load more challenges. |
| 6 | + * To load more challenges you should first dispatch GET_INIT action with some |
| 7 | + * UUID (use shortid package to generate it), then one of GET_CHALLENGES, |
| 8 | + * GET_MARATHON_MATCHES, GET_USER_CHALLENGES, or GET_USER_MARATHON_MATCHES |
| 9 | + * actions with the same UUID and a set of authorization, filtering, and |
| 10 | + * pagination options. Received challenges will be merged into the array of |
| 11 | + * challenges stored in the state, with some filtering options appended to the |
| 12 | + * challenge objects (so that we can filter them again at the frontend side: |
| 13 | + * challenge objects received from the backend do not include some of the |
| 14 | + * necessary data, like groupIds, lists of participating users, etc). |
| 15 | + * |
| 16 | + * RESET action allows to remove all loaded challenges and cancel any pending |
| 17 | + * requests to load challenges (removing an UUID from the set of pending |
| 18 | + * requests results in ignoring the response for that request). |
| 19 | + * |
| 20 | + * The backend includes into each response the total count of challenges |
| 21 | + * matching the specified filtering options (the actual number of challenge |
| 22 | + * objects included into the response might be smaller, due to the pagination |
| 23 | + * params). If "count" argument was provided in the dispatched action, |
| 24 | + * the total count of matching challenges from the response will be written |
| 25 | + * into a special map of counts in the Redux state. |
3 | 26 | */
|
4 | 27 |
|
5 |
| -import _ from 'lodash'; |
| 28 | +import logger from 'utils/logger'; |
6 | 29 | import { createActions } from 'redux-actions';
|
7 |
| -// import { getService } from 'services/challenges'; |
| 30 | +import { getService } from 'services/challenges'; |
| 31 | + |
| 32 | +/** |
| 33 | + * Private. Common processing of promises returned from ChallengesService. |
| 34 | + * @param {Object} promise |
| 35 | + * @param {String} uuid |
| 36 | + * @param {Object} filters |
| 37 | + * @param {Object} countCategory |
| 38 | + * @param {String} user |
| 39 | + * @return {Promise} |
| 40 | + */ |
| 41 | +function handle(promise, uuid, filters, countCategory, user) { |
| 42 | + return promise.catch((error) => { |
| 43 | + logger.error(error); |
| 44 | + return { |
| 45 | + challenges: [], |
| 46 | + totalCount: 0, |
| 47 | + }; |
| 48 | + }).then(res => ({ |
| 49 | + challenges: res.challenges, |
| 50 | + filters, |
| 51 | + totalCount: countCategory ? { |
| 52 | + category: countCategory, |
| 53 | + value: res.totalCount, |
| 54 | + } : null, |
| 55 | + user: user || null, |
| 56 | + uuid, |
| 57 | + })); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Gets a portion of challenges from the backend. |
| 62 | + * @param {String} uuid Should match an UUID stored into the state by |
| 63 | + * a previously dispatched GET_INIT action. Reducer will ignore the challenges |
| 64 | + * loaded by this action, if the UUID has already been removed from the set of |
| 65 | + * UUIDs of pending fetch challenge actions. Also, once the action results are |
| 66 | + * processed, its UUID is removed from the set of pending action UUIDs. |
| 67 | + * @param {Object} filters Optional. An object with filters to pass to the |
| 68 | + * backend. |
| 69 | + * @param {Object} params Optional. An object with params to pass to the backend |
| 70 | + * (except of the filter param, which is set by the previous argument). |
| 71 | + * @param {String} token Optional. Auth token for Topcoder API v3. Some of the |
| 72 | + * challenges are visible only to the properly authenticated and authorized |
| 73 | + * users. With this argument omitted you will fetch only public challenges. |
| 74 | + * @param {String} countCategory Optional. Specifies the category whereh the |
| 75 | + * total count of challenges returned by this request should be written. |
| 76 | + * @param {String} user Optional. User handle. If specified, only challenges |
| 77 | + * where this user has some role are loaded. |
| 78 | + * @return {Promise} |
| 79 | + */ |
| 80 | +function getChallenges(uuid, filters, params, token, countCategory, user) { |
| 81 | + const service = getService(token); |
| 82 | + const promise = user ? |
| 83 | + service.getUserChallenges(user, filters, params) : |
| 84 | + service.getChallenges(filters, params); |
| 85 | + return handle(promise, uuid, filters, countCategory, user); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Writes specified UUID into the set of pending requests to load challenges. |
| 90 | + * This allows (1) to understand whether we are waiting to load any challenges; |
| 91 | + * (2) to cancel pending request by removing UUID from the set. |
| 92 | + * @param {String} uuid |
| 93 | + */ |
| 94 | +function getInit(uuid) { |
| 95 | + return uuid; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Gets a portion of marathon matches from the backend. Parameters are the same |
| 100 | + * as for getChallenges() function. |
| 101 | + * @param {String} uuid |
| 102 | + * @param {Object} filters |
| 103 | + * @param {Object} params |
| 104 | + * @param {String} token |
| 105 | + * @param {String} countCategory |
| 106 | + * @param {String} user Optional. User handle. If specified, only challenges |
| 107 | + * where this user has some role are loaded. |
| 108 | + * @param {Promise} |
| 109 | + */ |
| 110 | +function getMarathonMatches(uuid, filters, params, token, countCategory, user) { |
| 111 | + const service = getService(token); |
| 112 | + const promise = user ? |
| 113 | + service.getUserMarathonMatches(user, filters, params) : |
| 114 | + service.getMarathonMatches(filters, params); |
| 115 | + return handle(promise, uuid, filters, countCategory, user); |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * This action tells Redux to remove all loaded challenges and to cancel |
| 120 | + * any pending requests to load more challenges. |
| 121 | + */ |
| 122 | +function reset() { |
| 123 | + return undefined; |
| 124 | +} |
8 | 125 |
|
9 | 126 | export default createActions({
|
10 | 127 | CHALLENGE_LISTING: {
|
11 |
| - REMOVE_ALL_CHALLENGES: _.noop, |
| 128 | + GET_CHALLENGES: getChallenges, |
| 129 | + GET_INIT: getInit, |
| 130 | + GET_MARATHON_MATCHES: getMarathonMatches, |
| 131 | + RESET: reset, |
12 | 132 | },
|
13 | 133 | });
|
0 commit comments