OpenContentApp.jsx 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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, workspaceContent, 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' && workspaceContent.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({
  20. type: `${contentToOpen.type}_reloadContent`, // handled by html-document:src/container/AdminWorkspaceUser.jsx
  21. data: contentToOpen
  22. })
  23. } else { // open another app
  24. // if another app is already visible, hide it
  25. if (appOpenedType !== false) dispatchCustomEvent({type: `${appOpenedType}_hideApp`})
  26. // open app
  27. renderAppFeature(
  28. contentType.find(ct => ct.slug === contentToOpen.type),
  29. user,
  30. contentToOpen
  31. )
  32. this.props.updateAppOpenedType(contentToOpen.type)
  33. }
  34. }
  35. }
  36. componentDidMount () {
  37. console.log('%c<OpenContentApp> did Mount', 'color: #dcae84', this.props)
  38. this.openContentApp()
  39. }
  40. componentDidUpdate () {
  41. console.log('%c<OpenContentApp> did Update', 'color: #dcae84', this.props)
  42. this.openContentApp()
  43. }
  44. render () {
  45. return null
  46. }
  47. }
  48. const mapStateToProps = ({ user, workspaceContent, contentType }) => ({ user, workspaceContent, contentType })
  49. export default withRouter(connect(mapStateToProps)(appFactory(OpenContentApp)))