Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Commit

Permalink
fix(editing): stop showing document editor after refresh COMPASS-4454 (
Browse files Browse the repository at this point in the history
…#324)

Handle a hit of the `Refresh` button like cancelling the document
editing process.
  • Loading branch information
addaleax authored Dec 18, 2020
1 parent 3ade950 commit 7771e44
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/components/document-footer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ class DocumentFooter extends React.Component {
this.handleModification();
}

/**
* Handle a possible switch of the document that we're targeting.
*/
componentDidUpdate(prevProps) {
if (this.props.doc !== prevProps.doc) {
// If the underlying document changed, that means that the collection
// contents have been refreshed. Treat that like cancelling the edit.
this.handleCancel();
}
}

/**
* Unsubscribe from the udpate store on unmount.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/components/editable-document.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ class EditableDocument extends React.Component {
if (prevProps.doc !== this.props.doc) {
this.unsubscribeFromDocumentEvents(prevProps.doc);
this.subscribeToDocumentEvents(this.props.doc);
if (this.state.editing || this.state.deleting) {
// If the underlying document changed, that means that the collection
// contents have been refreshed. In that case, stop editing/deleting.
setImmediate(() => {
this.setState({ editing: false, deleting: false });
});
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/components/editable-json.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class JsonEditor extends React.Component {
* Fold all documents on update as well, since we might get fresh documents
* from the query bar.
*/
componentDidUpdate() {
componentDidUpdate(prevProps) {
if (!this.state.editing) {
this.editor.getSession().foldAll(2);
}
Expand All @@ -113,6 +113,10 @@ class JsonEditor extends React.Component {
this.handleRemoveSuccess();
} else if (this.state.editing && this.props.updateSuccess) {
this.handleUpdateSuccess();
} else if (this.props.doc !== prevProps.doc) {
// If the underlying document changed, that means that the collection
// contents have been refreshed. Treat that like cancelling the edit.
this.handleCancel();
}
}

Expand All @@ -122,6 +126,7 @@ class JsonEditor extends React.Component {
handleCancel() {
this.setState({
editing: false,
deleting: false,
mode: VIEWING,
message: EMPTY,
value: this.props.doc
Expand Down
7 changes: 6 additions & 1 deletion src/components/json-editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class EditableJson extends React.Component {
* Fold all documents on update as well, since we might get fresh documents
* from the query bar.
*/
componentDidUpdate() {
componentDidUpdate(prevProps) {
if (!this.state.editing) {
this.editor.getSession().foldAll(2);
}
Expand All @@ -113,6 +113,10 @@ class EditableJson extends React.Component {
this.handleRemoveSuccess();
} else if (this.state.editing && this.props.updateSuccess) {
this.handleUpdateSuccess();
} else if (this.props.doc !== prevProps.doc) {
// If the underlying document changed, that means that the collection
// contents have been refreshed. Treat that like cancelling the edit.
this.handleCancel();
}
}

Expand All @@ -122,6 +126,7 @@ class EditableJson extends React.Component {
handleCancel() {
this.setState({
editing: false,
deleting: false,
mode: VIEWING,
message: EMPTY,
json: jsBeautify(EJSON.stringify(this.props.doc.generateObject()))
Expand Down

0 comments on commit 7771e44

Please sign in to comment.