Skip to content

Commit

Permalink
docs: model actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mle-moni committed Aug 29, 2024
1 parent 7d67fa5 commit 33892a8
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/test_adomin_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export const IDEA_CONFIG = createModelViewConfig(() => Idea, {
description: faker.lorem.sentence(),
})

return { mesfsage: 'Création effectuée' }
return { message: 'Création effectuée' }
},
},
{
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 96 additions & 0 deletions docs/src/content/docs/reference/views/models/global-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
title: Model Global Actions
sidebar:
order: 1030
description: Adomin model global actions reference
---

In the user interface **globalActions** are buttons that trigger an action relative to a table globally

![field image](~/assets/images/reference/models/actions/global_actions.png)

Here is the code needed to add those two buttons to the user interface

```ts
{
globalActions: [
{
name: 'add-random-idea',
tooltip: 'Ajouter une idée random',
icon: 'arrows-shuffle',
iconColor: 'red',
action: async () => {
await Idea.create({
title: faker.music.songName(),
description: faker.lorem.sentence(),
})

return { message: 'Création effectuée' }
},
},
{
name: 'delete-ideas-not-linked-to-user',
tooltip: 'Supprimer les idées non liées à un utilisateur',
icon: 'trash',
iconColor: 'orange',
action: async () => {
await Idea.query().whereNull('userId').delete()

return { message: 'Suppression effectuée' }
},
},
]
}
```

## Config

**globalActions** is an array of objects, each object representing an action, here are the parameters for each action

### name

Name of the action, this will be used to identify the action, must be unique for the model

e.g. the API endpoint generated will look like this if name = 'add-random-idea'

POST /adomin/api/actions/Idea/add-random-idea

### tooltip

Tooltip shown on the frontend, when hovering the button

### icon

Icon name, by default this uses Tabler icons

You can browse the list of available icons at: https://tabler.io/icons

### iconColor

Color of the icon, this will be passed as the css color property, so you can use any valid css color

### action

Function to execute when the action is triggered

It will be called with the http context as a parameter

You are in charge of doing authorization checks if you want to restrict which backoffice users can use this action

:::note
If you want the frontend to show a success toast message, you can return a 200 status code with an object like this

```ts
return { message: 'Success message' }
```

:::

:::note
If you want the frontend to show an error toast message, you can return a >= 400 status code with an object like this

```ts
return response.badRequest({ error: 'Error message' })
```

:::
14 changes: 13 additions & 1 deletion docs/src/content/docs/reference/views/models/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ virtualColumns: [
],
```

### globalActions

User interface buttons that trigger an action relative to a table globally

See [global actions](/adomin/reference/views/models/global-actions)

### instanceActions

User interface buttons that trigger an action relative to a table and a specific line

See [instance actions](/adomin/reference/views/models/instance-actions)

## Types of fields

### [String field](/adomin/reference/views/models/string/)
Expand Down Expand Up @@ -261,7 +273,7 @@ virtualColumns: [

#### Waiting for ~~love~~ docs

As you can see, the documentation is not ready for many field types, while you wait for propper documentation, see the type definitions:
Sometimes, the documentation is behind of developments, while you wait for propper documentation, you can always check the adomin fields type definitions:

import { FileTree } from '@astrojs/starlight/components'

Expand Down
98 changes: 98 additions & 0 deletions docs/src/content/docs/reference/views/models/instance-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
title: Model Instance Actions
sidebar:
order: 1031
description: Adomin model instance actions reference
---

In the user interface **instanceActions** are buttons that trigger an action relative to a table and a specific line

![field image](~/assets/images/reference/models/actions/instance_actions.png)

Here is the code needed to add this button to the user interface

```ts
{
instanceActions: [
{
name: 'duplicate-idea',
tooltip: "Dupliquer l'idée",
icon: 'copy',
iconColor: 'brown',
action: async (ctx) => {
const idea = await Idea.findOrFail(ctx.params.id)

await Idea.create({
title: idea.title,
description: idea.description,
userId: idea.userId,
})

return { message: 'Duplication effectuée' }
},
},
],
}
```

## Config

**instanceActions** is an array of objects, each object representing an action, here are the parameters for each action

### name

Name of the action, this will be used to identify the action, must be unique for the model

e.g. the API endpoint generated will look like this if name = 'duplicate-idea', and the id of the row is 1

POST /adomin/api/actions/Idea/duplicate-idea/1

### tooltip

Tooltip shown on the frontend, when hovering the button

### icon

Icon name, by default this uses Tabler icons

You can browse the list of available icons at: https://tabler.io/icons

### iconColor

Color of the icon, this will be passed as the css color property, so you can use any valid css color

### action

Function to execute when the action is triggered

It will be called with the http context as a parameter

You are in charge of doing authorization checks if you want to restrict which backoffice users can use this action

Your action function will receive the model instance primary key inside the `params` object like this

```ts
const yourActionFunction = async (ctx) => {
const primaryKeyValue = ctx.params.id
// do something with the primary key value
return { message: '🎉' }
}
```

:::note
If you want the frontend to show a success toast message, you can return a 200 status code with an object like this

```ts
return { message: 'Success message' }
```

:::

:::note
If you want the frontend to show an error toast message, you can return a >= 400 status code with an object like this

```ts
return response.badRequest({ error: 'Error message' })
```

:::

0 comments on commit 33892a8

Please sign in to comment.