Skip to content

Commit

Permalink
fix: use discard when doc is not published
Browse files Browse the repository at this point in the history
  • Loading branch information
cngonzalez committed Apr 30, 2024
1 parent 251d592 commit 7c7c8a8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,33 @@ import {isLiveEditEnabled} from '../utils/isLiveEditEnabled'

export const del: OperationImpl<[], 'NOTHING_TO_DELETE'> = {
disabled: ({snapshots}) => (snapshots.draft || snapshots.published ? false : 'NOTHING_TO_DELETE'),
execute: ({client: globalClient, schema, idPair, typeName}) => {
execute: ({client: globalClient, schema, idPair, typeName, snapshots}) => {
if (isLiveEditEnabled(schema, typeName)) {
const tx = globalClient.observable.transaction().delete(idPair.publishedId)
return tx.commit({tag: 'document.delete'})
}

const vXClient = globalClient.withConfig({apiVersion: 'X'})

const {dataset} = globalClient.config()

//the delete action requires a published doc -- discard if not present
if (!snapshots.published) {
return vXClient.observable.request({
url: `/data/actions/${dataset}`,
method: 'post',
tag: 'document.delete',
body: {
actions: [
{
actionType: 'sanity.action.document.discard',
draftId: idPair.draftId,
publishedId: idPair.publishedId,
},
],
},
})
}

return vXClient.observable.request({
url: `/data/actions/${dataset}`,
method: 'post',
Expand Down
29 changes: 29 additions & 0 deletions test/e2e/tests/document-actions/delete.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {expect} from '@playwright/test'
import {test} from '@sanity/test'

const name = 'Test Name'

test(`unpublished documents can be deleted`, async ({page, createDraftDocument}) => {
await createDraftDocument('/test/content/author')
await page.getByTestId('field-name').getByTestId('string-input').fill(name)

await page.getByTestId('action-menu-button').click()
await page.getByTestId('action-Delete').click()
await page.getByRole('button', {name: 'Delete now'}).click()

await expect(page.getByText('The document was successfully deleted')).toBeVisible()
})

test(`published documents can be deleted`, async ({page, createDraftDocument}) => {
await createDraftDocument('/test/content/author')
await page.getByTestId('field-name').getByTestId('string-input').fill(name)

await page.getByTestId('action-Publish').click()
await expect(page.getByText('was published')).toBeVisible()

await page.getByTestId('action-menu-button').click()
await page.getByTestId('action-Delete').click()
await page.getByRole('button', {name: 'Delete now'}).click()

await expect(page.getByText('The document was successfully deleted')).toBeVisible()
})

0 comments on commit 7c7c8a8

Please sign in to comment.