Skip to content

Instantly share code, notes, and snippets.

@Aberratio
Last active September 13, 2022 05:15
Show Gist options
  • Save Aberratio/932ee337dfabb34cf5e81bdecff5664e to your computer and use it in GitHub Desktop.
Save Aberratio/932ee337dfabb34cf5e81bdecff5664e to your computer and use it in GitHub Desktop.
[Sanity.io v2] with Schedule plugin

Read about the plugin here.

Instalation

sanity install @sanity/scheduled-publishing

Simply installing the plugin adds the ability to schedule the publication of all documents over time from the drop-down list level next to the "Publish" button. The snippets below show the customization options for this.

Three use cases were implemented:

  1. Change Publish button to Schedule for all documents that are type1 and type2
  2. Change Publish button to Schedule for all documents documents that are type3 and have not yet been published or have been published with a date in the future. type3 has field publishAt
  3. Show the Schedule button on type4 documents only.The Schedule button appears in the drop-down list next to the Publish button, but does not replace it.
//Create an actions folder in the root directory of the project and in it add the following file: /actions/resolveDocumentActions.js
import defaultResolve, { PublishAction } from 'part:@sanity/base/document-actions';
import { ScheduleAction } from '@sanity/scheduled-publishing';
export default function resolveDocumentActions(props) {
const defaultActions = defaultResolve(props);
//-------------CASE 1 - START------------------
//Change "Publish" button to "Schedule" for all documents that are type1 and type2
if (props.type === 'type1' || props.type === 'type2') {
return defaultResolve(props).map((Action) => (Action === PublishAction ? ScheduleAction : Action));
}
//-------------CASE 1 - END------------------
//-------------CASE 2 - START------------------
//Change "Publish" button to "Schedule" for all documents documents that are type3 and have
//not yet been published or have been published with a date in the future.
//type3 has field publishAt
const nowDate = new Date();
if ((props.type === 'type3') &&
(new Date(props.draft?.publishAt) > nowDate ||
(props.published && props.published.publishAt > nowDate))
) {
return defaultResolve(props).map((Action) => (Action === PublishAction ? ScheduleAction : Action));
}
//-------------CASE 2 - END------------------
//-------------CASE 3 - START------------------
//Show the "Schedule" button on type4 documents only.
//The "Schedule" button appears in the drop-down list next to the "Publish" button, but does not replace it.
if (props.type === 'type4') {
return [...defaultActions.slice(0, 1), ScheduleAction, ...defaultActions.slice(1)]
}
//-------------CASE 3 - END------------------
return defaultActions;
}
{
...,
"parts": [
{
...,
"implements": "part:@sanity/base/document-actions/resolver",
"path": "./actions/resolveDocumentActions"
}
]
}
//Don't forget to add type3 (and all other types!) to schema.js
export default {
name: 'type3',
title: 'Type 3',
type: 'document',
fields: [
{
name: 'title',
title: 'Title',
type: 'string',
},
{
name: 'publishAt',
title: 'Publish at',
type: 'datetime',
validation: (Rule) => Rule.required(),
},
],
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment