OpenContentApp.jsx 2.3KB

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