Skip to content

Commit 0be139e

Browse files
committedOct 9, 2019
Fix for issue #3351
1 parent 37a91ef commit 0be139e

File tree

13 files changed

+209
-72
lines changed

13 files changed

+209
-72
lines changed
 

‎__tests__/__snapshots__/index.js.snap

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Object {
5858
"getLookerDone": [Function],
5959
},
6060
"lookup": Object {
61+
"getAllCountriesDone": [Function],
62+
"getAllCountriesInit": [Function],
6163
"getCountriesDone": [Function],
6264
"getCountriesInit": [Function],
6365
"getSkillTagsDone": [Function],

‎__tests__/actions/__snapshots__/lookup.js.snap

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
exports[`Module exports 1`] = `
44
Object {
55
"lookup": Object {
6+
"getAllCountriesDone": [Function],
7+
"getAllCountriesInit": [Function],
68
"getCountriesDone": [Function],
79
"getCountriesInit": [Function],
810
"getSkillTagsDone": [Function],

‎__tests__/reducers/__snapshots__/lookup.js.snap

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
exports[`Default reducer Get countries 1`] = `
44
Object {
5+
"allCountries": Array [],
56
"countries": Array [
67
Object {
78
"country": "Afghanistan",
@@ -23,6 +24,7 @@ Object {
2324

2425
exports[`Default reducer Get countries error 1`] = `
2526
Object {
27+
"allCountries": Array [],
2628
"countries": Array [
2729
Object {
2830
"country": "Afghanistan",
@@ -44,6 +46,7 @@ Object {
4446

4547
exports[`Default reducer Get skill tags 1`] = `
4648
Object {
49+
"allCountries": Array [],
4750
"countries": Array [],
4851
"loadingSkillTagsError": false,
4952
"skillTags": Array [
@@ -59,6 +62,7 @@ Object {
5962

6063
exports[`Default reducer Get skill tags error 1`] = `
6164
Object {
65+
"allCountries": Array [],
6266
"countries": Array [],
6367
"loadingSkillTagsError": true,
6468
"skillTags": Array [
@@ -74,13 +78,15 @@ Object {
7478

7579
exports[`Default reducer Initial state 1`] = `
7680
Object {
81+
"allCountries": Array [],
7782
"countries": Array [],
7883
"skillTags": Array [],
7984
}
8085
`;
8186

8287
exports[`Factory without server side rendering Get countries 1`] = `
8388
Object {
89+
"allCountries": Array [],
8490
"countries": Array [
8591
Object {
8692
"country": "Afghanistan",
@@ -102,6 +108,7 @@ Object {
102108

103109
exports[`Factory without server side rendering Get countries error 1`] = `
104110
Object {
111+
"allCountries": Array [],
105112
"countries": Array [
106113
Object {
107114
"country": "Afghanistan",
@@ -123,6 +130,7 @@ Object {
123130

124131
exports[`Factory without server side rendering Get skill tags 1`] = `
125132
Object {
133+
"allCountries": Array [],
126134
"countries": Array [],
127135
"loadingSkillTagsError": false,
128136
"skillTags": Array [
@@ -138,6 +146,7 @@ Object {
138146

139147
exports[`Factory without server side rendering Get skill tags error 1`] = `
140148
Object {
149+
"allCountries": Array [],
141150
"countries": Array [],
142151
"loadingSkillTagsError": true,
143152
"skillTags": Array [
@@ -153,6 +162,7 @@ Object {
153162

154163
exports[`Factory without server side rendering Initial state 1`] = `
155164
Object {
165+
"allCountries": Array [],
156166
"countries": Array [],
157167
"skillTags": Array [],
158168
}

‎dist/dev/index.js

+57-57
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/actions.lookup.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,34 @@ Actions related to lookup data.
99
* [.getSkillTagsDone()](#module_actions.lookup.getSkillTagsDone) ⇒ <code>Action</code>
1010
* [.getCountriesInit()](#module_actions.lookup.getCountriesInit) ⇒ <code>Action</code>
1111
* [.getCountriesDone()](#module_actions.lookup.getCountriesDone) ⇒ <code>Action</code>
12+
* [.getAllCountriesInit()](#module_actions.lookup.getCountriesInit) ⇒ <code>Action</code>
13+
* [.getAllCountriesDone(tokenV3)](#module_actions.lookup.getCountriesDone) ⇒ <code>Action</code>
1214

1315
<a name="module_actions.lookup.getSkillTagsInit"></a>
1416

1517
### actions.lookup.getSkillTagsInit() ⇒ <code>Action</code>
1618
Creates an action that signals beginning of getting all skill tags.
1719

18-
**Kind**: static method of [<code>actions.lookup</code>](#module_actions.lookup)
20+
**Kind**: static method of [<code>actions.lookup</code>](#module_actions.lookup)
1921
<a name="module_actions.lookup.getSkillTagsDone"></a>
2022

2123
### actions.lookup.getSkillTagsDone() ⇒ <code>Action</code>
2224
Creates an action that gets all skill tags.
2325

24-
**Kind**: static method of [<code>actions.lookup</code>](#module_actions.lookup)
26+
**Kind**: static method of [<code>actions.lookup</code>](#module_actions.lookup)
2527
<a name="module_actions.lookup.getCountriesDone"></a>
2628

2729
### actions.lookup.getCountriesDone() ⇒ <code>Action</code>
2830
Creates an action that gets all countries.
2931

30-
**Kind**: static method of [<code>actions.lookup</code>](#module_actions.lookup)
32+
**Kind**: static method of [<code>actions.lookup</code>](#module_actions.lookup)
33+
<a name="module_actions.lookup.getCountriesDone"></a>
34+
35+
### actions.lookup.getAllCountriesDone(tokenV3) ⇒ <code>Action</code>
36+
Creates an action that gets all countries new version.
37+
38+
**Kind**: static method of [<code>actions.lookup</code>](#module_actions.lookup)
39+
40+
| Param | Type | Description |
41+
| --- | --- | --- |
42+
| tokenV3 | <code>String</code> | Topcoder v3 auth token. |

‎docs/services.members.md

+22-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ members via API V3.
1313
* [new MembersService(tokenV3)](#new_module_services.members..MembersService_new)
1414
* [.getMemberFinances(handle)](#module_services.members..MembersService+getMemberFinances) ⇒ <code>Promise</code>
1515
* [.getMemberInfo(handle)](#module_services.members..MembersService+getMemberInfo) ⇒ <code>Promise</code>
16+
* [.getListMemberInfo(members)](#module_services.members..MembersService+getListMemberInfo) ⇒ <code>Promise</code>
1617
* [.getExternalAccounts(handle)](#module_services.members..MembersService+getExternalAccounts) ⇒ <code>Promise</code>
1718
* [.getExternalLinks(handle)](#module_services.members..MembersService+getExternalLinks) ⇒ <code>Promise</code>
1819
* [.getSkills(handle)](#module_services.members..MembersService+getSkills) ⇒ <code>Promise</code>
@@ -53,6 +54,7 @@ Service class.
5354
* [new MembersService(tokenV3)](#new_module_services.members..MembersService_new)
5455
* [.getMemberFinances(handle)](#module_services.members..MembersService+getMemberFinances) ⇒ <code>Promise</code>
5556
* [.getMemberInfo(handle)](#module_services.members..MembersService+getMemberInfo) ⇒ <code>Promise</code>
57+
* [.getListMemberInfo(members)](#module_services.members..MembersService+getListMemberInfo) ⇒ <code>Promise</code>
5658
* [.getExternalAccounts(handle)](#module_services.members..MembersService+getExternalAccounts) ⇒ <code>Promise</code>
5759
* [.getExternalLinks(handle)](#module_services.members..MembersService+getExternalLinks) ⇒ <code>Promise</code>
5860
* [.getSkills(handle)](#module_services.members..MembersService+getSkills) ⇒ <code>Promise</code>
@@ -104,6 +106,18 @@ This method does not require any authorization.
104106
| --- | --- | --- |
105107
| handle | <code>String</code> | Member handle. |
106108

109+
#### membersService.getListMemberInfo(members) ⇒ <code>Promise</code>
110+
Gets public information on a list member.
111+
112+
This method does not require any authorization.
113+
114+
**Kind**: instance method of [<code>MembersService</code>](#module_services.members..MembersService)
115+
**Returns**: <code>Promise</code> - Resolves to the array.
116+
117+
| Param | Type | Description |
118+
| --- | --- | --- |
119+
| members | <code>Array</code> | Array of member info. |
120+
107121
<a name="module_services.members..MembersService+getExternalAccounts"></a>
108122

109123
#### membersService.getExternalAccounts(handle) ⇒ <code>Promise</code>
@@ -114,7 +128,7 @@ Gets member external account info.
114128

115129
| Param | Type |
116130
| --- | --- |
117-
| handle | <code>String</code> |
131+
| handle | <code>String</code> |
118132

119133
<a name="module_services.members..MembersService+getExternalLinks"></a>
120134

@@ -126,7 +140,7 @@ Gets member external links.
126140

127141
| Param | Type |
128142
| --- | --- |
129-
| handle | <code>String</code> |
143+
| handle | <code>String</code> |
130144

131145
<a name="module_services.members..MembersService+getSkills"></a>
132146

@@ -138,7 +152,7 @@ Gets member skills.
138152

139153
| Param | Type |
140154
| --- | --- |
141-
| handle | <code>String</code> |
155+
| handle | <code>String</code> |
142156

143157
<a name="module_services.members..MembersService+getStats"></a>
144158

@@ -150,7 +164,7 @@ Gets member statistics.
150164

151165
| Param | Type |
152166
| --- | --- |
153-
| handle | <code>String</code> |
167+
| handle | <code>String</code> |
154168

155169
<a name="module_services.members..MembersService+getStatsHistory"></a>
156170

@@ -162,7 +176,7 @@ Gets member statistics history
162176

163177
| Param | Type |
164178
| --- | --- |
165-
| handle | <code>String</code> |
179+
| handle | <code>String</code> |
166180

167181
<a name="module_services.members..MembersService+getStatsDistribution"></a>
168182

@@ -174,9 +188,9 @@ Gets member statistics distribution
174188

175189
| Param | Type |
176190
| --- | --- |
177-
| handle | <code>String</code> |
178-
| track | <code>String</code> |
179-
| subTrack | <code>String</code> |
191+
| handle | <code>String</code> |
192+
| track | <code>String</code> |
193+
| subTrack | <code>String</code> |
180194

181195
<a name="module_services.members..MembersService+getMemberSuggestions"></a>
182196

‎package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/actions/lookup.js

+19
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,30 @@ function getCountriesDone() {
4242
return getService().getCountries();
4343
}
4444

45+
/**
46+
* @static
47+
* @desc Creates an action that signals beginning of getting all countries api version 5.
48+
* @return {Action}
49+
*/
50+
function getAllCountriesInit() {}
51+
52+
/**
53+
* @static
54+
* @desc Creates an action that gets all countries api version 5.
55+
* @param {String} tokenV3 Optional. Auth token for Topcoder API v3.
56+
* @return {Action}
57+
*/
58+
function getAllCountriesDone(tokenV3) {
59+
return getService(tokenV3).getAllCountries();
60+
}
61+
4562
export default createActions({
4663
LOOKUP: {
4764
GET_SKILL_TAGS_INIT: getSkillTagsInit,
4865
GET_SKILL_TAGS_DONE: getSkillTagsDone,
4966
GET_COUNTRIES_INIT: getCountriesInit,
5067
GET_COUNTRIES_DONE: getCountriesDone,
68+
GET_ALL_COUNTRIES_INIT: getAllCountriesInit,
69+
GET_ALL_COUNTRIES_DONE: getAllCountriesDone,
5170
},
5271
});

‎src/reducers/lookup.js

+22
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ function onGetCountriesDone(state, { payload, error }) {
4848
});
4949
}
5050

51+
/**
52+
* Handles LOOKUP/GET_ALL_COUNTRIES_DONE action.
53+
* @param {Object} state
54+
* @param {Object} action Payload will be JSON from api call
55+
* @return {Object} New state
56+
*/
57+
function onGetAllCountriesDone(state, { payload, error }) {
58+
if (error) {
59+
logger.error('Failed to get all countries', payload);
60+
return { ...state, loadingAllCountriesError: true };
61+
}
62+
63+
return ({
64+
...state,
65+
loadingAllCountriesError: false,
66+
allCountries: payload,
67+
});
68+
}
69+
5170
/**
5271
* Creates a new Lookup reducer with the specified initial state.
5372
* @param {Object} initialState Optional. Initial state.
@@ -60,9 +79,12 @@ function create(initialState = {}) {
6079
[a.getSkillTagsDone]: onGetSkillTagsDone,
6180
[a.getCountriesInit]: state => state,
6281
[a.getCountriesDone]: onGetCountriesDone,
82+
[a.getAllCountriesInit]: state => state,
83+
[a.getAllCountriesDone]: onGetAllCountriesDone,
6384
}, _.defaults(initialState, {
6485
skillTags: [],
6586
countries: [],
87+
allCountries: [],
6688
}));
6789
}
6890

‎src/services/challenges.js

+13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import logger from '../utils/logger';
1212
import { setErrorIcon, ERROR_ICON_TYPES } from '../utils/errors';
1313
import { COMPETITION_TRACKS, getApiResponsePayload } from '../utils/tc';
1414
import { getApi } from './api';
15+
import { getService as getMembersService } from './members';
1516

1617
export const ORDER_BY = {
1718
SUBMISSION_END_DATE: 'submissionEndDate',
@@ -83,6 +84,15 @@ export function normalizeChallengeDetails(challenge, filtered, user, username) {
8384
registrants: challenge.registrants || [],
8485
};
8586

87+
88+
_.forEach(finalChallenge.registrants, (registrant) => {
89+
if (registrant.memberInfo) {
90+
const { homeCountryCode } = registrant.memberInfo;
91+
// eslint-disable-next-line no-param-reassign
92+
registrant.countryCode = homeCountryCode || registrant.memberInfo.competitionCountryCode;
93+
}
94+
});
95+
8696
// Winners have different field names, needs to be normalized to match `filtered` and `challenge`
8797
finalChallenge.winners = _.map(
8898
challenge.winners,
@@ -262,6 +272,7 @@ class ChallengesService {
262272
getChallenges,
263273
tokenV2,
264274
tokenV3,
275+
memberService: getMembersService(),
265276
};
266277
}
267278

@@ -373,6 +384,8 @@ class ChallengesService {
373384
const challengeUser = username && await this.getUserChallenges(username, { id: challengeId })
374385
.then(res => res.challenges[0]).catch(() => null);
375386

387+
await this.private.memberService.getListMemberInfo(challenge.registrants || [], false);
388+
376389
const finalChallenge = normalizeChallengeDetails(
377390
challenge,
378391
challengeFiltered,

‎src/services/lookup.js

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class LookupService {
1414
constructor(tokenV3) {
1515
this.private = {
1616
api: getApi('V3', tokenV3),
17+
apiV5: getApi('V5', tokenV3),
1718
tokenV3,
1819
};
1920
}
@@ -37,6 +38,16 @@ class LookupService {
3738
const res = await this.private.api.get('/members/lookup/countries');
3839
return getApiResponsePayload(res);
3940
}
41+
42+
/**
43+
* Gets all countries.
44+
* @return {Promise} Resolves to the countries.
45+
*/
46+
async getAllCountries() {
47+
const res = await this.private.apiV5.get('/lookups/countries');
48+
const jsonResult = await res.json();
49+
return jsonResult;
50+
}
4051
}
4152

4253
let lastInstance = null;

‎src/services/members.js

+19
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ class MembersService {
4848
return getApiResponsePayload(res);
4949
}
5050

51+
/**
52+
* Gets public information on a list member.
53+
*
54+
* This method does not require any authorization.
55+
*
56+
* @param {Array} members List member info.
57+
* @param {Boolean} shouldThrowError should throw error if request fail
58+
* @return {Promise} Resolves to the data object.
59+
*/
60+
async getListMemberInfo(members, shouldThrowError = true) {
61+
const getHandle = async (member) => {
62+
const result = await this.private.api.get(`/members/${member.handle}`);
63+
// eslint-disable-next-line no-param-reassign
64+
member.memberInfo = await getApiResponsePayload(result, shouldThrowError);
65+
};
66+
const apis = members.map(member => getHandle(member));
67+
await Promise.all(apis);
68+
}
69+
5170
/**
5271
* Gets member external account info.
5372
* @param {String} handle

‎src/utils/tc.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,25 @@ export const REVIEW_OPPORTUNITY_TYPES = {
2929
* Gets payload from a standard success response from TC API; or throws
3030
* an error in case of a failure response.
3131
* @param {Object} res
32+
* @param {Boolean} shouldThrowError should throw error if request fail
3233
* @return {Promise} Resolves to the payload.
3334
*/
34-
export async function getApiResponsePayload(res) {
35-
if (!res.ok) throw new Error(res.statusText);
35+
export async function getApiResponsePayload(res, shouldThrowError = true) {
36+
if (!res.ok) {
37+
if (shouldThrowError) {
38+
throw new Error(res.statusText);
39+
} else {
40+
return null;
41+
}
42+
}
3643
const x = (await res.json()).result;
37-
if (!x.success) throw new Error(x.content);
44+
if ((!x.success)) {
45+
if (shouldThrowError) {
46+
throw new Error(x.content);
47+
} else {
48+
return null;
49+
}
50+
}
3851
return x.content;
3952
}
4053

0 commit comments

Comments
 (0)
Please sign in to comment.