Newer
Older
import { filter, find, forEach, union } from 'lodash'
import React from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
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)
this.fileUpload = this.fileUpload.bind(this)
// this._releaseLock = this._releaseLock.bind(this)
// this._acquireLock = this._acquireLock.bind(this)
this.state = {
canEdit: false
}
}
componentWillMount () {
const { getCollections, getFragments } = this.props.actions
getCollections().then(result => {
getFragments(result.collections[0])
})
const { user } = this.props
user.roles = this.getRoles()
// this._checkRights()
// this._acquireLock()
save (source, callback) {
const { book, fragment } = this.props
const { updateFragment } = this.props.actions
fragment.source = source
updateFragment(book, fragment)
}
fileUpload (file) {
const { fileUpload } = this.props.actions
return fileUpload(file)
}
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])
}
// TODO -- simply use lowercase and replace spaces
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
}
// TODO -- merge update and updateComments
return (
<SimpleEditor
book={book}
canEdit={this.state.canEdit}
fileUpload={this.fileUpload}
updateComments={this.updateComments}
user={user}
/>
)
}
}
SimpleEditorWrapper.propTypes = {
book: React.PropTypes.object.isRequired,
user: React.PropTypes.object.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)