Main.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import React, { Component } from 'react';
  2. import styled, { createGlobalStyle } from 'styled-components';
  3. import { BrowserRouter, Route, Redirect, Switch } from 'react-router-dom';
  4. import close from '../assets/close.png';
  5. import api from '../shared/api';
  6. import { Context } from '../shared/Context';
  7. import Login from './Login';
  8. import Register from './Register';
  9. import CurrentError from './CurrentError';
  10. import Home from './home/Home';
  11. import Loading from '../components/Loading';
  12. type PropsType = {
  13. };
  14. type StateType = {
  15. loading: boolean,
  16. isLoggedIn: boolean,
  17. initialized: boolean,
  18. };
  19. export default class Main extends Component<PropsType, StateType> {
  20. state = {
  21. loading: true,
  22. isLoggedIn : false,
  23. initialized: localStorage.getItem("init") === 'true'
  24. }
  25. componentDidMount() {
  26. let { setUser } = this.context;
  27. api.checkAuth('', {}, {}, (err: any, res: any) => {
  28. console.log(err)
  29. if (err && err.response?.status == 403) {
  30. this.setState({ isLoggedIn: false, loading: false })
  31. }
  32. if (res && res.data) {
  33. setUser(res?.data?.id, res?.data?.email);
  34. this.setState({ isLoggedIn: true, initialized: true, loading: false });
  35. } else {
  36. this.setState({ isLoggedIn: false, loading: false })
  37. }
  38. });
  39. }
  40. initialize = () => {
  41. this.setState({isLoggedIn: true, initialized: true});
  42. localStorage.setItem('init', 'true');
  43. }
  44. authenticate = () => {
  45. this.setState({ isLoggedIn: true, initialized: true });
  46. }
  47. handleLogOut = () => {
  48. // Clears local storage for proper rendering of clusters
  49. localStorage.clear();
  50. this.context.clearContext();
  51. this.setState({ isLoggedIn: false, initialized: true });
  52. }
  53. renderMain = () => {
  54. if (this.state.loading) {
  55. return <Loading />
  56. }
  57. return (
  58. <Switch>
  59. <Route path='/login' render={() => {
  60. if (!this.state.isLoggedIn) {
  61. return <Login authenticate={this.authenticate} />
  62. } else {
  63. return <Redirect to='/' />
  64. }
  65. }} />
  66. <Route path='/register' render={() => {
  67. if (!this.state.isLoggedIn) {
  68. return <Register authenticate={this.initialize} />
  69. } else {
  70. return <Redirect to='/' />
  71. }
  72. }} />
  73. <Route path='/dashboard' render={() => {
  74. if (this.state.isLoggedIn && this.state.initialized) {
  75. return <Home logOut={this.handleLogOut} />
  76. } else {
  77. return <Redirect to='/' />
  78. }
  79. }}/>
  80. <Route path='/' render={() => {
  81. if (this.state.isLoggedIn) {
  82. return <Redirect to='/dashboard'/>
  83. } else if (this.state.initialized) {
  84. return <Redirect to='/login'/>
  85. } else {
  86. return <Redirect to='/register' />
  87. }
  88. }}/>
  89. </Switch>
  90. );
  91. }
  92. render() {
  93. return (
  94. <StyledMain>
  95. <GlobalStyle />
  96. <BrowserRouter>
  97. {this.renderMain()}
  98. </BrowserRouter>
  99. <CurrentError />
  100. </StyledMain>
  101. );
  102. }
  103. }
  104. Main.contextType = Context;
  105. const GlobalStyle = createGlobalStyle`
  106. * {
  107. box-sizing: border-box;
  108. font-family: 'Work Sans', sans-serif;
  109. }
  110. body {
  111. background: #202227;
  112. overscroll-behavior-x: none;
  113. }
  114. a {
  115. color: #949eff;
  116. text-decoration: none;
  117. }
  118. img {
  119. max-width: 100%;
  120. }
  121. `;
  122. const StyledMain = styled.div`
  123. height: 100vh;
  124. width: 100vw;
  125. position: fixed;
  126. top: 0;
  127. left: 0;
  128. background: #202227;
  129. color: white;
  130. `;