How to add UnpublishAction in Sanity studio document actions
7 replies
Last updated: Feb 17, 2022
S
Hi all, I'm trying to unpublish a document in Sanity studio but I don't seem to have the option to do so?Dependencies and versions:
This is what my options look like:
"@sanity/base": "^2.25.4",
"@sanity/color-input": "^2.25.4",
"@sanity/components": "^2.14.0",
"@sanity/core": "^2.25.0",
"@sanity/default-layout": "^2.25.4",
"@sanity/default-login": "^2.24.1",
"@sanity/desk-tool": "^2.25.4",
"@sanity/vision": "^2.25.4",Feb 17, 2022, 10:06 PM
S
I don't think so. There's another document that does allow me to unpublish it, a location group that is referenced by the above document I am trying to unpublish. This location group document is published, and it only prevents me from unpublishing it due to it being referenced by the location document.
Feb 17, 2022, 10:17 PM
Thanks for that additional context. I also edited my comment above as you’re right—it is published.
Feb 17, 2022, 10:17 PM
S
This what it looks like:
import defaultResolve, {
PublishAction,
DiscardChangesAction,
DeleteAction,
DuplicateAction,
} from "part:@sanity/base/document-actions";
import { FiEye } from "react-icons/fi";
const previewSecret = process.env.SANITY_STUDIO_PREVIEW_SECRET;
const localURL = "<http://localhost:3000>";
const baseUrl = window.location.hostname === "localhost" ? localURL : "<https://ivee.vercel.app>";
const singletons = ["pageHome", "jobsPage", "blogIndex", "settingsGeneral", "settingsSeo", "emailModal"];
const editAndDelete = ["person", "service", "page", "pageCollection", "blogPost", "location", "service"];
const previews = [
"pageHome",
"job",
"jobsPage",
"blogIndex",
"service",
"page",
"location",
"pageCollection",
"blogPost",
];
const PreviewAction = (props) => {
const slug = props.draft
? props.draft.content?.slug?.current || props.draft.slug?.current
: props.published?.content?.slug?.current || props.published?.slug?.current;
return {
label: "Open Preview",
icon: FiEye,
onHandle: () => {
window.open(`${baseUrl}/api/preview?token=${previewSecret}&type=${props.type}&slug=${slug || ""}`);
},
};
};
export default function resolveDocumentActions(props) {
const isSingle = singletons.indexOf(props.type) > -1;
const canEditDelete = editAndDelete.indexOf(props.type) > -1;
const canPreview = previews.indexOf(props.type) > -1;
if (isSingle) {
return [PublishAction, DiscardChangesAction, ...(canPreview ? [PreviewAction] : [])];
}
if (canEditDelete) {
return [PublishAction, DiscardChangesAction, DuplicateAction, DeleteAction, ...(canPreview ? [PreviewAction] : [])];
}
return [...defaultResolve(props), ...(canPreview ? [PreviewAction] : [])];
}Feb 17, 2022, 10:22 PM
I believe you’ll want to import UnpublishAction and add it to your arrays at the bottom. See here for an example.
Feb 17, 2022, 10:24 PM
S
Thank you for the help! I'm still fairly new to Sanity and am working within this already existing codebase. That is indeed the fix :)
Feb 17, 2022, 10:27 PM
That’s great! 🎉
Unless you have a good reason to do so, you might want to spread
Unless you have a good reason to do so, you might want to spread
defaultResolve(props)and then add your additional actions, such as is done here . That way, you don’t need to worry if Sanity updates the defaults. Of course, there are good cases for listing them out manually, too. 🙂
Feb 17, 2022, 10:30 PM
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
