Aller au contenu

Actions d'entité

Les actions d’entité sont des boutons personnalisés qui s’affichent sur les entités individuelles. Utilisez-les pour des opérations telles que la publication, l’archivage, le clonage ou le déclenchement de workflows externes.

import { defineCollection } from "@rebasepro/cms-types";
import { resolveSelection } from "@rebasepro/cms";
import { iconSize } from "@rebasepro/ui";
import { Copy, Upload } from "lucide-react";
const articlesCollection = defineCollection({
slug: "articles",
name: "Articles",
table: "articles",
properties: {
id: { name: "ID", type: "number", isId: "increment" },
name: { name: "Name", type: "string" },
status: { name: "Status", type: "string" },
publishedAt: { name: "Published At", type: "date" }
},
admin: {
entityActions: [
{
name: "Publish",
icon: <Upload size={iconSize.small}/>,
onClick: async ({ entity, context }) => {
if (!entity || !context) return;
await context.data.collection<Record<string, unknown>>(entity.path)
.update(entity.id, { status: "published", publishedAt: new Date() });
context.snackbarController?.open({
type: "success",
message: "Article published!"
});
}
},
{
name: "Clone",
icon: <Copy size={iconSize.small}/>,
onClick: async ({ entity, context }) => {
if (!entity || !context) return;
const { id, ...values } = entity.values;
await context.data.collection<Record<string, unknown>>(entity.path)
.create({ ...values, name: values.name + " (Copy)" });
}
}
]
}
});

Pour les actions au niveau de la barre d’outils qui s’appliquent à la collection ou aux entités sélectionnées :

import { defineCollection } from "@rebasepro/cms-types";
import { useData } from "@rebasepro/app";
import { resolveSelection } from "@rebasepro/cms";
function PublishSelectedAction({ selectionController, path }: CollectionActionsProps) {
const data = useData();
const handlePublish = async () => {
// `selection` is either the rows that were ticked or a query standing
// for every row that matches — `resolveSelection` reads them, one page
// at a time, and refuses rather than returning a prefix.
const selected = await resolveSelection({
selection: selectionController.selection,
accessor: data.collection(path)
});
for (const entity of selected) {
await data.collection(entity.path).update(entity.id, { status: "published" });
}
};
return (
<button onClick={handlePublish}>
Publish {selectionController.selectedCount ?? "all"} selected
</button>
);
}
// Register — `Actions` is an array, so several can be composed.
const collection = defineCollection({
slug: "products",
name: "Products",
table: "products",
properties: { /* … */ },
admin: {
Actions: [PublishSelectedAction]
}
});

Actions de collection