Skip to content
Snippets Groups Projects
SimpleEditorWrapper.jsx 4.81 KiB
Newer Older
import { get, filter, find, forEach, union } from 'lodash'
import React from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'

john's avatar
john committed
import * as Actions from 'pubsweet-frontend/src/actions'

import SimpleEditor from './SimpleEditor'

export class SimpleEditorWrapper extends React.Component {
  constructor (props) {
    super(props)

    this.save = this.save.bind(this)
    this.update = this.update.bind(this)
    this.updateComments = this.updateComments.bind(this)
john's avatar
john committed

    this._checkRights = this._checkRights.bind(this)
john's avatar
john committed
    this._releaseLock = this._releaseLock.bind(this)
    this._acquireLock = this._acquireLock.bind(this)

    this.state = {
      canEdit: false
    }
  }

  componentWillMount () {
    const { getCollections, getFragments } = this.props.actions
john's avatar
john committed

    getCollections().then(result => {
      getFragments(result.collections[0])
    })
john's avatar
john committed

john's avatar
john committed
    const { user } = this.props
    user.roles = this.getRoles()

    this._checkRights()
john's avatar
john committed
    this._acquireLock()
  }

  componentDidMount () {
    window.addEventListener('beforeunload', this._releaseLock)
  }

  componentWillUnmount () {
    this._releaseLock()
    window.removeEventListener('beforeunload', this._releaseLock)
  }

  _releaseLock () {
    const { book, fragment, user } = this.props
    const { updateFragment } = this.props.actions
    if (get(fragment, 'lock.editor.username') && get(fragment, 'lock.editor.username') === user.username) {
      fragment.lock = null
    }
    updateFragment(book, fragment)
  }

  _acquireLock () {
    const { book, fragment, user } = this.props
    const { updateFragment } = this.props.actions

    fragment.lock = {
      editor: {
        username: user.username
john's avatar
john committed
    }
    updateFragment(book, fragment)
chris's avatar
chris committed
  // componentDidUpdate () {
  //   console.log('did update wrapper')
  //   // this._checkRights()
  // }
john's avatar
john committed
  // TODO -- refactor
  _checkRights () {
    const { fragment, user } = this.props

john's avatar
john committed
    if (user.admin) return this.setState({ canEdit: true })

    if ((fragment.progress['review'] === 1 && user.teams[0].teamType.name === 'Author') ||
        (fragment.progress['edit'] === 1 && user.teams[0].teamType.name === 'Copy Editor')) {
      this.setState({ canEdit: true })
    } else {
      this.setState({ canEdit: false })
    }
    if (user.teams[0].teamType.name === 'Production Editor') {
      this.setState({ canEdit: true })
    }
  }

  save (source, callback) {
    const { book, fragment } = this.props
    const { updateFragment } = this.props.actions

    fragment.source = source
    updateFragment(book, fragment)
  }

  updateComments (newComments) {
    const { book, fragment } = this.props
    const { updateFragment } = this.props.actions

    fragment.comments = newComments
    updateFragment(book, fragment)
  }

  update (newChapter) {
    const { book } = this.props
    const { updateFragment } = this.props.actions
    updateFragment(book, newChapter)
  }

  getRoles () {
    const { user, book } = this.props

    const teams = filter(user.teams, function (t) {
      return t.object.id === book.id
    })

    let roles = []
    if (user.admin) roles.push('admin')

    function addRole (role) {
      roles = union(roles, [role])
    }

    forEach(teams, function (t) {
      switch (t.teamType.name) {
        case 'Production Editor':
          addRole('production-editor')
          break
        case 'Copy Editor':
          addRole('copy-editor')
          break
        case 'Author':
          addRole('author')
          break
      }
    })

    return roles
  }

john's avatar
john committed
    const { book, fragment, history, user } = this.props
    // TODO -- merge update and updateComments
    return (
      <SimpleEditor
        book={book}
        canEdit={this.state.canEdit}
        fragment={fragment}
john's avatar
john committed
        history={history}
        onSave={this.save}
        update={this.update}
        updateComments={this.updateComments}
        user={user}
      />
    )
  }
}

SimpleEditorWrapper.propTypes = {
john's avatar
john committed
  actions: React.PropTypes.object.isRequired,
  book: React.PropTypes.object.isRequired,
  fragment: React.PropTypes.object.isRequired,
john's avatar
john committed
  history: React.PropTypes.object.isRequired,
  user: React.PropTypes.object.isRequired,
  update: React.PropTypes.func.isRequired
}

function mapStateToProps (state, ownProps) {
  const bookId = ownProps.params.bookId
  let book = find(state.collections, function (c) {
    return c.id === bookId
  })

  const fragmentId = ownProps.params.fragmentId
  let fragment = state.fragments[fragmentId]
  // console.log('fragment source', fragment.source)

  return {
    fragment: fragment,
    book: book,
    user: state.currentUser.user
  }
}

function mapDispatchToProps (dispatch) {
  return {
    actions: bindActionCreators(Actions, dispatch)
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(SimpleEditorWrapper)