Skip to content

Commit

Permalink
[desk-tool] Publish documents in a transaction (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Jan 30, 2018
1 parent 10e5287 commit a9f00f3
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions packages/@sanity/desk-tool/src/pane/EditorWrapper.js
Expand Up @@ -200,7 +200,7 @@ export default class EditorPane extends React.Component {
.catch(error => Observable.of({
type: 'error',
message: `An error occurred while attempting to unpublish document.
This usually means that you attempted to {transactionResult.action} a document that other documents
This usually means that you attempted to unpublish a document that other documents
refers to.`,
error
}))
Expand All @@ -209,25 +209,39 @@ export default class EditorPane extends React.Component {
})
}

handlePublish = draft => {

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

const publishedId = this.getPublishedId()

this.published.createOrReplace({
...omit(draft, '_createdAt', '_updatedAt'),
_id: publishedId
})
const tx = client.observable
.transaction()
.createOrReplace({
...omit(draft.snapshot, '_createdAt', '_updatedAt'),
_id: getPublishedId(documentId)
})
.delete(getDraftId(documentId))

return this.published.commit()
.mergeMap(() => {
this.draft.delete()
return this.draft.commit()
Observable.from(tx.commit())
.map(result => ({
type: 'success',
result: result
}))
.catch(error => Observable.of({
type: 'error',
message: 'An error occurred while attempting to publishing document',
error
}))
.subscribe({
next: result => {
this.setState({
transactionResult: result
})
},
complete: () => {
this.setState({isPublishing: false})
}
})
.subscribe(() =>
this.setState({isPublishing: false})
)
}

handleChange = event => {
Expand Down

0 comments on commit a9f00f3

Please sign in to comment.