Skip to content

Commit

Permalink
feat(sanity): use actions API when discarding drafts
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 authored and ricokahler committed May 15, 2024
1 parent 1f6b8a8 commit c1755d1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {publish} from './operations/publish'
import {restore} from './operations/restore'
import {unpublish} from './operations/unpublish'
import {del as serverDel} from './serverOperations/delete'
import {discardChanges as serverDiscardChanges} from './serverOperations/discardChanges'
import {patch as serverPatch} from './serverOperations/patch'
import {publish as serverPublish} from './serverOperations/publish'
import {unpublish as serverUnpublish} from './serverOperations/unpublish'
Expand Down Expand Up @@ -65,6 +66,7 @@ const serverOperationImpls = {
...operationImpls,
del: serverDel,
delete: serverDel,
discardChanges: serverDiscardChanges,
patch: serverPatch,
publish: serverPublish,
unpublish: serverUnpublish,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {type IdPair} from '../../types'
import {emitOperation} from '../operationEvents'
import {publish} from '../operations/publish'
import {del as serverDel} from '../serverOperations/delete'
import {discardChanges as serverDiscardChanges} from '../serverOperations/discardChanges'
import {patch as serverPatch} from '../serverOperations/patch'
import {publish as serverPublish} from '../serverOperations/publish'
import {unpublish as serverUnpublish} from '../serverOperations/unpublish'
Expand Down Expand Up @@ -73,6 +74,7 @@ export function createOperationsAPI(args: OperationArgs): OperationsAPI {
...operationsAPI,
delete: wrap('delete', serverDel, args),
del: wrap('delete', serverDel, args),
discardChanges: wrap('discardChanges', serverDiscardChanges, args),
patch: wrap('patch', serverPatch, args),
publish: wrap('publish', serverPublish, args),
unpublish: wrap('unpublish', serverUnpublish, args),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {type OperationImpl} from '../operations/types'

type DisabledReason = 'NO_CHANGES' | 'NOT_PUBLISHED'

export const discardChanges: OperationImpl<[], DisabledReason> = {
disabled: ({snapshots}) => {
if (!snapshots.draft) {
return 'NO_CHANGES'
}
if (!snapshots.published) {
return 'NOT_PUBLISHED'
}
return false
},
execute: ({client: globalClient, idPair}) => {
const vXClient = globalClient.withConfig({apiVersion: 'X'})
const {dataset} = globalClient.config()

return vXClient.observable.request({
url: `/data/actions/${dataset}`,
method: 'post',
tag: 'document.discard-changes',
body: {
actions: [
{
actionType: 'sanity.action.document.discard',
draftId: idPair.draftId,
publishedId: idPair.publishedId,
},
],
},
})
},
}

0 comments on commit c1755d1

Please sign in to comment.