Skip to content

Commit eded8d1

Browse files
committed
feat: add documentation for programmatically starting actions with runAction
1 parent b0d9e41 commit eded8d1

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

  • adminforth/documentation/docs/tutorial/03-Customization

adminforth/documentation/docs/tutorial/03-Customization/09-Actions.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,46 @@ Backend handler: read the payload via `extra`.
360360
Notes:
361361
- If you don’t emit a payload, the default behavior is used by the UI (e.g., in lists the current row context is used). When you do provide a payload, it will be forwarded to the backend as `extra` for your action handler.
362362
- You can combine default context with your own payload by merging before emitting, for example: `emit('callAction', { ...row, asListed: true })` if your component has access to the row object.
363+
364+
## Start actions programmatically
365+
You can execute resource actions manually using adminforth.runAction(). This is useful inside hooks, plugins, cron jobs, custom endpoints, or any backend automation.
366+
367+
```ts title="./resources/apartments.ts"
368+
actions: [
369+
{
370+
//diff-add
371+
id: 'testToggle listedAction',
372+
name: 'Toggle listed',
373+
icon: 'flowbite:eye-solid',
374+
...
375+
}
376+
]
377+
```
378+
Then execute it from a hook for example:
379+
380+
```ts title="./resources/apartments.ts"
381+
hooks: {
382+
...
383+
afterSave: async ({ record, adminUser, resource, adminforth }: { record: any, adminUser: AdminUser, resource: AdminForthResource, adminforth: any }) => {
384+
385+
await adminforth.runAction({
386+
actionId: 'Toggle listed',
387+
resourceId: resource.resourceId,
388+
recordId: record.id,
389+
adminUser,
390+
});
391+
392+
return { ok: true };
393+
},
394+
},
395+
```
396+
397+
runAction() automatically:
398+
- finds the resource
399+
- finds the action
400+
- checks permissions via allowed
401+
- executes the action handler
402+
- passes full action context (recordId, adminUser, extra, etc.)
403+
404+
> ☝️ runAction() is not limited to hooks — you can call it anywhere you have access to the AdminForth instance.
405+

0 commit comments

Comments
 (0)