Skip to content

Commit 32ffdd4

Browse files
committed
1 parent 820d8ca commit 32ffdd4

File tree

7 files changed

+58
-50
lines changed

7 files changed

+58
-50
lines changed

src/components/Authentication/index.jsx

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/components/LoadingIndicator/index.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import "./styles.module.scss";
1010

1111
const LoadingIndicator = ({ error }) => {
1212
return (
13-
<div styleName="loading-indicator">{!error ? "Loading..." : _.get(error, 'response.data.message') || error}</div>
13+
<div styleName="loading-indicator">
14+
{!error ? "Loading..." : _.get(error, "response.data.message") || error}
15+
</div>
1416
);
1517
};
1618

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: 7 additions & 4 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" auth/>
17+
<MyTeamsList path="/taas/myteams" auth />
1818
<MyTeamsDetails path="/taas/myteams/:teamId" auth />
19-
<PositionDetails path="/taas/myteams/:teamId/positions/:positionId" 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +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 Authentication from '../../components/Authentication'
17+
import withAuthentication from "../../hoc/withAuthentication";
1818
import { useAsync } from "react-use";
1919

2020
const MyTeamsDetails = ({ teamId }) => {
@@ -40,4 +40,4 @@ MyTeamsDetails.propTypes = {
4040
teamId: PT.string,
4141
};
4242

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

src/routes/MyTeamsList/index.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +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 Authentication from '../../components/Authentication'
16+
import withAuthentication from "../../hoc/withAuthentication";
1717
import { useDebounce } from "react-use";
1818
import { TEAMS_PER_PAGE } from "constants";
1919
import "./styles.module.scss";
@@ -99,4 +99,4 @@ const MyTeamsList = () => {
9999
);
100100
};
101101

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

src/routes/PositionDetails/index.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +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 Authentication from '../../components/Authentication'
12+
import withAuthentication from "../../hoc/withAuthentication";
1313
import PositionCandidates from "./components/PositionCandidates";
1414
import CandidatesStatusFilter from "./components/CandidatesStatusFilter";
1515
import { useTeamPositionsState } from "./hooks/useTeamPositionsState";
@@ -62,4 +62,4 @@ PositionDetails.propTypes = {
6262
positionId: PT.string,
6363
};
6464

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

0 commit comments

Comments
 (0)