Skip to content
Snippets Groups Projects
SimpleEditorWrapper.jsx 2.74 KiB
Newer Older
john's avatar
john committed
import { each, filter, find, 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)

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

  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()
john's avatar
john committed
  render () {
    const { book, fragment, history, user } = this.props
john's avatar
john committed
    return (
      <SimpleEditor
        book={book}
        fileUpload={this.fileUpload}
        fragment={fragment}
        history={history}
        onSave={this.save}
        update={this.update}
        user={user}
      />
    )
  fileUpload (file) {
    const { fileUpload } = this.props.actions
    return fileUpload(file)
  }

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

john's avatar
john committed
    const teams = filter(user.teams, (t) => {
      return t.object.id === book.id
    })

    let roles = []
john's avatar
john committed
    const addRole = role => { roles = union(roles, [role]) }

    if (user.admin) addRole('admin')

    each(teams, (team) => {
      const name = team.teamType.name
      const modified = name.trim().toLowerCase().replace(' ', '-')
      addRole(modified)
john's avatar
john committed
  save (source, callback) {
    const { book, fragment } = this.props
    const { updateFragment } = this.props.actions
john's avatar
john committed
    fragment.source = source
    updateFragment(book, fragment)
  }

  update (newChapter) {
    const { book } = this.props
    const { updateFragment } = this.props.actions
    updateFragment(book, newChapter)
john's avatar
john committed
// TODO -- review required props
SimpleEditorWrapper.propTypes = {
john's avatar
john committed
  actions: React.PropTypes.object.isRequired,
  book: React.PropTypes.object.isRequired,
  fragment: React.PropTypes.object,
john's avatar
john committed
  history: React.PropTypes.object.isRequired,
  user: React.PropTypes.object.isRequired,
  update: React.PropTypes.func
john's avatar
john committed
const mapStateToProps = (state, ownProps) => {
  const bookId = ownProps.params.bookId
john's avatar
john committed
  const book = find(state.collections, (c) => {
    return c.id === bookId
  })

  const fragmentId = ownProps.params.fragmentId
john's avatar
john committed
  const fragment = state.fragments[fragmentId]
john's avatar
john committed
  const user = state.currentUser.user

  return { fragment, book, user }
john's avatar
john committed
const mapDispatchToProps = (dispatch) => {
  return {
    actions: bindActionCreators(Actions, dispatch)
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(SimpleEditorWrapper)