# `Alternera.Resource.UI.Actions`
[🔗](https://github.com/10e-ab/alternera-sdk/blob/v0.9.0/alternera_resource_ui/lib/alternera/resource/ui/actions.ex#L1)

The declared actions of a resource (`action :id, "Label"` in its `ui`):
who may run one, what it asks for, and running it. The host's buttons,
its dialog (`Alternera.Resource.UI.ActionRunner`) and the contract
kit's harness all run actions through here, so an action behaves the same
wherever it is drawn.

An action with `run:` runs that Ash action of the resource with the
context's tenant and actor: the policies decide, the history records the
change. Its input is the Ash action's public arguments and the attributes
it accepts. An action with `handler:` calls
`c:Alternera.Resource.UI.ActionHandler.handle_action/4` with the
arguments it declares. A selection runs the action once per record, so
each record gets its own policy check and version.

# `input`

```elixir
@type input() :: %{
  name: atom(),
  type: module(),
  constraints: keyword(),
  label: String.t(),
  description: String.t() | nil,
  default: term(),
  required?: boolean()
}
```

An input the dialog asks for.

# `result`

```elixir
@type result() :: %{
  status: :ok | :error,
  message: String.t(),
  failed: [%{key: String.t(), message: String.t()}],
  open: tuple() | nil,
  download: {String.t(), iodata()} | nil
}
```

What running an action came to.

# `allowed?`

```elixir
@spec allowed?(
  Alternera.Resource.Extension.Action.t(),
  Alternera.Resource.UI.Context.t()
) :: boolean()
```

Whether the viewer may run `action`: a read-only action always, any other
only with `can_write?`. The resource's policies still decide when it runs.

# `cast`

```elixir
@spec cast([input()], map(), keyword()) ::
  {:ok, map()} | {:error, %{required(atom()) =&gt; String.t()}}
```

The dialog's params (string keys) cast to the inputs' types:
`{:ok, input}` with atom keys, or `{:error, %{name => message}}`. A blank
value is nil; a required input that is nil is refused; a missing one
takes its default.

With `clear: true` (one record, its values prefilled) an optional input
without a default that the reader emptied is passed as nil, so the
action clears it; otherwise a blank input is left out (unchanged).

# `inputs`

```elixir
@spec inputs(module(), Alternera.Resource.Extension.Action.t()) :: [input()]
```

The inputs the dialog asks for, in order.

# `offered?`

```elixir
@spec offered?(
  Alternera.Resource.Extension.Action.t(),
  struct() | nil,
  Alternera.Resource.UI.Context.t()
) :: boolean()
```

Whether to offer `action` for `record` (nil for the resource or a
selection): allowed, and its `visible` function, if any, says yes. A
`visible` function that raises is logged and hides the action.

# `on`

```elixir
@spec on(
  [Alternera.Resource.Extension.Action.t()],
  Alternera.Resource.Extension.Action.on()
) :: [
  Alternera.Resource.Extension.Action.t()
]
```

The actions of `actions` that act on `on` (`:resource`, `:record` or `:selection`).

# `prefill`

```elixir
@spec prefill([input()], :resource | {:record, struct()} | {:selection, [struct()]}) ::
  map()
```

What the dialog shows first for `target`: on one record, each input
that is one of its attributes holds the record's value (string keys, as
the dialog's params), so an update starts from what is there. Elsewhere
nothing; the inputs show their defaults.

# `run`

```elixir
@spec run(
  module(),
  Alternera.Resource.Extension.Action.t(),
  :resource | {:record, struct()} | {:selection, [struct()]},
  map(),
  Alternera.Resource.UI.Context.t()
) :: result()
```

Runs `action` on `target` (`:resource`, `{:record, record}` or
`{:selection, records}`) with the cast `input`. Every error is logged.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
