OpenContentApp.jsx 2.0KB

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