Skip to content

Commit

Permalink
feat(test-studio): add noop custom publish action example
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed Aug 12, 2024
1 parent 441d7e5 commit 7860c8e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable react-hooks/rules-of-hooks */
import {type DocumentActionComponent, type DocumentActionProps, useDocumentOperation} from 'sanity'

export function createCustomPublishAction(originalAction: DocumentActionComponent) {
export function createCustomPublishAction(
originalAction: DocumentActionComponent,
): DocumentActionComponent {
return function CustomPublishAction(props: DocumentActionProps) {
const defaultPublishAction = originalAction(props)
const documentOperations = useDocumentOperation(props.id, props.type)
Expand All @@ -11,7 +13,24 @@ export function createCustomPublishAction(originalAction: DocumentActionComponen
label: 'Custom publish that sets publishedAt to now',
onHandle: () => {
documentOperations.patch.execute([{set: {publishedAt: new Date().toISOString()}}])
defaultPublishAction?.onHandle?.()
},
}
}
}

export function createNoopPatchPublishAction(
originalAction: DocumentActionComponent,
): DocumentActionComponent {
return function NoopPatchPublishAction(props) {
const defaultPublishAction = originalAction(props)
const documentOperations = useDocumentOperation(props.id, props.type)

return {
...defaultPublishAction,
label: 'Custom publish that sets someBoolean to true',
onHandle: () => {
documentOperations.patch.execute([{set: {someBoolean: true}}])
defaultPublishAction?.onHandle?.()
},
}
Expand Down
9 changes: 6 additions & 3 deletions dev/test-studio/documentActions/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {type DocumentActionsResolver} from 'sanity'

import {createCustomPublishAction} from './actions/createCustomPublishAction'
import {
createCustomPublishAction,
createNoopPatchPublishAction,
} from './actions/createCustomPublishAction'
import {TestConfirmDialogAction} from './actions/TestConfirmDialogAction'
import {TestCustomComponentAction} from './actions/TestCustomComponentAction'
import {TestCustomRestoreAction} from './actions/TestCustomRestoreAction'
Expand All @@ -15,12 +18,12 @@ export const resolveDocumentActions: DocumentActionsResolver = (prev, {schemaTyp
TestPopoverDialogAction,
TestCustomComponentAction,
...prev,
].map((action) => {
].flatMap((action) => {
if (action.action === 'restore') {
return TestCustomRestoreAction(action)
}
if (action.action === 'publish') {
return createCustomPublishAction(action)
return [createCustomPublishAction(action), createNoopPatchPublishAction(action)]
}
return action
})
Expand Down
5 changes: 5 additions & 0 deletions dev/test-studio/schema/debug/documentActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ export default {
title: 'Title',
},
{type: 'datetime', name: 'publishedAt', title: 'Published at'},
{
name: 'someBoolean',
title: 'Some Boolean',
type: 'boolean',
},
],
}

0 comments on commit 7860c8e

Please sign in to comment.