Tracim.jsx 4.7KB

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