OpenContentApp.jsx 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import { withRouter } from 'react-router'
  4. import appFactory from '../../appFactory.js'
  5. // @FIXME Côme - 2018/07/31 - should this be in a component like AppFeatureManager ?
  6. export class OpenContentApp extends React.Component {
  7. openContentApp = () => {
  8. const { idWorkspace, appOpenedType, user, workspaceContentList, contentType, renderAppFeature, dispatchCustomEvent, match } = this.props
  9. if (isNaN(idWorkspace) || idWorkspace === -1) return
  10. if (['type', 'idcts'].every(p => p in match.params) && match.params.type !== 'contents' && workspaceContentList.length) {
  11. if (isNaN(match.params.idcts) || !contentType.map(c => c.slug).includes(match.params.type)) return
  12. const contentToOpen = {
  13. content_id: parseInt(match.params.idcts),
  14. workspace_id: parseInt(idWorkspace),
  15. type: match.params.type
  16. }
  17. console.log('%c<OpenContentApp> contentToOpen', 'color: #dcae84', contentToOpen)
  18. if (appOpenedType === contentToOpen.type) { // app already open
  19. dispatchCustomEvent(`${contentToOpen.type}_reloadContent`, contentToOpen)
  20. } else { // open another app
  21. // if another app is already visible, hide it
  22. if (appOpenedType !== false) dispatchCustomEvent(`${appOpenedType}_hideApp`, {})
  23. // open app
  24. renderAppFeature(
  25. contentType.find(ct => ct.slug === contentToOpen.type),
  26. user,
  27. contentToOpen
  28. )
  29. this.props.updateAppOpenedType(contentToOpen.type)
  30. }
  31. }
  32. }
  33. componentDidMount () {
  34. console.log('%c<OpenContentApp> did Mount', 'color: #dcae84', this.props)
  35. this.openContentApp()
  36. }
  37. componentDidUpdate () {
  38. console.log('%c<OpenContentApp> did Update', 'color: #dcae84', this.props)
  39. this.openContentApp()
  40. }
  41. render () {
  42. return null
  43. }
  44. }
  45. const mapStateToProps = ({ user, workspaceContentList, contentType }) => ({ user, workspaceContentList, contentType })
  46. export default withRouter(connect(mapStateToProps)(appFactory(OpenContentApp)))