Skip to content

Commit

Permalink
[Sanity] Updated custom publish actions (#3315)
Browse files Browse the repository at this point in the history
* sanity config cleanup

* removed extra const for type

* re-organized admin tools and fixed types

* Removed no longer needed custom actions for translations

* Simplified publication plugin

* Removed extra logic in publish action

* synced SEO-updates

* lock sync

* bug: Removed old index export
  • Loading branch information
KenAJoh authored Nov 6, 2024
1 parent 4118a27 commit 256ab2a
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 220 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { DocumentActionComponent, useDocumentOperation } from "sanity";

export function createPublishWithDateAction(
originalAction: DocumentActionComponent,
): DocumentActionComponent {
return (props) => {
const originalResult = originalAction(props);

const { patch } = useDocumentOperation(props.id, props.type);
if (!originalResult) {
return null;
}
return {
...originalResult,
onHandle: () => {
patch.execute([
{ setIfMissing: { publishedAt: new Date().toISOString() } },
]);
originalResult?.onHandle?.();
},
};
};
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
export * from "./approve";
export * from "./delete";
export * from "./discardChanges";
export * from "./duplicate";
export * from "./focusAction";
export * from "./restore";
export * from "./unpublish";
export * from "./updateAction";
export * from "./defaultPublish";

This file was deleted.

This file was deleted.

70 changes: 25 additions & 45 deletions aksel.nav.no/website/sanity/plugins/publication-flow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@ import {
import { allArticleDocuments } from "@/sanity/config";
import {
createWrappedApproveAction,
createWrappedDefaultPublish,
createWrappedDeleteAction,
createWrappedDiscardChangesAction,
createWrappedDuplicateAction,
createWrappedFocusAction,
createWrappedRestoreAction,
createWrappedUnpublishAction,
createWrappedUpdateAction,
} from "./actions";
import { createPublishWithDateAction } from "./actions/createPublishWithDateAction";
import { CreateStatusBadge, createBadgeComponent } from "./badges";

const generateBadges = (prev: DocumentBadgeComponent[]) => {
Expand All @@ -29,21 +24,6 @@ const getCustomActions = (prev: DocumentActionComponent[]) => {
if (action.action === "publish") {
return createWrappedFocusAction(action);
}
if (action.action === "unpublish") {
return createWrappedUnpublishAction(action);
}
if (action.action === "delete") {
return createWrappedDeleteAction(action);
}
if (action.action === "duplicate") {
return createWrappedDuplicateAction(action);
}
if (action.action === "restore") {
return createWrappedRestoreAction(action);
}
if (action.action === "discardChanges") {
return createWrappedDiscardChangesAction(action);
}
return action;
});

Expand All @@ -55,42 +35,42 @@ const getCustomActions = (prev: DocumentActionComponent[]) => {
return [...customActions, ...defaultActions.slice(1)];
};

const withCustomPublishAction = (prev: DocumentActionComponent[]) => {
return [createWrappedDefaultPublish(prev[0]), ...prev.slice(1)];
};

interface PublicationFlowOptions {
hasQualityControl: string[];
hasPublishedAt: string[];
}
export const publicationFlow = definePlugin(() => {
const hasQualityControl = [
"komponent_artikkel",
"ds_artikkel",
"aksel_artikkel",
];
const hasPublishedAt = allArticleDocuments;

export const publicationFlowConfig = definePlugin<PublicationFlowOptions>(
({ hasQualityControl, hasPublishedAt }) => ({
return {
name: "publication-flow",
document: {
actions: (prev, { schemaType }) => {
if (hasQualityControl.some((e) => e === schemaType)) {
actions: (prev, context) => {
if (
hasQualityControl.some((docType) => docType === context.schemaType)
) {
return getCustomActions(prev);
}

if (hasPublishedAt.some((e) => e === schemaType)) {
return withCustomPublishAction(prev);
if (hasPublishedAt.some((docType) => docType === context.schemaType)) {
return prev.map((originalAction) =>
originalAction.action === "publish"
? createPublishWithDateAction(originalAction)
: originalAction,
);
}

return prev;
},
badges: (prev, { schemaType }) => {
if (hasQualityControl.some((e) => e === schemaType)) {
badges: (prev, context) => {
if (
hasQualityControl.some((docType) => docType === context.schemaType)
) {
return generateBadges(prev);
}
return prev;
},
},
}),
);

export const publicationFlow = () =>
publicationFlowConfig({
hasQualityControl: ["komponent_artikkel", "ds_artikkel", "aksel_artikkel"],
hasPublishedAt: [...allArticleDocuments],
});
};
});

0 comments on commit 256ab2a

Please sign in to comment.