Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use discard in delete action when document is not published #6535

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.discard',
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()
})
Loading