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

Commit 8a68e70

Browse files
authored
Merge pull request #47 from yoution/feature/issue-44
fix: error message for #44
2 parents a2657fc + 32ffdd4 commit 8a68e70

File tree

6 files changed

+63
-12
lines changed

6 files changed

+63
-12
lines changed

src/components/LoadingIndicator/index.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
* Optionally shows error.
55
*/
66
import React from "react";
7+
import _ from "lodash";
78
import PT from "prop-types";
89
import "./styles.module.scss";
910

1011
const LoadingIndicator = ({ error }) => {
1112
return (
12-
<div styleName="loading-indicator">{!error ? "Loading..." : error}</div>
13+
<div styleName="loading-indicator">
14+
{!error ? "Loading..." : _.get(error, "response.data.message") || error}
15+
</div>
1316
);
1417
};
1518

1619
LoadingIndicator.propTypes = {
17-
error: PT.string,
20+
error: PT.object,
1821
};
1922

2023
export default LoadingIndicator;

src/hoc/withAuthentication.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Authentication
3+
*
4+
* wrap component for authentication
5+
*/
6+
import React, { useCallback, useState, useEffect } from "react";
7+
import { getAuthUserTokens, login } from "@topcoder/micro-frontends-navbar-app";
8+
import LoadingIndicator from "../components/LoadingIndicator";
9+
10+
export default function withAuthentication(Component) {
11+
const AuthenticatedComponent = (props) => {
12+
let [isLoggedIn, setIsLoggedIn] = useState(null);
13+
let [authError, setAuthError] = useState(false);
14+
15+
useEffect(() => {
16+
if (props.auth) {
17+
getAuthUserTokens()
18+
.then(({ tokenV3 }) => {
19+
if (!!tokenV3) {
20+
setIsLoggedIn(!!tokenV3);
21+
} else {
22+
login();
23+
}
24+
})
25+
.catch((err) => {
26+
setAuthError(err);
27+
});
28+
}
29+
}, [props.auth]);
30+
31+
return (
32+
<>
33+
{authError && <LoadingIndicator error={authError.toString()} />}
34+
{!props.auth || (props.auth && isLoggedIn === true) ? (
35+
<Component {...props} />
36+
) : null}
37+
</>
38+
);
39+
};
40+
41+
return AuthenticatedComponent;
42+
}

src/root.component.jsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ import { Router } from "@reach/router";
44
import MyTeamsList from "./routes/MyTeamsList";
55
import MyTeamsDetails from "./routes/MyTeamsDetails";
66
import PositionDetails from "./routes/PositionDetails";
7-
import ReduxToastr from 'react-redux-toastr'
7+
import ReduxToastr from "react-redux-toastr";
88
import store from "./store";
99
import "./styles/main.vendor.scss";
1010
import styles from "./styles/main.module.scss";
1111

1212
export default function Root() {
1313
return (
14-
<div className={styles['topcoder-micro-frontends-teams-app']}>
14+
<div className={styles["topcoder-micro-frontends-teams-app"]}>
1515
<Provider store={store}>
1616
<Router>
17-
<MyTeamsList path="/taas/myteams" />
18-
<MyTeamsDetails path="/taas/myteams/:teamId" />
19-
<PositionDetails path="/taas/myteams/:teamId/positions/:positionId" />
17+
<MyTeamsList path="/taas/myteams" auth />
18+
<MyTeamsDetails path="/taas/myteams/:teamId" auth />
19+
<PositionDetails
20+
path="/taas/myteams/:teamId/positions/:positionId"
21+
auth
22+
/>
2023
</Router>
2124

2225
{/* Global config for Toastr popups */}

src/routes/MyTeamsDetails/index.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import LoadingIndicator from "components/LoadingIndicator";
1414
import TeamSummary from "./components/TeamSummary";
1515
import TeamMembers from "./components/TeamMembers";
1616
import TeamPositions from "./components/TeamPositions";
17+
import withAuthentication from "../../hoc/withAuthentication";
1718
import { useAsync } from "react-use";
1819

1920
const MyTeamsDetails = ({ teamId }) => {
@@ -22,7 +23,7 @@ const MyTeamsDetails = ({ teamId }) => {
2223
return (
2324
<LayoutContainer>
2425
{!team ? (
25-
<LoadingIndicator error={loadingError && loadingError.toString()} />
26+
<LoadingIndicator error={loadingError} />
2627
) : (
2728
<>
2829
<PageHeader title={team.name} backTo="/taas/myteams" />
@@ -39,4 +40,4 @@ MyTeamsDetails.propTypes = {
3940
teamId: PT.string,
4041
};
4142

42-
export default MyTeamsDetails;
43+
export default withAuthentication(MyTeamsDetails);

src/routes/MyTeamsList/index.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getMyTeams } from "../../services/teams";
1313
import TeamCard from "./components/TeamCard";
1414
import TeamCardGrid from "./components/TeamCardGrid";
1515
import LoadingIndicator from "../../components/LoadingIndicator";
16+
import withAuthentication from "../../hoc/withAuthentication";
1617
import { useDebounce } from "react-use";
1718
import { TEAMS_PER_PAGE } from "constants";
1819
import "./styles.module.scss";
@@ -74,7 +75,7 @@ const MyTeamsList = () => {
7475
<div styleName="empty">No teams found</div>
7576
)}
7677
{!myTeams ? (
77-
<LoadingIndicator error={loadingError && loadingError.toString()} />
78+
<LoadingIndicator error={loadingError} />
7879
) : (
7980
<>
8081
<TeamCardGrid>
@@ -98,4 +99,4 @@ const MyTeamsList = () => {
9899
);
99100
};
100101

101-
export default MyTeamsList;
102+
export default withAuthentication(MyTeamsList);

src/routes/PositionDetails/index.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import LayoutContainer from "components/LayoutContainer";
99
import LoadingIndicator from "components/LoadingIndicator";
1010
import PageHeader from "components/PageHeader";
1111
import { CANDIDATE_STATUS } from "constants";
12+
import withAuthentication from "../../hoc/withAuthentication";
1213
import PositionCandidates from "./components/PositionCandidates";
1314
import CandidatesStatusFilter from "./components/CandidatesStatusFilter";
1415
import { useTeamPositionsState } from "./hooks/useTeamPositionsState";
@@ -61,4 +62,4 @@ PositionDetails.propTypes = {
6162
positionId: PT.string,
6263
};
6364

64-
export default PositionDetails;
65+
export default withAuthentication(PositionDetails);

0 commit comments

Comments
 (0)