index.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 {
  29. makeSelectLogged,
  30. makeSelectNavigator,
  31. makeSelectOauth2Enabled
  32. } from '../App/selectors'
  33. import {
  34. DOWNLOAD_LIST_POLLING_FREQUENCY,
  35. EXTERNAL_LOG_OUT_URL
  36. } from 'app/globalConstants'
  37. import { Project, ProjectList } from 'containers/Projects/Loadable'
  38. import { Sidebar } from './Loadable'
  39. import Viz from 'containers/Viz/Loadable'
  40. import { Widget, Workbench } from 'containers/Widget/Loadable'
  41. import { View, ViewEditor } from 'containers/View/Loadable'
  42. import { Source } from 'containers/Source/Loadable'
  43. import { Schedule, ScheduleEditor } from 'containers/Schedule/Loadable'
  44. import { Dashboard } from 'containers/Dashboard/Loadable'
  45. import { Account } from 'containers/Account/Loadable'
  46. import { Profile, UserProfile } from 'containers/Profile/Loadable'
  47. import { ResetPassword } from 'containers/ResetPassword/Loadable'
  48. import {
  49. OrganizationList,
  50. Organization
  51. } from 'containers/Organizations/Loadable'
  52. import { NoAuthorization } from 'containers/NoAuthorization/Loadable'
  53. import { DataManager } from 'containers/DataManager/Loadable'
  54. import { DataShareService } from 'containers/DataShareService/Loadable'
  55. import { DataGovernance } from 'containers/DataGovernance/Loadable'
  56. const styles = require('./Main.less')
  57. type MappedStates = ReturnType<typeof mapStateToProps>
  58. type MappedDispatches = ReturnType<typeof mapDispatchToProps>
  59. type IMainProps = MappedStates & MappedDispatches & RouteComponentWithParams
  60. export class Main extends React.Component<IMainProps, {}> {
  61. private downloadListPollingTimer: number
  62. constructor(props: IMainProps & RouteComponentWithParams) {
  63. super(props)
  64. this.initPolling()
  65. }
  66. public componentWillUnmount() {
  67. if (this.downloadListPollingTimer) {
  68. clearInterval(this.downloadListPollingTimer)
  69. }
  70. }
  71. private initPolling = () => {
  72. this.props.onLoadDownloadList()
  73. this.downloadListPollingTimer = window.setInterval(() => {
  74. this.props.onLoadDownloadList()
  75. }, DOWNLOAD_LIST_POLLING_FREQUENCY)
  76. }
  77. private logout = () => {
  78. const { history, oauth2Enabled, onLogout } = this.props
  79. onLogout()
  80. if (oauth2Enabled) {
  81. history.replace(EXTERNAL_LOG_OUT_URL)
  82. } else {
  83. history.replace('/login')
  84. }
  85. }
  86. private renderAccount = () => (
  87. <Account>
  88. <Switch>
  89. <Redirect from='/account' exact to='/account/profile' />
  90. <Route path='/account/profile' component={Profile} />
  91. <Route path='/account/profile/:userId' component={UserProfile} />
  92. <Route path='/account/resetPassword' component={ResetPassword} />
  93. <Route path='/account/organizations' component={OrganizationList} />
  94. <Route
  95. path='/account/organization/:organizationId'
  96. component={Organization}
  97. />
  98. </Switch>
  99. </Account>
  100. )
  101. public render() {
  102. const { logged, navigator } = this.props
  103. return logged ? (
  104. <div className={styles.container}>
  105. <Navigator show={navigator} onLogout={this.logout} />
  106. <Switch>
  107. <Route path='/project(s?)'>
  108. <Switch>
  109. <Route path='/projects' exact component={ProjectList} />
  110. <Route path='/project/:projectId'>
  111. <Project>
  112. <Switch>
  113. <Route
  114. path='/project/:projectId/portal/:portalId'
  115. component={Dashboard}
  116. />
  117. <Route
  118. path='/project/:projectId/display/:displayId'
  119. component={Viz}
  120. />
  121. <Route
  122. exact
  123. path='/project/:projectId/widget/:widgetId?'
  124. component={Workbench}
  125. />
  126. <Route
  127. exact
  128. path='/project/:projectId/view/:viewId?'
  129. component={ViewEditor}
  130. />
  131. <Route
  132. exact
  133. path='/project/:projectId/schedule/:scheduleId?'
  134. component={ScheduleEditor}
  135. />
  136. <Sidebar>
  137. <Switch>
  138. <AuthorizedRoute
  139. permission='vizPermission'
  140. path='/project/:projectId/vizs'
  141. component={Viz}
  142. />
  143. <AuthorizedRoute
  144. permission='widgetPermission'
  145. path='/project/:projectId/widgets'
  146. component={Widget}
  147. />
  148. <AuthorizedRoute
  149. exact
  150. permission='viewPermission'
  151. path='/project/:projectId/views'
  152. component={View}
  153. />
  154. <AuthorizedRoute
  155. permission='sourcePermission'
  156. path='/project/:projectId/sources'
  157. component={Source}
  158. />
  159. <AuthorizedRoute
  160. permission='schedulePermission'
  161. path='/project/:projectId/schedules'
  162. component={Schedule}
  163. />
  164. </Switch>
  165. </Sidebar>
  166. </Switch>
  167. </Project>
  168. {/*<Switch> */}
  169. <Route
  170. path='/project/:projectId/dataManager'
  171. component={DataManager}
  172. />
  173. <Route
  174. path='/project/:projectId/dataShareService'
  175. component={DataShareService}
  176. />
  177. <Route
  178. path='/project/:projectId/dataGovernance'
  179. component={DataGovernance}
  180. />
  181. {/* </Switch> */}
  182. </Route>
  183. </Switch>
  184. </Route>
  185. <Route path='/account' render={this.renderAccount} />
  186. <Route path='/noAuthorization' component={NoAuthorization} />
  187. {/*<Redirect to='/projects' />*/}
  188. <Redirect to='/project/1/dataManager' />
  189. </Switch>
  190. </div>
  191. ) : (
  192. <div />
  193. )
  194. }
  195. }
  196. const mapStateToProps = createStructuredSelector({
  197. logged: makeSelectLogged(),
  198. oauth2Enabled: makeSelectOauth2Enabled(),
  199. navigator: makeSelectNavigator()
  200. })
  201. export function mapDispatchToProps(dispatch) {
  202. return {
  203. onLogged: (user) => dispatch(logged(user)),
  204. onLogout: () => dispatch(logout()),
  205. onLoadDownloadList: () => dispatch(loadDownloadList())
  206. }
  207. }
  208. export default connect(mapStateToProps, mapDispatchToProps)(Main)