|
@@ -1,59 +1,57 @@
|
|
|
-import React, { Component } from 'react';
|
|
|
|
|
-import styled, { createGlobalStyle } from 'styled-components';
|
|
|
|
|
-import { BrowserRouter, Route, Redirect, Switch } from 'react-router-dom';
|
|
|
|
|
-import close from 'assets/close.png';
|
|
|
|
|
-
|
|
|
|
|
-import api from 'shared/api';
|
|
|
|
|
-import { Context } from 'shared/Context';
|
|
|
|
|
-
|
|
|
|
|
-import Login from './Login';
|
|
|
|
|
-import Register from './Register';
|
|
|
|
|
-import CurrentError from './CurrentError';
|
|
|
|
|
-import Home from './home/Home';
|
|
|
|
|
-import Loading from 'components/Loading';
|
|
|
|
|
-import PorterUrls from 'shared/urls';
|
|
|
|
|
-
|
|
|
|
|
-type PropsType = {
|
|
|
|
|
-};
|
|
|
|
|
|
|
+import React, { Component } from "react";
|
|
|
|
|
+import styled, { createGlobalStyle } from "styled-components";
|
|
|
|
|
+import { BrowserRouter, Route, Redirect, Switch } from "react-router-dom";
|
|
|
|
|
+import close from "assets/close.png";
|
|
|
|
|
+
|
|
|
|
|
+import api from "shared/api";
|
|
|
|
|
+import { Context } from "shared/Context";
|
|
|
|
|
+
|
|
|
|
|
+import Login from "./Login";
|
|
|
|
|
+import Register from "./Register";
|
|
|
|
|
+import CurrentError from "./CurrentError";
|
|
|
|
|
+import Home from "./home/Home";
|
|
|
|
|
+import Loading from "components/Loading";
|
|
|
|
|
+import { PorterUrls } from "shared/urls";
|
|
|
|
|
+
|
|
|
|
|
+type PropsType = {};
|
|
|
|
|
|
|
|
type StateType = {
|
|
type StateType = {
|
|
|
- loading: boolean,
|
|
|
|
|
- isLoggedIn: boolean,
|
|
|
|
|
- initialized: boolean,
|
|
|
|
|
|
|
+ loading: boolean;
|
|
|
|
|
+ isLoggedIn: boolean;
|
|
|
|
|
+ initialized: boolean;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
export default class Main extends Component<PropsType, StateType> {
|
|
export default class Main extends Component<PropsType, StateType> {
|
|
|
-
|
|
|
|
|
state = {
|
|
state = {
|
|
|
loading: true,
|
|
loading: true,
|
|
|
- isLoggedIn : false,
|
|
|
|
|
- initialized: localStorage.getItem("init") === 'true'
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ isLoggedIn: false,
|
|
|
|
|
+ initialized: localStorage.getItem("init") === "true",
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
componentDidMount() {
|
|
|
let { setUser } = this.context;
|
|
let { setUser } = this.context;
|
|
|
- api.checkAuth('', {}, {}, (err: any, res: any) => {
|
|
|
|
|
|
|
+ api.checkAuth("", {}, {}, (err: any, res: any) => {
|
|
|
if (err && err.response?.status == 403) {
|
|
if (err && err.response?.status == 403) {
|
|
|
- this.setState({ isLoggedIn: false, loading: false })
|
|
|
|
|
|
|
+ this.setState({ isLoggedIn: false, loading: false });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (res && res.data) {
|
|
if (res && res.data) {
|
|
|
setUser(res?.data?.id, res?.data?.email);
|
|
setUser(res?.data?.id, res?.data?.email);
|
|
|
this.setState({ isLoggedIn: true, initialized: true, loading: false });
|
|
this.setState({ isLoggedIn: true, initialized: true, loading: false });
|
|
|
} else {
|
|
} else {
|
|
|
- this.setState({ isLoggedIn: false, loading: false })
|
|
|
|
|
|
|
+ this.setState({ isLoggedIn: false, loading: false });
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
initialize = () => {
|
|
initialize = () => {
|
|
|
- this.setState({isLoggedIn: true, initialized: true});
|
|
|
|
|
- localStorage.setItem('init', 'true');
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
+ this.setState({ isLoggedIn: true, initialized: true });
|
|
|
|
|
+ localStorage.setItem("init", "true");
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
authenticate = () => {
|
|
authenticate = () => {
|
|
|
this.setState({ isLoggedIn: true, initialized: true });
|
|
this.setState({ isLoggedIn: true, initialized: true });
|
|
|
- }
|
|
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
handleLogOut = () => {
|
|
handleLogOut = () => {
|
|
|
// Clears local storage for proper rendering of clusters
|
|
// Clears local storage for proper rendering of clusters
|
|
@@ -61,75 +59,89 @@ export default class Main extends Component<PropsType, StateType> {
|
|
|
|
|
|
|
|
this.context.clearContext();
|
|
this.context.clearContext();
|
|
|
this.setState({ isLoggedIn: false, initialized: true });
|
|
this.setState({ isLoggedIn: false, initialized: true });
|
|
|
- }
|
|
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
renderMain = () => {
|
|
renderMain = () => {
|
|
|
if (this.state.loading) {
|
|
if (this.state.loading) {
|
|
|
- return <Loading />
|
|
|
|
|
|
|
+ return <Loading />;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const authedUrls: PorterUrls[] = [
|
|
const authedUrls: PorterUrls[] = [
|
|
|
- "dashboard", "templates", "integrations", "new-project",
|
|
|
|
|
- "cluster-dashboard", "provisioner", "project-settings"
|
|
|
|
|
|
|
+ "dashboard",
|
|
|
|
|
+ "templates",
|
|
|
|
|
+ "integrations",
|
|
|
|
|
+ "new-project",
|
|
|
|
|
+ "cluster-dashboard",
|
|
|
|
|
+ "provisioner",
|
|
|
|
|
+ "project-settings",
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
<Switch>
|
|
<Switch>
|
|
|
- <Route path='/login' render={() => {
|
|
|
|
|
- if (!this.state.isLoggedIn) {
|
|
|
|
|
- return <Login authenticate={this.authenticate} />
|
|
|
|
|
- } else {
|
|
|
|
|
- return <Redirect to='/' />
|
|
|
|
|
- }
|
|
|
|
|
- }} />
|
|
|
|
|
-
|
|
|
|
|
- <Route path='/register' render={() => {
|
|
|
|
|
- if (!this.state.isLoggedIn) {
|
|
|
|
|
- return <Register authenticate={this.initialize} />
|
|
|
|
|
- } else {
|
|
|
|
|
- return <Redirect to='/' />
|
|
|
|
|
- }
|
|
|
|
|
- }} />
|
|
|
|
|
-
|
|
|
|
|
- // TODO: Possible template this into a map from url to routed home
|
|
|
|
|
- {...authedUrls.map(route =>
|
|
|
|
|
- <Route path={`/${route}`} render={() => {
|
|
|
|
|
- if (this.state.isLoggedIn && this.state.initialized) {
|
|
|
|
|
|
|
+ <Route
|
|
|
|
|
+ path="/login"
|
|
|
|
|
+ render={() => {
|
|
|
|
|
+ if (!this.state.isLoggedIn) {
|
|
|
|
|
+ return <Login authenticate={this.authenticate} />;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return <Redirect to="/" />;
|
|
|
|
|
+ }
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ <Route
|
|
|
|
|
+ path="/register"
|
|
|
|
|
+ render={() => {
|
|
|
|
|
+ if (!this.state.isLoggedIn) {
|
|
|
|
|
+ return <Register authenticate={this.initialize} />;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return <Redirect to="/" />;
|
|
|
|
|
+ }
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ <Route
|
|
|
|
|
+ path={`/:subroute`}
|
|
|
|
|
+ render={(routeProps) => {
|
|
|
|
|
+ const urlRoute = routeProps.location.pathname.slice(1);
|
|
|
|
|
+ if (
|
|
|
|
|
+ this.state.isLoggedIn &&
|
|
|
|
|
+ this.state.initialized &&
|
|
|
|
|
+ PorterUrls.includes(urlRoute)
|
|
|
|
|
+ ) {
|
|
|
return (
|
|
return (
|
|
|
- <Home
|
|
|
|
|
|
|
+ <Home
|
|
|
|
|
+ key="home"
|
|
|
currentProject={this.context.currentProject}
|
|
currentProject={this.context.currentProject}
|
|
|
- currentCluster={this.context.currentCluster}
|
|
|
|
|
- currentRoute={route}
|
|
|
|
|
- logOut={this.handleLogOut}
|
|
|
|
|
|
|
+ currentCluster={this.context.currentCluster}
|
|
|
|
|
+ currentRoute={urlRoute as PorterUrls}
|
|
|
|
|
+ logOut={this.handleLogOut}
|
|
|
/>
|
|
/>
|
|
|
);
|
|
);
|
|
|
} else {
|
|
} else {
|
|
|
- return <Redirect to='/' />
|
|
|
|
|
|
|
+ return <Redirect to="/" />;
|
|
|
|
|
+ }
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ <Route
|
|
|
|
|
+ path="/"
|
|
|
|
|
+ render={() => {
|
|
|
|
|
+ if (this.state.isLoggedIn) {
|
|
|
|
|
+ return <Redirect to="/dashboard" />;
|
|
|
|
|
+ } else if (this.state.initialized) {
|
|
|
|
|
+ return <Redirect to="/login" />;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return <Redirect to="/register" />;
|
|
|
}
|
|
}
|
|
|
- }}/>
|
|
|
|
|
- )}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- <Route path='/' render={() => {
|
|
|
|
|
- if (this.state.isLoggedIn) {
|
|
|
|
|
- return <Redirect to='/dashboard'/>
|
|
|
|
|
- } else if (this.state.initialized) {
|
|
|
|
|
- return <Redirect to='/login'/>
|
|
|
|
|
- } else {
|
|
|
|
|
- return <Redirect to='/register' />
|
|
|
|
|
- }
|
|
|
|
|
- }}/>
|
|
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
</Switch>
|
|
</Switch>
|
|
|
);
|
|
);
|
|
|
- }
|
|
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
render() {
|
|
render() {
|
|
|
return (
|
|
return (
|
|
|
<StyledMain>
|
|
<StyledMain>
|
|
|
<GlobalStyle />
|
|
<GlobalStyle />
|
|
|
- <BrowserRouter>
|
|
|
|
|
- {this.renderMain()}
|
|
|
|
|
- </BrowserRouter>
|
|
|
|
|
|
|
+ <BrowserRouter>{this.renderMain()}</BrowserRouter>
|
|
|
<CurrentError currentError={this.context.currentError} />
|
|
<CurrentError currentError={this.context.currentError} />
|
|
|
</StyledMain>
|
|
</StyledMain>
|
|
|
);
|
|
);
|
|
@@ -167,4 +179,4 @@ const StyledMain = styled.div`
|
|
|
left: 0;
|
|
left: 0;
|
|
|
background: #202227;
|
|
background: #202227;
|
|
|
color: white;
|
|
color: white;
|
|
|
-`;
|
|
|
|
|
|
|
+`;
|