Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): adopt Actions API (#6257)
* test(structure): add document title test id * test(structure): document panel displays correct title for published document * feat(sanity): use actions API when publishing documents * feat(sanity): use Actions API when unpublishing documents (#6094) * feat(sanity): use Actions API when unpublishing documents * feat(core): set `skipCrossDatasetReferenceValidation` parameter when unpublishing documents * feat(sanity): use actions API when deleting documents (#6115) * feat(sanity): use actions API when deleting documents * fix(sanity): add skipCrossDatasetReferenceValidation to delete action --------- Co-authored-by: Bjørge Næss <bjoerge@gmail.com>
- Loading branch information
Showing
7 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/sanity/src/core/store/_legacy/document/document-pair/serverOperations/delete.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {type OperationImpl} from '../operations/types' | ||
|
||
export const del: OperationImpl<[], 'NOTHING_TO_DELETE'> = { | ||
disabled: ({snapshots}) => (snapshots.draft || snapshots.published ? false : 'NOTHING_TO_DELETE'), | ||
execute: ({client: globalClient, schema, idPair, typeName}) => { | ||
const vXClient = globalClient.withConfig({apiVersion: 'X'}) | ||
|
||
const {dataset} = globalClient.config() | ||
|
||
return vXClient.observable.request({ | ||
url: `/data/actions/${dataset}`, | ||
method: 'post', | ||
tag: 'document.delete', | ||
// this disables referential integrity for cross-dataset references. we | ||
// have this set because we warn against deletes in the `ConfirmDeleteDialog` | ||
// UI. This operation is run when "delete anyway" is clicked | ||
query: {skipCrossDatasetReferenceValidation: 'true'}, | ||
body: { | ||
actions: [ | ||
{ | ||
actionType: 'sanity.action.document.delete', | ||
draftId: idPair.draftId, | ||
publishedId: idPair.publishedId, | ||
}, | ||
], | ||
}, | ||
}) | ||
}, | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/sanity/src/core/store/_legacy/document/document-pair/serverOperations/publish.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {type OperationImpl} from '../operations/index' | ||
import {isLiveEditEnabled} from '../utils/isLiveEditEnabled' | ||
|
||
type DisabledReason = 'LIVE_EDIT_ENABLED' | 'ALREADY_PUBLISHED' | 'NO_CHANGES' | ||
|
||
export const publish: OperationImpl<[], DisabledReason> = { | ||
disabled: ({schema, typeName, snapshots}) => { | ||
if (isLiveEditEnabled(schema, typeName)) { | ||
return 'LIVE_EDIT_ENABLED' | ||
} | ||
if (!snapshots.draft) { | ||
return snapshots.published ? 'ALREADY_PUBLISHED' : 'NO_CHANGES' | ||
} | ||
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.publish', | ||
body: { | ||
actions: [ | ||
{ | ||
actionType: 'sanity.action.document.publish', | ||
draftId: idPair.draftId, | ||
publishedId: idPair.publishedId, | ||
}, | ||
], | ||
}, | ||
}) | ||
}, | ||
} |
36 changes: 36 additions & 0 deletions
36
packages/sanity/src/core/store/_legacy/document/document-pair/serverOperations/unpublish.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {type OperationImpl} from '../operations/types' | ||
import {isLiveEditEnabled} from '../utils/isLiveEditEnabled' | ||
|
||
type DisabledReason = 'LIVE_EDIT_ENABLED' | 'NOT_PUBLISHED' | ||
|
||
export const unpublish: OperationImpl<[], DisabledReason> = { | ||
disabled: ({schema, snapshots, typeName}) => { | ||
if (isLiveEditEnabled(schema, typeName)) { | ||
return 'LIVE_EDIT_ENABLED' | ||
} | ||
return snapshots.published ? false : 'NOT_PUBLISHED' | ||
}, | ||
execute: ({client: globalClient, idPair}) => { | ||
const vXClient = globalClient.withConfig({apiVersion: 'X'}) | ||
const {dataset} = globalClient.config() | ||
|
||
return vXClient.observable.request({ | ||
url: `/data/actions/${dataset}`, | ||
method: 'post', | ||
// this disables referential integrity for cross-dataset references. we | ||
// have this set because we warn against unpublishes in the `ConfirmDeleteDialog` | ||
// UI. This operation is run when "unpublish anyway" is clicked | ||
query: {skipCrossDatasetReferenceValidation: 'true'}, | ||
tag: 'document.unpublish', | ||
body: { | ||
actions: [ | ||
{ | ||
actionType: 'sanity.action.document.unpublish', | ||
draftId: idPair.draftId, | ||
publishedId: idPair.publishedId, | ||
}, | ||
], | ||
}, | ||
}) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {expect} from '@playwright/test' | ||
import {test} from '@sanity/test' | ||
|
||
test(`document panel displays correct title for published document`, async ({ | ||
page, | ||
createDraftDocument, | ||
}) => { | ||
const title = 'Test Title' | ||
|
||
await createDraftDocument('/test/content/book') | ||
await page.getByTestId('field-title').getByTestId('string-input').fill(title) | ||
|
||
// Ensure the correct title is displayed before publishing. | ||
await expect(page.getByTestId('document-panel-document-title')).toHaveText(title) | ||
|
||
// Wait for the document to be published. | ||
page.getByTestId('action-Publish').click() | ||
await expect(page.getByText('Published just now')).toBeVisible() | ||
|
||
// Ensure the correct title is displayed after publishing. | ||
expect(page.getByTestId('document-panel-document-title')).toHaveText(title) | ||
}) |