Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 6b05bfd

Browse files
committed
fix: issue #16
1 parent 2f87b41 commit 6b05bfd

File tree

7 files changed

+65
-64
lines changed

7 files changed

+65
-64
lines changed

package-lock.json

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

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"dependencies": {
5757
"@popperjs/core": "^2.5.4",
5858
"@reach/router": "^1.3.4",
59+
"@topcoder-platform/tc-auth-lib": "git+https://github.com/topcoder-platform/tc-auth-lib.git#1.0.4",
5960
"axios": "^0.21.0",
6061
"classnames": "^2.2.6",
6162
"express": "^4.17.1",

src/routes/MyTeamsDetails/index.jsx

+1-6
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@ import TeamSummary from "./components/TeamSummary";
1515
import TeamMembers from "./components/TeamMembers";
1616
import TeamPositions from "./components/TeamPositions";
1717
import { useAsync } from "react-use";
18-
import {
19-
getAuthUserTokens,
20-
} from "@topcoder/micro-frontends-navbar-app";
2118

2219
const MyTeamsDetails = ({ teamId }) => {
23-
const authUserTokens = useAsync(getAuthUserTokens);
24-
const tokenV3 = authUserTokens.value ? authUserTokens.value.tokenV3 : null;
25-
const [team, loadingError] = useData(getTeamById, tokenV3, teamId);
20+
const [team, loadingError] = useData(getTeamById, teamId);
2621

2722
return (
2823
<LayoutContainer>

src/routes/MyTeamsList/index.jsx

+1-6
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,9 @@ import TeamCard from "./components/TeamCard";
1212
import TeamCardGrid from "./components/TeamCardGrid";
1313
import LoadingIndicator from "../../components/LoadingIndicator";
1414
import { useAsync } from "react-use";
15-
import {
16-
getAuthUserTokens,
17-
} from "@topcoder/micro-frontends-navbar-app";
1815

1916
const MyTeamsList = () => {
20-
const authUserTokens = useAsync(getAuthUserTokens);
21-
const tokenV3 = authUserTokens.value ? authUserTokens.value.tokenV3 : null;
22-
const [myTeams, loadingError] = useData(getMyTeams, tokenV3);
17+
const [myTeams, loadingError] = useData(getMyTeams);
2318

2419
return (
2520
<LayoutContainer>

src/routes/PositionDetails/actions/index.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Position Details page actions
33
*/
44
import { getPositionDetails, patchPositionCandidate } from "services/teams";
5-
import { getAuthUserTokens } from "@topcoder/micro-frontends-navbar-app";
65
import { ACTION_TYPE } from "constants";
76

87
/**
@@ -16,12 +15,7 @@ import { ACTION_TYPE } from "constants";
1615
export const loadPosition = (teamId, positionId) => ({
1716
type: ACTION_TYPE.LOAD_POSITION,
1817
payload: async () => {
19-
const tokens = await getAuthUserTokens();
20-
const response = await getPositionDetails(
21-
tokens.tokenV3,
22-
teamId,
23-
positionId
24-
);
18+
const response = await getPositionDetails(teamId, positionId);
2519

2620
return response.data;
2721
},
@@ -42,9 +36,7 @@ export const loadPosition = (teamId, positionId) => ({
4236
export const updateCandidate = (candidateId, partialCandidateData) => ({
4337
type: ACTION_TYPE.UPDATE_CANDIDATE,
4438
payload: async () => {
45-
const tokens = await getAuthUserTokens();
4639
const response = await patchPositionCandidate(
47-
tokens.tokenV3,
4840
candidateId,
4941
partialCandidateData
5042
);
@@ -61,4 +53,4 @@ export const updateCandidate = (candidateId, partialCandidateData) => ({
6153
*/
6254
export const resetPositionState = () => ({
6355
type: ACTION_TYPE.RESET_POSITION_STATE,
64-
})
56+
});

src/services/requestInterceptor.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import axios from "axios";
2+
import store from "../store";
3+
import { getFreshToken, isTokenExpired } from "@topcoder-platform/tc-auth-lib";
4+
import { getAuthUserTokens } from "@topcoder/micro-frontends-navbar-app";
5+
6+
export const getToken = () => {
7+
return new Promise(async (resolve, reject) => {
8+
const authUserTokens = await getAuthUserTokens();
9+
const token = authUserTokens ? authUserTokens.tokenV3 : null;
10+
if (token && !isTokenExpired(token)) {
11+
return resolve(token);
12+
} else {
13+
return getFreshToken()
14+
.then((token) => {
15+
resolve(token);
16+
})
17+
.catch((err) => {
18+
console.log(err);
19+
reject(err);
20+
});
21+
}
22+
});
23+
};
24+
25+
export const axiosInstance = axios.create({
26+
headers: {
27+
"Content-Type": "application/json",
28+
},
29+
});
30+
31+
// request interceptor to pass auth token
32+
axiosInstance.interceptors.request.use((config) => {
33+
return getToken()
34+
.then((token) => {
35+
config.headers["Authorization"] = `Bearer ${token}`;
36+
return config;
37+
})
38+
.catch((err) => {
39+
// TODO handle this error somehow
40+
console.log(err);
41+
return config;
42+
});
43+
});

src/services/teams.js

+10-42
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,52 @@
11
/**
22
* Topcoder TaaS Service
33
*/
4-
import axios from "axios";
4+
import { axiosInstance as axios } from "./requestInterceptor";
55
import config from "../../config";
66

77
/**
88
* Get my teams.
99
*
10-
* @param {string} tokenV3 login token
11-
*
1210
* @returns {Promise<object[]>} list of teams
1311
*/
14-
export const getMyTeams = (tokenV3) => {
15-
if (!tokenV3) {
16-
return Promise.resolve({
17-
data: null,
18-
});
19-
}
20-
return axios.get(`${config.API.V5}/taas-teams`, {
21-
headers: { Authorization: `Bearer ${tokenV3}` },
22-
});
12+
export const getMyTeams = () => {
13+
debugger;
14+
return axios.get(`${config.API.V5}/taas-teams`);
2315
};
2416

2517
/**
2618
* Get team by id.
2719
*
28-
* @param {string} tokenV3 login token
2920
* @param {string|number} teamId team id
3021
*
3122
* @returns {Promise<{}>} team object
3223
*/
33-
export const getTeamById = (tokenV3, teamId) => {
34-
if (!tokenV3) {
35-
return Promise.resolve({
36-
data: null,
37-
});
38-
}
39-
return axios.get(`${config.API.V5}/taas-teams/${teamId}`, {
40-
headers: { Authorization: `Bearer ${tokenV3}` },
41-
});
24+
export const getTeamById = (teamId) => {
25+
return axios.get(`${config.API.V5}/taas-teams/${teamId}`);
4226
};
4327

4428
/**
4529
* Get team position details.
4630
*
47-
* @param {string} tokenV3 login token
4831
* @param {string|number} teamId team id
4932
* @param {string|number} positionId position id
5033
*
5134
* @returns {Promise<object{}>} job object
5235
*/
53-
export const getPositionDetails = (tokenV3, teamId, positionId) => {
54-
if (!tokenV3) {
55-
return Promise.resolve({
56-
data: null,
57-
});
58-
}
59-
return axios.get(`${config.API.V5}/taas-teams/${teamId}/jobs/${positionId}`, {
60-
headers: { Authorization: `Bearer ${tokenV3}` },
61-
});
36+
export const getPositionDetails = (teamId, positionId) => {
37+
return axios.get(`${config.API.V5}/taas-teams/${teamId}/jobs/${positionId}`);
6238
};
6339

6440
/**
6541
* Patch Position Candidate
6642
*
67-
* @param {string} tokenV3 login token
6843
* @param {string} candidateId position candidate id
6944
*
7045
* @returns {Promise<object{}>} position candidate
7146
*/
72-
export const patchPositionCandidate = (
73-
tokenV3,
74-
candidateId,
75-
partialCandidateData
76-
) => {
47+
export const patchPositionCandidate = (candidateId, partialCandidateData) => {
7748
return axios.patch(
7849
`${config.API.V5}/jobCandidates/${candidateId}`,
79-
partialCandidateData,
80-
{
81-
headers: { Authorization: `Bearer ${tokenV3}` },
82-
}
50+
partialCandidateData
8351
);
8452
};

0 commit comments

Comments
 (0)