Skip to content

Commit 9b3da5d

Browse files
author
Huan Li
committed
Remove V2 API from Dashboard and Profile Page
1 parent de547d8 commit 9b3da5d

File tree

10 files changed

+95
-34
lines changed

10 files changed

+95
-34
lines changed

__tests__/__snapshots__/index.js.snap

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Object {
6969
"dropAll": [Function],
7070
"getAchievementsDone": [Function],
7171
"getAchievementsInit": [Function],
72+
"getAchievementsV3Done": [Function],
7273
"getActiveChallengesDone": [Function],
7374
"getActiveChallengesInit": [Function],
7475
"getFinancesDone": [Function],
@@ -98,6 +99,7 @@ Object {
9899
"deleteWebLinkInit": [Function],
99100
"getAchievementsDone": [Function],
100101
"getAchievementsInit": [Function],
102+
"getAchievementsV3Done": [Function],
101103
"getCredentialDone": [Function],
102104
"getCredentialInit": [Function],
103105
"getEmailPreferencesDone": [Function],
@@ -206,14 +208,17 @@ Object {
206208
"countReset": [Function],
207209
"debug": [Function],
208210
"dir": [Function],
211+
"dirxml": [Function],
209212
"error": [Function],
210213
"group": [Function],
211214
"groupCollapsed": [Function],
212215
"groupEnd": [Function],
213216
"info": [Function],
214217
"log": [Function],
218+
"table": [Function],
215219
"time": [Function],
216220
"timeEnd": [Function],
221+
"timeLog": [Function],
217222
"trace": [Function],
218223
"warn": [Function],
219224
},
@@ -318,6 +323,7 @@ Object {
318323
"Spec Review": "Specification Review",
319324
},
320325
"getApiResponsePayload": [Function],
326+
"getApiResponsePayloadV3": [Function],
321327
"getLookerApiResponsePayload": [Function],
322328
},
323329
"time": Object {

__tests__/actions/__snapshots__/profile.js.snap

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Object {
1414
"deleteWebLinkInit": [Function],
1515
"getAchievementsDone": [Function],
1616
"getAchievementsInit": [Function],
17+
"getAchievementsV3Done": [Function],
1718
"getCredentialDone": [Function],
1819
"getCredentialInit": [Function],
1920
"getEmailPreferencesDone": [Function],

package-lock.json

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

src/actions/members.js

+18
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ async function getAchievementsDone(handle, uuid) {
5757
return { data, handle, uuid };
5858
}
5959

60+
/**
61+
* @static
62+
* @desc Creates an action that loads member achievements from v3 API.
63+
* @param {String} handle Member handle.
64+
* @param {String} uuid Loading operation
65+
* @return {Action}
66+
*/
67+
async function getAchievementsV3Done(handle, uuid) {
68+
let data;
69+
try {
70+
data = await getUserService().getAchievementsV3(handle);
71+
} catch (e) {
72+
data = [];
73+
}
74+
return { data, handle, uuid };
75+
}
76+
6077
/**
6178
* @static
6279
* @desc Creates an action that signals beginning of loading the member's
@@ -344,6 +361,7 @@ export default createActions({
344361
DROP_ALL: dropAll,
345362
GET_ACHIEVEMENTS_INIT: getAchievementsInit,
346363
GET_ACHIEVEMENTS_DONE: getAchievementsDone,
364+
GET_ACHIEVEMENTS_V3_DONE: getAchievementsV3Done,
347365
GET_FINANCES_INIT: getFinancesInit,
348366
GET_FINANCES_DONE: getFinancesDone,
349367
GET_STATS_INIT: getStatsInit,

src/actions/profile.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function getAchievementsInit() {}
3131

3232
/**
3333
* @static
34-
* @desc Creates an action that loads user achievements.
34+
* @desc Creates an action that loads user achievements from API v2.
3535
* @todo This duplicates similar action in {@link actions.members.md}!
3636
* @param {String} handle User handle.
3737
* @return {Action}
@@ -40,6 +40,17 @@ function getAchievementsDone(handle) {
4040
return getUserService().getUserPublic(handle);
4141
}
4242

43+
/**
44+
* @static
45+
* @desc Creates an action that loads user achievements from API v3.
46+
* @todo This duplicates similar action in {@link actions.members.md}!
47+
* @param {String} handle User handle.
48+
* @return {Action}
49+
*/
50+
function getAchievementsV3Done(handle) {
51+
return getUserService().getUserPublicV3(handle);
52+
}
53+
4354
/**
4455
* @static
4556
* @todo To be documented.
@@ -450,6 +461,7 @@ export default createActions({
450461
CLEAR_PROFILE: _.noop,
451462
GET_ACHIEVEMENTS_INIT: getAchievementsInit,
452463
GET_ACHIEVEMENTS_DONE: getAchievementsDone,
464+
GET_ACHIEVEMENTS_V3_DONE: getAchievementsV3Done,
453465
GET_EXTERNAL_ACCOUNTS_INIT: getExternalAccountsInit,
454466
GET_EXTERNAL_ACCOUNTS_DONE: getExternalAccountsDone,
455467
GET_EXTERNAL_LINKS_INIT: getExternalLinksInit,

src/actions/settings.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import { getService } from '../services/user-traits';
1010
/**
1111
* @static
1212
* @desc Creates an action that loads user's all traits.
13-
* @param {String} uuid Operation UUID.
13+
* @param {String} handle the topcoder member handle
1414
* @param {String} tokenV3 v3 auth token.
1515
* @return {Action}
1616
*/
17-
1817
async function getAllUserTraits(handle, tokenV3) {
1918
const data = await getService(tokenV3).getAllUserTraits(handle);
2019
return { data, handle };

src/reducers/members.js

+1
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ function create(initialState = {}) {
438438
[a.dropAll]: onDropAll,
439439
[a.getAchievementsInit]: onGetAchievementsInit,
440440
[a.getAchievementsDone]: onGetAchievementsDone,
441+
[a.getAchievementsV3Done]: onGetAchievementsDone,
441442
[a.getFinancesInit]: onGetFinancesInit,
442443
[a.getFinancesDone]: onGetFinancesDone,
443444
[a.getStatsInit]: onGetStatsInit,

src/reducers/profile.js

+21
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ function onGetAchievementsDone(state, { payload, error }) {
2929
});
3030
}
3131

32+
/**
33+
* Handles PROFILE/GET_ACHIEVEMENTS_V3_DONE action.
34+
* @param {Object} state
35+
* @param {Object} action Payload will be JSON from api call
36+
* @return {Object} New state
37+
*/
38+
function onGetAchievementsV3Done(state, { payload, error }) {
39+
if (error) {
40+
return { ...state, loadingError: true };
41+
}
42+
43+
return ({
44+
...state,
45+
achievements: payload.achievements || [],
46+
copilot: payload.copilot,
47+
country: payload.country,
48+
loadingError: false,
49+
});
50+
}
51+
3252
/**
3353
* Handles PROFILE/GET_EXTERNAL_ACCOUNTS_DONE action.
3454
* @param {Object} state
@@ -490,6 +510,7 @@ function create(initialState) {
490510
[a.loadProfile]: (state, action) => ({ ...state, profileForHandle: action.payload }),
491511
[a.getAchievementsInit]: state => state,
492512
[a.getAchievementsDone]: onGetAchievementsDone,
513+
[a.getAchievementsV3Done]: onGetAchievementsV3Done,
493514
[a.getExternalAccountsInit]: state => state,
494515
[a.getExternalAccountsDone]: onGetExternalAccountsDone,
495516
[a.getExternalLinksInit]: state => state,

src/services/user.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { config, isomorphy } from 'topcoder-react-utils';
77

88
import logger from '../utils/logger';
9-
import { getApiResponsePayload } from '../utils/tc';
9+
import { getApiResponsePayload, getApiResponsePayloadV3 } from '../utils/tc';
1010
import { getApi } from './api';
1111

1212
let auth0;
@@ -141,6 +141,16 @@ class User {
141141
return res.json() || null;
142142
}
143143

144+
/**
145+
* Gets public user info from v3 API. Does not need auth.
146+
* @param {String} username
147+
* @return {Object}
148+
*/
149+
async getUserPublicV3(username) {
150+
const res = await this.private.api.get(`/members/${username}`);
151+
return getApiResponsePayloadV3(res);
152+
}
153+
144154
/**
145155
* Gets user data object for the specified username.
146156
*

src/utils/tc.js

+12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ export async function getApiResponsePayload(res) {
3838
return x.content;
3939
}
4040

41+
/**
42+
* Gets payload from a standard success response from TC API v3; or throws
43+
* an error in case of a failure response.
44+
* @param {Object} res
45+
* @return {Promise} Resolves to the payload.
46+
*/
47+
export async function getApiResponsePayloadV3(res) {
48+
if (!res.ok) throw new Error(res.statusText);
49+
const x = (await res.json()).result;
50+
if (!x.success) throw new Error(x.content);
51+
return x.content;
52+
}
4153

4254
/**
4355
* Gets payload from a standard success response from TC LOOKER API; or throws

0 commit comments

Comments
 (0)