Tracim.jsx 5.4KB

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