Aller au contenu

Actions d'Entité

Les actions d’entité sont des boutons personnalisés qui apparaissent 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 { 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 }) => {
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 }) => {
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";
function PublishSelectedAction({ selectionController, context }: CollectionActionsProps) {
const handlePublish = async () => {
const selected = selectionController.selectedEntities;
for (const entity of selected) {
await context.data.save({
path: entity.path,
entityId: entity.id,
values: { status: "published" },
collection: context.collection
});
}
};
return (
<button onClick={handlePublish}>
Publish {selectionController.selectedEntities.length} selected
</button>
);
}
// Register
const collection = defineCollection({
admin: {
Actions: PublishSelectedAction
}
// ...
});

Actions de collection