index.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 Helmet from 'react-helmet'
  22. import { connect } from 'react-redux'
  23. import { createStructuredSelector } from 'reselect'
  24. import { Route, HashRouter as Router, Switch, Redirect } from 'react-router-dom'
  25. import { RouteComponentWithParams } from 'utils/types'
  26. import { compose } from 'redux'
  27. import { logged, logout, getServerConfigurations, getUserByToken } from './actions'
  28. import injectReducer from 'utils/injectReducer'
  29. import reducer from './reducer'
  30. import injectSaga from 'utils/injectSaga'
  31. import saga from './sagas'
  32. import { makeSelectLogged } from './selectors'
  33. import checkLogin from 'utils/checkLogin'
  34. import { setToken } from 'utils/request'
  35. import { statistic } from 'utils/statistic/statistic.dv'
  36. import FindPassword from 'containers/FindPassword'
  37. import { Background } from 'containers/Background/Loadable'
  38. import { Main } from 'containers/Main/Loadable'
  39. import { Activate } from 'containers/Register/Loadable'
  40. type MappedStates = ReturnType<typeof mapStateToProps>
  41. type MappedDispatches = ReturnType<typeof mapDispatchToProps>
  42. type AppProps = MappedStates & MappedDispatches & RouteComponentWithParams
  43. export class App extends React.PureComponent<AppProps> {
  44. constructor (props: AppProps) {
  45. super(props)
  46. props.onGetServerConfigurations()
  47. this.checkTokenLink()
  48. }
  49. private getQs = () => {
  50. const search = location.search
  51. const qs = search ? search.substr(1) : ''
  52. if (qs) {
  53. return qs
  54. .split('&')
  55. .reduce((rdc, val) => {
  56. const pair = val.split('=')
  57. rdc[pair[0]] = pair[1]
  58. return rdc
  59. }, {})
  60. } else {
  61. return false
  62. }
  63. }
  64. private checkTokenLink = () => {
  65. const {
  66. history,
  67. onGetLoginUser
  68. } = this.props
  69. const qs = this.getQs()
  70. const token = qs['usertoken']
  71. // TODO allow take other parameters
  72. // const dashboard = qs['dashboard']
  73. // @FIXME login with token from url query
  74. // if (token) {
  75. // setToken(token)
  76. // // onGetLoginUser(() => {
  77. // history.replace('/projects')
  78. // // if (dashboard) {
  79. // // router.replace(`/project/${this.props.params.projectId}/dashboard/${dashboard}`)
  80. // // } else {
  81. // // }
  82. // // })
  83. // } else {
  84. this.checkNormalLogin()
  85. // }
  86. }
  87. private checkNormalLogin = () => {
  88. if (checkLogin()) {
  89. const token = localStorage.getItem('TOKEN')
  90. const loginUser = localStorage.getItem('loginUser')
  91. setToken(token)
  92. this.props.onLogged(JSON.parse(loginUser))
  93. statistic.sendPrevDurationRecord()
  94. } else {
  95. this.props.onLogout()
  96. }
  97. }
  98. private renderRoute = () => {
  99. const { logged } = this.props
  100. return (
  101. logged ? (
  102. <Redirect to="/projects" />
  103. ) : (
  104. <Redirect to="/login" />
  105. )
  106. )
  107. }
  108. public render () {
  109. const { logged } = this.props
  110. if (typeof logged !== 'boolean') { return null }
  111. return (
  112. <div>
  113. <Helmet
  114. titleTemplate="%s - Davinci"
  115. defaultTitle="Davinci Web Application"
  116. meta={[
  117. {
  118. name: 'description',
  119. content: 'Davinci web application built for data visualization'
  120. }
  121. ]}
  122. />
  123. <Router>
  124. <Switch>
  125. <Route path="/activate" component={Activate} />
  126. <Route path="/joinOrganization" exact component={Background} />
  127. <Route path="/findPassword" component={FindPassword} />
  128. <Route path="/" exact render={this.renderRoute} />
  129. <Route path="/" component={logged ? Main : Background} />
  130. </Switch>
  131. </Router>
  132. </div>
  133. )
  134. }
  135. }
  136. const withReducer = injectReducer({ key: 'global', reducer })
  137. const withSaga = injectSaga({ key: 'global', saga })
  138. const mapStateToProps = createStructuredSelector({
  139. logged: makeSelectLogged()
  140. })
  141. const mapDispatchToProps = (dispatch) => ({
  142. onLogged: (user) => dispatch(logged(user)),
  143. onLogout: () => dispatch(logout()),
  144. onGetLoginUser: (token: string) => dispatch(getUserByToken(token)),
  145. onGetServerConfigurations: () => dispatch(getServerConfigurations())
  146. })
  147. const withConnect = connect(
  148. mapStateToProps,
  149. mapDispatchToProps
  150. )
  151. export default compose(
  152. withReducer,
  153. withSaga,
  154. withConnect
  155. )(App)