App.jsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import React from 'react'
  16. import { Switch, Route } from 'react-router-dom'
  17. import styled, { injectGlobal } from 'styled-components'
  18. import { observe } from 'mobx'
  19. import Fonts from './atoms/Fonts'
  20. import Notifications from './organisms/Notifications'
  21. import LoginPage from './pages/LoginPage'
  22. import ReplicasPage from './pages/ReplicasPage'
  23. import NotFoundPage from './pages/NotFoundPage'
  24. import ReplicaDetailsPage from './pages/ReplicaDetailsPage'
  25. import MigrationsPage from './pages/MigrationsPage'
  26. import MigrationDetailsPage from './pages/MigrationDetailsPage'
  27. import EndpointsPage from './pages/EndpointsPage'
  28. import EndpointDetailsPage from './pages/EndpointDetailsPage'
  29. import WizardPage from './pages/WizardPage'
  30. import userStore from '../stores/UserStore'
  31. import AssessmentsPage from './pages/AssessmentsPage'
  32. import AssessmentDetailsPage from './pages/AssessmentDetailsPage'
  33. import UsersPage from './pages/UsersPage'
  34. import UserDetailsPage from './pages/UserDetailsPage'
  35. import ProjectsPage from './pages/ProjectsPage'
  36. import ProjectDetailsPage from './pages/ProjectDetailsPage'
  37. import DashboardPage from './pages/DashboardPage'
  38. import LogsPage from './pages/LogsPage'
  39. import LogStreamPage from './pages/LogStreamPage'
  40. import Tooltip from './atoms/Tooltip/Tooltip'
  41. import { navigationMenu } from '../constants'
  42. import Palette from './styleUtils/Palette'
  43. import StyleProps from './styleUtils/StyleProps'
  44. import configLoader from '../utils/Config'
  45. injectGlobal`
  46. ${Fonts}
  47. html, body, main {
  48. height: 100%;
  49. display: flex;
  50. flex-direction: column;
  51. min-height: 0;
  52. }
  53. body {
  54. margin: 0;
  55. color: ${Palette.black};
  56. font-family: Rubik;
  57. font-size: 14px;
  58. font-weight: ${StyleProps.fontWeights.regular};
  59. -webkit-font-smoothing: antialiased;
  60. -moz-osx-font-smoothing: grayscale;
  61. }
  62. `
  63. const Wrapper = styled.div`
  64. height: 100%;
  65. min-height: 0;
  66. display: flex;
  67. flex-direction: column;
  68. > div:first-child {
  69. height: 100%;
  70. }
  71. `
  72. type State = {
  73. isConfigReady: boolean,
  74. }
  75. class App extends React.Component<{}, State> {
  76. state = {
  77. isConfigReady: false,
  78. }
  79. async componentWillMount() {
  80. observe(userStore, 'loggedUser', () => { this.setState({}) })
  81. userStore.tokenLogin()
  82. await configLoader.load()
  83. this.setState({ isConfigReady: true })
  84. }
  85. render() {
  86. if (!this.state.isConfigReady) {
  87. return null
  88. }
  89. let renderOptionalPage = (name: string, component: any, path?: string, exact?: boolean) => {
  90. if (configLoader.config.disabledPages.find(p => p === name)) {
  91. return null
  92. }
  93. let requiresAdmin = Boolean(navigationMenu.find(n => n.value === name && n.requiresAdmin))
  94. if (!requiresAdmin) {
  95. return <Route path={`${path || `/${name}`}`} component={component} exact={exact} />
  96. }
  97. const renderNotFound = (title: string, subtitle: string) => (
  98. <Route
  99. path={`${path || `/${name}`}`}
  100. exact={exact}
  101. render={() => <NotFoundPage title={title} subtitle={subtitle} />}
  102. />
  103. )
  104. if (!userStore.loggedUser || userStore.loggedUser.isAdmin == null) {
  105. return renderNotFound('Checking permissions...', 'Please wait while checking user\'s permissions.')
  106. }
  107. if (userStore.loggedUser && userStore.loggedUser.isAdmin === false) {
  108. return renderNotFound('User doesn\'t have permissions to view this page', 'Please login in with an administrator acount to view this page.')
  109. }
  110. if (userStore.loggedUser && userStore.loggedUser.isAdmin) {
  111. return <Route path={`${path || `/${name}`}`} exact={exact} component={component} />
  112. }
  113. return null
  114. }
  115. return (
  116. <Wrapper>
  117. <Switch>
  118. <Route path="/" component={DashboardPage} exact />
  119. <Route path="/login" component={LoginPage} />
  120. <Route path="/dashboard" component={DashboardPage} />
  121. <Route path="/replicas" component={ReplicasPage} />
  122. <Route path="/replica/:id" component={ReplicaDetailsPage} exact />
  123. <Route path="/replica/:page/:id" component={ReplicaDetailsPage} />
  124. <Route path="/migrations" component={MigrationsPage} />
  125. <Route path="/migration/:id" component={MigrationDetailsPage} exact />
  126. <Route path="/migration/:page/:id" component={MigrationDetailsPage} />
  127. <Route path="/endpoints" component={EndpointsPage} />
  128. <Route path="/endpoint/:id" component={EndpointDetailsPage} />
  129. <Route path="/wizard/:type" component={WizardPage} />
  130. {renderOptionalPage('planning', AssessmentsPage)}
  131. {renderOptionalPage('planning', AssessmentDetailsPage, '/assessment/:info')}
  132. {renderOptionalPage('users', UsersPage)}
  133. {renderOptionalPage('users', UserDetailsPage, '/user/:id', true)}
  134. {renderOptionalPage('projects', ProjectsPage)}
  135. {renderOptionalPage('projects', ProjectDetailsPage, '/project/:id', true)}
  136. {renderOptionalPage('logging', LogsPage)}
  137. <Route path="/streamlog" component={LogStreamPage} />
  138. <Route component={NotFoundPage} />
  139. </Switch>
  140. <Notifications />
  141. <Tooltip />
  142. </Wrapper >
  143. )
  144. }
  145. }
  146. export default App