Skip to content

Commit

Permalink
[desk-tool] Lock to last-known local revision when publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Mar 1, 2018
1 parent f32b2ff commit 50bc679
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
29 changes: 24 additions & 5 deletions packages/@sanity/desk-tool/src/pane/EditorWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,36 @@ export default class EditorWrapper extends React.Component {

handlePublish = () => {
const {documentId} = this.props
const {draft} = this.state
const {draft, published} = this.state
this.setState({isPublishing: true})

const tx = client.observable
.transaction()
.createOrReplace({
const tx = client.observable.transaction()

if (!published || !published.snapshot) {
// If the document has not been published, we want to create it - if it suddenly exists
// before being created, we don't want to overwrite if, instead we want to yield an error
tx.create({
...omit(draft.snapshot, '_updatedAt'),
_id: getPublishedId(documentId)
})
.delete(getDraftId(documentId))
} else {
// If it exists already, we only want to update it if the revision on the remote server
// matches what our local state thinks it's at
tx
.patch(getPublishedId(documentId), {
// Hack until other mutations support revision locking
unset: ['_reserved_prop_'],
ifRevisionID: published.snapshot._rev
})
.createOrReplace({
...omit(draft.snapshot, '_updatedAt'),
_id: getPublishedId(documentId)
})
}

tx.delete(getDraftId(documentId))

// @todo add error handling for revision mismatch
Observable.from(tx.commit())
.map(result => ({
type: 'success',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ function toFormBuilderPatch(origin: Origin, patch: GradientPatch): Patch {
origin
}
}
if (type === 'ifRevisionID') {
return {
type: 'ifRevisionID',
value: patch[type],
origin
}
}
console.warn(new Error(`Unsupported patch type: ${type}`))
return null
})
Expand Down

0 comments on commit 50bc679

Please sign in to comment.