# `Alternera.Plugin.Editor.TargetList`
[🔗](https://github.com/10e-ab/alternera-sdk/blob/v0.9.0/alternera_plugin_editor/lib/alternera/plugin/editor/target_list.ex#L1)

The **To** of a step whose plugin registers several outputs
(`Alternera.Plugin`'s `outputs/0`, read by `Plugin.Targets`): a
list of rows, each one output written to one place. Mounted by the
`RequestEditor`, `Validate.Editor` and the `Script.Editor`.

Each row has:

  * an **output** select of the plugin's registered outputs, and only
    those;
  * a **key** field for a keyed output (one header by name, one name a
    script sets), with the names the editor knows as suggestions
    (`<.suggest_input>`: the same names for every keyed output, or each
    output's own when the editor hands a map of output name to names);
    blank means the whole set;
  * a `LocationPicker` (role `target`) offering only the kinds that fit
    the output (`kinds/3`): a status goes to a variable or the response
    status, one header to a variable or one header of the side, the
    whole headers to a variable, a body or a value anywhere the side
    allows and, where the response is written, to the response status
    too (a body whole, never a JSON path in it, for the outputs the
    editor names in `whole_body`);
  * a remove button.

**Add target** appends a row of the next output no row writes yet (all
written: the first keyed output, which may repeat with another key,
else the first), to a suggested variable (`suggested_name/2`); given
keys per output (`add/3`), a keyed row starts with the first of them no
row of that output has yet.

A row is stored as `Targets` reads it, `%{"output" => name, "key" =>
text, "to" => location}` (no `"key"` when blank), always with its `to`
explicit. A step with the single `"target"` it had before shows as one
row of the default output (`rows/2`).

The inputs are named `<prefix>-to-<row>-output`, `<prefix>-to-<row>-key`
and `<prefix>-to-<row>-loc-target-in|address` (rows from 0) and report
through the editor's `targets` event, whose params go to `changed/5`;
the buttons send `add_target` and `remove_target` (`phx-value-row`),
for `add/3` and `remove/2`. A save error about a row (`To row N …`,
rows from 1, after whatever prefix the plugin puts in front, read by
`Plugin.Targets.row_error/1` and split by `row_errors/3`, on
`RowErrors`) shows under
it; one about the list as a whole, or about a row the list does not
have, above the rows.

# `add`

```elixir
@spec add([map()], [map()], %{optional(String.t()) =&gt; [String.t()]}) :: [map()]
```

The rows with one more: the next output no row writes, else the first
keyed output, else the first, to a suggested variable. `keys` maps an
output's name to the keys suggested for it: a keyed row starts with the
first of them no row of that output has yet.

# `changed`

```elixir
@spec changed(map(), String.t(), [map()], String.t(), [map()]) ::
  {:ok, [map()]} | :error
```

The rows after a `targets` event: `{:ok, rows}` with the changed field
of its row stored, `:error` when the event is not about a row.

# `key_suggestions`

```elixir
@spec key_suggestions([String.t()], map()) :: [String.t()]
```

The names a key is suggested from, lower-cased (an output's keys are):
the headers of the step's played call or saved output (`played`), else
the preview exchange's response headers (`PipelinesLive.NameHints`),
else the common ones.

# `kinds`

```elixir
@spec kinds(map(), String.t(), [map()]) :: [{String.t(), String.t()}]
```

The kinds a row's location may take on `side`, as `{value, label}`:
the response status and the response's headers in After and among the
own Implementation steps (`"answer"`), never in Before.

# `list`

The To list: its rows, their errors and Add target.
## Attributes

* `prefix` (`:string`) (required)
* `rows` (`:list`) (required)
* `outputs` (`:list`) (required) - the plugin's registered outputs.
* `side` (`:string`) (required)
* `errors` (`:list`) - Defaults to `[]`.
* `hints` (`:map`) (required) - the location fields' suggestions (`LocationPicker.hints/2`).
* `key_suggestions` (`:any`) - the names a key is suggested from: a list for every keyed output, or a map of output name to its list. Defaults to `[]`.
* `whole_body` (`:list`) - the outputs whose body location is the whole body, never a JSON path in it. Defaults to `[]`.
* `target` (`:any`) (required)
* `input_class` (`:string`) (required)
* `select_class` (`:string`) (required)
* `empty_note` (`:string`) - what an empty To list means for this step. Defaults to `"Nothing is written: add a target."`.

# `remove`

```elixir
@spec remove([map()], term()) :: {:ok, [map()]} | :error
```

The rows without the one at `at` (from 0), or `:error`.

# `row_errors`

```elixir
@spec row_errors([String.t()] | nil, non_neg_integer(), (String.t() -&gt; String.t())) ::
  {%{required(non_neg_integer()) =&gt; String.t()}, [String.t()]}
```

`errors` split by the To row they are about: `RowErrors.split/4` with
the label `To row` (`Plugin.Targets.row_error/1`), `reword` applied to
each row's reason before it is capitalised.

# `rows`

```elixir
@spec rows(map(), [map()]) :: [map()]
```

The rows of `settings`: its `"targets"` (the maps in it), or the legacy
`"target"` as one row of the default output, or none.

# `suggested_name`

```elixir
@spec suggested_name(map(), [map()]) :: String.t()
```

The variable a row is suggested to write: `response` for a body,
`request_status` for a status, `request_<key>` for one header (other
than letters, digits and `_` becomes `_`), `request_headers` for all of
them (a Request's outputs, the call's); for any other output its key
when it has one, else its name (a Validate's `valid`, a Script's
`order_id`); with `_2`, `_3`… when another of `rows` writes a variable
of that name.

---

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