From 10d04c29388a1b73a4f8372ff1200aaaa04dfefe Mon Sep 17 00:00:00 2001 From: john <johnbarlas39@gmail.com> Date: Thu, 8 Dec 2016 21:45:21 +0200 Subject: [PATCH] lock fragment on editor use --- .../SimpleEditor/SimpleEditorWrapper.jsx | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/app/components/SimpleEditor/SimpleEditorWrapper.jsx b/app/components/SimpleEditor/SimpleEditorWrapper.jsx index e9d2c0f..05a0989 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 () { -- GitLab