Tracim.jsx 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import { translate } from 'react-i18next'
  4. import Sidebar from './Sidebar.jsx'
  5. import Header from './Header.jsx'
  6. import Login from './Login.jsx'
  7. import Account from './Account.jsx'
  8. import AppFullscreenRouter from './AppFullscreenRouter.jsx'
  9. import FlashMessage from '../component/FlashMessage.jsx'
  10. import WorkspaceContent from './WorkspaceContent.jsx'
  11. import WIPcomponent from './WIPcomponent.jsx'
  12. import {
  13. Route, withRouter, Redirect
  14. } from 'react-router-dom'
  15. import { COOKIE, PAGE } from '../helper.js'
  16. import {
  17. getUserIsConnected
  18. } from '../action-creator.async.js'
  19. import {
  20. newFlashMessage,
  21. removeFlashMessage,
  22. setUserConnected
  23. } from '../action-creator.sync.js'
  24. import Cookies from 'js-cookie'
  25. import Dashboard from './Dashboard.jsx'
  26. class Tracim extends React.Component {
  27. constructor (props) {
  28. super(props)
  29. document.addEventListener('appCustomEvent', this.customEventReducer)
  30. }
  31. customEventReducer = async ({ detail: { type, data } }) => {
  32. switch (type) {
  33. case 'redirect':
  34. console.log('%c<Tracim> Custom event', 'color: #28a745', type, data)
  35. this.props.history.push(data.url)
  36. break
  37. case 'addFlashMsg':
  38. console.log('%c<Tracim> Custom event', 'color: #28a745', type, data)
  39. this.props.dispatch(newFlashMessage(data.msg, data.type, data.delay))
  40. break
  41. }
  42. }
  43. async componentDidMount () {
  44. // console.log('<Tracim> did Mount')
  45. const { dispatch } = this.props
  46. const userFromCookies = {
  47. email: Cookies.get(COOKIE.USER_LOGIN),
  48. auth: Cookies.get(COOKIE.USER_AUTH)
  49. }
  50. const fetchGetUserIsConnected = await dispatch(getUserIsConnected(userFromCookies))
  51. switch (fetchGetUserIsConnected.status) {
  52. case 200:
  53. dispatch(setUserConnected({
  54. ...fetchGetUserIsConnected.json,
  55. auth: userFromCookies.auth,
  56. logged: true
  57. }))
  58. break
  59. case 401:
  60. dispatch(setUserConnected({logged: false})); break
  61. default:
  62. dispatch(setUserConnected({logged: null})); break
  63. }
  64. }
  65. handleRemoveFlashMessage = msg => this.props.dispatch(removeFlashMessage(msg))
  66. render () {
  67. const { props } = this
  68. if (props.user.logged === null) return null // @TODO show loader
  69. if (props.user.logged === false && props.location.pathname !== '/login') {
  70. return <Redirect to={{pathname: '/login', state: {from: props.location}}} />
  71. }
  72. return (
  73. <div className='tracim'>
  74. <Header />
  75. <FlashMessage flashMessage={props.flashMessage} removeFlashMessage={this.handleRemoveFlashMessage} t={props.t} />
  76. <div className='tracim__content'>
  77. <Route path={PAGE.LOGIN} component={Login} />
  78. <Route exact path='/' component={() => {
  79. switch (props.user.logged) {
  80. case true:
  81. return <Redirect to={{pathname: PAGE.WORKSPACE.ROOT, state: {from: props.location}}} />
  82. case false:
  83. return <Redirect to={{pathname: '/login', state: {from: props.location}}} />
  84. case null:
  85. return null
  86. }
  87. }} />
  88. <Route path='/workspaces/:idws?' render={() => // Workspace Router
  89. <div className='sidebarpagecontainer'>
  90. <Sidebar />
  91. <Route exact path={PAGE.WORKSPACE.ROOT} render={() => props.workspaceList.length === 0 // handle '/' and redirect to first workspace
  92. ? null // @FIXME this needs to be handled in case of new user that has no workspace
  93. : <Redirect to={{pathname: PAGE.WORKSPACE.DASHBOARD(props.workspaceList[0].id), state: {from: props.location}}} />
  94. } />
  95. <Route exact path={`${PAGE.WORKSPACE.ROOT}/:idws`} render={props2 => // handle '/workspaces/:id' and add '/contents'
  96. <Redirect to={{pathname: PAGE.WORKSPACE.CONTENT_LIST(props2.match.params.idws), state: {from: props.location}}} />
  97. } />
  98. <Route path={PAGE.WORKSPACE.DASHBOARD(':idws')} component={Dashboard} />
  99. <Route path={PAGE.WORKSPACE.CALENDAR(':idws')} component={() => <div><br /><br /><br /><br />NYI</div>} />
  100. <Route path={PAGE.WORKSPACE.CONTENT(':idws', ':type', ':idcts')} component={WorkspaceContent} />
  101. <Route exact path={PAGE.WORKSPACE.CONTENT_LIST(':idws')} component={WorkspaceContent} />
  102. </div>
  103. } />
  104. <Route path={PAGE.ACCOUNT} render={() =>
  105. <div className='sidebarpagecontainer'>
  106. <Sidebar />
  107. <Account />
  108. </div>
  109. } />
  110. <Route path={PAGE.ADMIN.ROOT} render={() =>
  111. <div className='sidebarpagecontainer'>
  112. <Sidebar />
  113. <AppFullscreenRouter />
  114. </div>
  115. } />
  116. <Route path={'/wip/:cp'} component={WIPcomponent} /> {/* for testing purpose only */}
  117. <div id='appFeatureContainer' />
  118. <div id='popupCreateContentContainer' />
  119. </div>
  120. </div>
  121. )
  122. }
  123. }
  124. const mapStateToProps = ({ user, appList, contentType, workspaceList, flashMessage }) => ({ user, appList, contentType, workspaceList, flashMessage })
  125. export default withRouter(connect(mapStateToProps)(translate()(Tracim)))