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

An action declared with `handler:` (`action :id, "Label" do handler M
… end`), for work that is not one Ash action of the resource: call a
service, then write many records. An action that is one Ash action
declares `run:` instead and needs no code.

`handle_action/4` gets the action's id, what it acts on, the input the
dialog asked for (cast to the declared arguments' types) and the context.
Read and write with the context (`Alternera.Resource.UI.Records` or
Ash with `Alternera.Resource.UI.opts/1`), so the policies decide, and
log every error before returning it.

It answers:

  * `{:ok, message}` or `{:error, message}`: the message in the action's
    dialog, as a success or an error, and the list reloads;
  * `{:ok, message, open: target}`: `target` opens
    (`Alternera.Resource.UI.open/2`'s targets) and the dialog closes;
  * `{:ok, message, download: {filename, iodata}}`: the message in the
    dialog, and the browser saves the file.

The host sends no notification of an outcome; a handler that wants one
(work that goes on after the dialog, a note to the reader) calls
`Alternera.Resource.UI.notify/3`.

The host runs it in a task of its own (`start_async`), so a slow call
does not hold the page. What it asks of the host while it runs
(`Alternera.Resource.UI.notify/3`, `open/2`, `records_changed/1`)
waits in that task and reaches the host, in order, when it is done.

# `result`

```elixir
@type result() ::
  {:ok, String.t()}
  | {:ok, String.t(),
     [{:open, tuple()}] | [{:download, {String.t(), iodata()}}]}
  | {:error, String.t()}
```

# `target`

```elixir
@type target() :: :resource | {:record, struct()} | {:selection, [struct()]}
```

# `handle_action`

```elixir
@callback handle_action(
  id :: atom(),
  target(),
  input :: map(),
  Alternera.Resource.UI.Context.t()
) ::
  result()
```

---

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