diff --git a/app/components/SimpleEditor/SimpleEditorWrapper.jsx b/app/components/SimpleEditor/SimpleEditorWrapper.jsx index e9d2c0ffdde473f72a6f6a0e9afe00743e808112..05a0989f3d457ce5b5f6e0aee49d6812caffd0e2 100644 --- a/app/components/SimpleEditor/SimpleEditorWrapper.jsx +++ b/app/components/SimpleEditor/SimpleEditorWrapper.jsx @@ -1,4 +1,4 @@ -import { find } from 'lodash' +import { get, find } from 'lodash' import React from 'react' import { connect } from 'react-redux' import { bindActionCreators } from 'redux' @@ -13,7 +13,10 @@ export class SimpleEditorWrapper extends React.Component { this.save = this.save.bind(this) this.updateComments = this.updateComments.bind(this) + this._checkRights = this._checkRights.bind(this) + this._releaseLock = this._releaseLock.bind(this) + this._acquireLock = this._acquireLock.bind(this) this.state = { canEdit: false @@ -28,6 +31,37 @@ export class SimpleEditorWrapper extends React.Component { }) this._checkRights() + 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 + } + } + updateFragment(book, fragment) } // componentDidUpdate () {