index.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React from 'react'
  21. import { connect } from 'react-redux'
  22. import { Route, Switch, Redirect } from 'react-router-dom'
  23. import AuthorizedRoute from './AuthorizedRoute'
  24. import { RouteComponentWithParams } from 'utils/types'
  25. import { createStructuredSelector } from 'reselect'
  26. import Navigator from 'components/Navigator'
  27. import { logged, logout, loadDownloadList } from '../App/actions'
  28. import { makeSelectLogged, makeSelectNavigator, makeSelectOauth2Enabled } from '../App/selectors'
  29. import { DOWNLOAD_LIST_POLLING_FREQUENCY, EXTERNAL_LOG_OUT_URL } from 'app/globalConstants'
  30. import { Project, ProjectList } from 'containers/Projects/Loadable'
  31. import { Sidebar } from './Loadable'
  32. import Viz from 'containers/Viz/Loadable'
  33. import { Widget, Workbench } from 'containers/Widget/Loadable'
  34. import { View, ViewEditor } from 'containers/View/Loadable'
  35. import { Source } from 'containers/Source/Loadable'
  36. import { Schedule, ScheduleEditor } from 'containers/Schedule/Loadable'
  37. import { Dashboard } from 'containers/Dashboard/Loadable'
  38. import { Account } from 'containers/Account/Loadable'
  39. import { Profile, UserProfile } from 'containers/Profile/Loadable'
  40. import { ResetPassword } from 'containers/ResetPassword/Loadable'
  41. import {
  42. OrganizationList,
  43. Organization
  44. } from 'containers/Organizations/Loadable'
  45. import { NoAuthorization } from 'containers/NoAuthorization/Loadable'
  46. const styles = require('./Main.less')
  47. type MappedStates = ReturnType<typeof mapStateToProps>
  48. type MappedDispatches = ReturnType<typeof mapDispatchToProps>
  49. type IMainProps = MappedStates & MappedDispatches & RouteComponentWithParams
  50. export class Main extends React.Component<IMainProps, {}> {
  51. private downloadListPollingTimer: number
  52. constructor(props: IMainProps & RouteComponentWithParams) {
  53. super(props)
  54. this.initPolling()
  55. }
  56. public componentWillUnmount() {
  57. if (this.downloadListPollingTimer) {
  58. clearInterval(this.downloadListPollingTimer)
  59. }
  60. }
  61. private initPolling = () => {
  62. this.props.onLoadDownloadList()
  63. this.downloadListPollingTimer = window.setInterval(() => {
  64. this.props.onLoadDownloadList()
  65. }, DOWNLOAD_LIST_POLLING_FREQUENCY)
  66. }
  67. private logout = () => {
  68. const { history, oauth2Enabled, onLogout } = this.props
  69. onLogout()
  70. if (oauth2Enabled) {
  71. history.replace(EXTERNAL_LOG_OUT_URL)
  72. } else {
  73. history.replace('/login')
  74. }
  75. }
  76. private renderAccount = () => (
  77. <Account>
  78. <Switch>
  79. <Redirect from="/account" exact to="/account/profile" />
  80. <Route path="/account/profile" component={Profile} />
  81. <Route path="/account/profile/:userId" component={UserProfile} />
  82. <Route path="/account/resetPassword" component={ResetPassword} />
  83. <Route path="/account/organizations" component={OrganizationList} />
  84. <Route
  85. path="/account/organization/:organizationId"
  86. component={Organization}
  87. />
  88. </Switch>
  89. </Account>
  90. )
  91. public render() {
  92. const { logged, navigator } = this.props
  93. return logged ? (
  94. <div className={styles.container}>
  95. <Navigator show={navigator} onLogout={this.logout} />
  96. <Switch>
  97. <Route path="/project(s?)">
  98. <Switch>
  99. <Route path="/projects" exact component={ProjectList} />
  100. <Route path="/project/:projectId">
  101. <Project>
  102. <Switch>
  103. <Route
  104. path="/project/:projectId/portal/:portalId"
  105. component={Dashboard}
  106. />
  107. <Route
  108. path="/project/:projectId/display/:displayId"
  109. component={Viz}
  110. />
  111. <Route
  112. exact
  113. path="/project/:projectId/widget/:widgetId?"
  114. component={Workbench}
  115. />
  116. <Route
  117. exact
  118. path="/project/:projectId/view/:viewId?"
  119. component={ViewEditor}
  120. />
  121. <Route
  122. exact
  123. path="/project/:projectId/schedule/:scheduleId?"
  124. component={ScheduleEditor}
  125. />
  126. <Sidebar>
  127. <Switch>
  128. <AuthorizedRoute
  129. permission="vizPermission"
  130. path="/project/:projectId/vizs"
  131. component={Viz}
  132. />
  133. <AuthorizedRoute
  134. permission="widgetPermission"
  135. path="/project/:projectId/widgets"
  136. component={Widget}
  137. />
  138. <AuthorizedRoute
  139. exact
  140. permission="viewPermission"
  141. path="/project/:projectId/views"
  142. component={View}
  143. />
  144. <AuthorizedRoute
  145. permission="sourcePermission"
  146. path="/project/:projectId/sources"
  147. component={Source}
  148. />
  149. <AuthorizedRoute
  150. permission="schedulePermission"
  151. path="/project/:projectId/schedules"
  152. component={Schedule}
  153. />
  154. </Switch>
  155. </Sidebar>
  156. </Switch>
  157. </Project>
  158. </Route>
  159. </Switch>
  160. </Route>
  161. <Route path="/account" render={this.renderAccount} />
  162. <Route path="/noAuthorization" component={NoAuthorization} />
  163. <Redirect to="/projects" />
  164. </Switch>
  165. </div>
  166. ) : (
  167. <div />
  168. )
  169. }
  170. }
  171. const mapStateToProps = createStructuredSelector({
  172. logged: makeSelectLogged(),
  173. oauth2Enabled: makeSelectOauth2Enabled(),
  174. navigator: makeSelectNavigator()
  175. })
  176. export function mapDispatchToProps(dispatch) {
  177. return {
  178. onLogged: (user) => dispatch(logged(user)),
  179. onLogout: () => dispatch(logout()),
  180. onLoadDownloadList: () => dispatch(loadDownloadList())
  181. }
  182. }
  183. export default connect(mapStateToProps, mapDispatchToProps)(Main)