import React from 'react'
import { connect } from 'react-redux'
import { withRouter } from 'react-router'
import appFactory from '../../appFactory.js'

const qs = require('query-string')

// @FIXME Côme - 2018/07/31 - should this be in a component like AppFeatureManager ? (or AppCreateContentManager)
export class OpenCreateContentApp extends React.Component {
  openCreateContentApp = () => {
    const { idWorkspace, user, contentType, renderAppPopupCreation, match, location } = this.props

    if (isNaN(idWorkspace) || idWorkspace === -1) return

    if (['idws', 'type'].every(p => p in match.params) && contentType.map(c => c.slug).includes(match.params.type)) {
      renderAppPopupCreation(
        contentType.find(ct => ct.slug === match.params.type),
        user,
        idWorkspace,
        qs.parse(location.search).parent_id
      )
    }
  }

  componentDidMount () {
    console.log('%c<OpenCreateContentApp> did Mount', 'color: #dcae84', this.props)

    this.openCreateContentApp()
  }

  componentDidUpdate () {
    console.log('%c<OpenCreateContentApp> did Update', 'color: #dcae84', this.props)

    this.openCreateContentApp()
  }

  render () {
    return null
  }
}

const mapStateToProps = ({ user, contentType }) => ({ user, contentType })
export default withRouter(connect(mapStateToProps)(appFactory(OpenCreateContentApp)))
