# `RiddlerLiveView.Journey`
[🔗](https://github.com/riddler/riddler_live_view/blob/v0.1.0/lib/riddler_live_view/journey.ex#L1)

The LiveComponent a host's LiveView embeds to show one Journey.

A host names the Journey and its own two modules, and this component does
the rest of a visit: it asks the host where the Journey is, draws what
`Riddler.Journey.present/3` answers for it, and hands back what the
visitor submits. It steps nothing and stores nothing: every step is the
host's, through the four callbacks of `Riddler.Journey.Host`.

    <.live_component
      module={RiddlerLiveView.Journey}
      id="registration"
      journey={@journey}
      host={Library.RegistrationHost}
      access={Library.PatronAccess}
      actor={@patron_id}
    />

## Assigns

  * `id` - required, as for every LiveComponent; it is also the prefix of
    every `id` the component's markup derives, unless `id_prefix` says
    otherwise, so two Journeys on one page carry no `id` twice.
  * `journey` - required, the host's name for the Journey, which the
    component passes to the host and never reads.
  * `host` - required, the host's module implementing
    `Riddler.Journey.Host`.
  * `access` - required, the host's module implementing
    `Riddler.Journey.Access`.
  * `actor` - who is asking, the host's own value, which the component
    hands to the access check and never reads.
  * `components` - the component map `RiddlerLiveView.Components.elements/1`
    takes, for the host's own element kinds.
  * `finding_text` and `unknown_text` - passed to the components, as
    `RiddlerLiveView.Components` describes them.
  * `time_zone` - the visitor's time zone, a name the host's configured
    `Calendar.TimeZoneDatabase` resolves, such as `"America/Denver"`:
    a datetime question is drawn as a datetime-local input in it, and the
    offset is added to what the visitor submits, as "A submit" says.
    Where it is nil, or the host's database does not resolve it, a
    datetime question is a plain text input and its value is handed on
    as typed.
  * `waiting_text` and `done_text` - the sentence drawn while the
    execution waits and once it is done.
  * `refused_text` - the sentence drawn in place of a Journey the visitor
    may not see.
  * `id_prefix` - the prefix of every `id` the markup derives, in place of
    `id`.
  * `class` - classes added to the component's own.

`send_update(RiddlerLiveView.Journey, id: id, refresh: true)` asks the
host again where the Journey is and draws the answer; any other update
keeps the answer drawn.

## What is drawn

The component asks the host's `present/1` when it mounts, and draws what
it answers:

  * a screen as a form: `RiddlerLiveView.Components.frame/1` around
    `RiddlerLiveView.Components.elements/1`, then the Back control where
    the moves offer it;
  * waiting as a paragraph with `role="status"`, carrying the answer's
    `"reason"` as `data-riddler-reason`; where the answer carries
    `"retry_after_ms"`, the component sets a LiveView timer,
    `Phoenix.LiveView.send_update_after/4` to itself on a connected
    socket, and asks the host again when it fires; a new answer cancels a
    timer still pending; the component waits at most 4294967295
    milliseconds, about 49.7 days, its own ceiling, so a longer
    `"retry_after_ms"` waits that long;
  * done as a paragraph; the answer's `"result"` is the host's and is not
    drawn.

The component's root carries `data-riddler-journey`, and
`data-riddler-state` with the answer's `"state"`.

## Who may see the Journey

The component asks `Riddler.Journey.Access.check/4` of the host's access
module, with the `actor` and the scope the host's `present/1` answered,
each time before it shows or steps the Journey:

  * with `:present`, after `present/1` answers and before anything of the
    answer is drawn: on mount, on a refresh and on each retry of a
    waiting answer;
  * with `:submit`, before every call of the host's `submit/2`;
  * with `:back`, before every call of the host's `back/2`.

It asks every time rather than once because whether an actor belongs to
a scope is the host's, and can change while the Journey is open: a
membership the host revokes after the mount refuses the next submit. A
change to the `journey`, `host`, `access` or `actor` assign asks the
host's `present/1` and the check again, as on mount.

A refused check draws one refusal, a paragraph carrying
`data-riddler-refused` with the `refused_text`, and nothing of the
Journey, and no submit or back reaches the host after it. A host callback
that answers `{:error, :no_such_journey}` draws the same refusal, so the
markup does not say whether the Journey exists or is not the visitor's.
It is the markup that is the same, not the work behind it: an unknown
Journey is answered by the host's `present/1` before any check is asked,
and a forbidden one by the check after it.

A component given no `access`, or an `access` that is not a loaded module
exporting `member?/2`, raises `ArgumentError` when it mounts: that is the
host's wiring, never a visitor's input, and no refusal is drawn for it.

## A host that answers outside its contract

The component does not crash the LiveView on any of these:

  * a host callback, or the access module's callbacks, that raise, throw
    or exit;
  * a host answer that is not one `Riddler.Journey.Host` names;
  * an answer that is not of a shape `Riddler.Journey.present/3` gives:
    a `"state"` other than `"screen"`, `"waiting"` and `"done"`, or a
    screen answer without a `"screen"` map carrying a string `"key"` and
    an `"elements"` list of maps, without a `"moves"` map, or with a
    `"progress"` that is not a map, or a waiting answer whose
    `"retry_after_ms"` is not an integer of 0 or more;
  * an answer, or a list of findings, whose markup raises when it is
    rendered: the component renders it once, inside this guard, before
    it draws it.

On `present/1` and on the access check the component draws the refusal;
on `submit/2` and `back/2` it keeps what is drawn. Either way it logs one
error line naming the callback and the kind of failure, the exception's
module, `threw`, `exited` or that the answer is outside its contract, and
never the exception's message, the answer or the event's parameters, any
of which may carry what the visitor typed. The retry timer a drawn
answer sets takes only a `"retry_after_ms"` these checks passed, capped
as "What is drawn" says. Anything else that raises is not caught here:
a component map `RiddlerLiveView.Components.elements/1` refuses, or an
option of the wrong type the host passes, is the host's wiring.

## A submit

When the visitor presses a button, the component builds riddler's submit
from the presented answer and the form, never from the form alone:

  * `"screen"` is the key of the screen presented, and a form sent from
    another screen is refused;
  * `"outcome"` is the pressed button's, and is refused unless the moves
    presented list it and a button presented on the screen raises it;
  * `"responses"` carries a response for each question presented on the
    screen, keyed by the question's `"key"`, and the form's value under
    the key of each presented element of a kind the component map names,
    where the form carries one; nothing else the form carries is handed
    on. The pressed button's outcome arrives under `"outcome"` and the
    screen's key under `"riddler-screen"`, so a question keyed
    `"outcome"` cannot be read from the form.

A boolean question's submitted `"true"` becomes `true`, and an unticked
one `false`, since riddler validates a boolean response as one of those.
A choice question under `"many"` with nothing ticked sends `[]`. A secret
question's empty field is left out of the responses, so a value already
held stays held and an empty one is never handed on.

A text question of the `"datetime"` format, where a `time_zone` is in
hand, is drawn as a datetime-local input, which submits a wall time with
no offset, `YYYY-MM-DDTHH:MM` or `YYYY-MM-DDTHH:MM:SS`. The component reads
it in the visitor's zone and hands on riddler's datetime: the wall time as
submitted with the offset in force in that zone at that moment appended,
`+HH:MM` or `-HH:MM`, or `Z` where the offset is zero. A wall time the zone
skips, when the clocks go forward, is read with the offset in force before
the change, so `02:30` on the morning the clocks in Denver go from `02:00`
to `03:00` names the instant the clocks there show as `03:30`. A wall time
the zone passes twice, when the clocks go back, is read with the offset in
force first, the earlier of the two instants. Anything else under the
question's key, an empty field, a value that already carries an offset or
one that is not a wall time, is handed on unchanged, for riddler to
validate.

A screen carrying an element nothing draws, as
`RiddlerLiveView.Components.unknown_kinds/2` answers, is refused whatever
the browser sends. The submit is then checked by
`Riddler.Journey.decode_submit/1` and handed to the host's `submit/2`. The
host validates the responses with `Riddler.Screens.validate_screen/4`,
passing the key of the button whose outcome the submit names, which the
host finds in its own document: a form submit carries the pressed
button's `name` and `value` and nothing else of it.

A host's `{:ok, answer}` is drawn in place of the screen. Its
`{:error, findings}` is drawn beside the questions they name, and focus
moves to the first finding on every refusal, the same first finding
included. Back is handled the same way: the back request is refused unless
it names the screen presented and the moves offered back, and then goes to
the host's `back/2`.

A refused submit or back request calls no host callback and changes
nothing drawn.

## What the component holds

The Journey's name, the host's modules and options, the answer drawn, the
Journey's scope, the findings of the last refusal, a count of refusals and
the reference of the pending retry timer, if a waiting answer set one:
nothing a visitor typed. What a submit carries is handed to the
host and not kept, so a secret question's value is in no assign after the
submit and in no markup. The component logs nothing but the line a
callback outside its contract earns, and the secret
question's input is named under `"riddler-password"`, which Phoenix's
default `:filter_parameters` filters out of the line LiveView logs for
each event; a host that changes that setting keeps a filter that
matches `password`.

---

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