The contract of a resource package's own UI: components a package ships
for its resources, which the proxy draws in its generated Resources page,
in place of it, at a page of the package's own, or as a Workbench tab.
docs/WRITING_RESOURCE_PLUGINS.md ("Your own UI") is the guide; the
declarations are the ui section of alternera_resource and the
alternera_ui section of the domain (Alternera.Resource.Info.ui/1).
Kinds
| Kind | Declared as | Written as | Gets |
|---|---|---|---|
| Cell | cell :field, M | use UI, :slot | value, record, field, ctx |
| Form field | field :attribute, M | use UI, :slot | field (a form field), record, ctx, errors, options |
| Form | form M | use UI, :component | form, record, fields, options, ctx |
| Panel | panel :id, "Label", M | use UI, :component | record, ctx |
| Action | action :id, "Label" | Ash action or use UI, :handler | see Alternera.Resource.UI.ActionHandler |
| View | view :id, "Label", M | use UI, :component | records, count, truncated?, ctx |
| Screen | screen M | use UI, :surface | see "Surfaces" |
| Package page | page :id, "Label", M (domain) | use UI, :surface | see "Surfaces" |
| Workbench item | workbench_item :id, "Label", M | use UI, :surface | see "Surfaces" |
Every component gets ctx, an Alternera.Resource.UI.Context, and
must ignore assigns it does not know: the host only ever adds them within
a contract version (version/0).
Surfaces
A screen, a package page and a Workbench item are the same thing to
write: a LiveComponent that use Alternera.Resource.UI, :surface. It
gets id (put it on the root element), prefix (put it before every DOM
id it draws: the Workbench can draw two of a kind side by side), ctx,
params (its own state, string keys, as put_params/2 last left it) and
arg (what a Workbench item was opened on; nil elsewhere). A Workbench
item may also implement Alternera.Resource.UI.Surface's optional
callbacks (load/2, title/1, request_close/1), and then gets the
loaded state too.
A LiveComponent cannot receive messages or own the URL, so it asks the host, which is the same in every place:
put_params/2: keep state where a reload restores it (the URL on a page, the tab in the Workbench); the newparamscome back throughupdate/2, as strings.notify/3: a notification from the host, the one way a package sends one (an action's own outcome shows in its dialog instead).subscribe/2andbroadcast/3: messages between a package's components, scoped to the package and the tenant; one arrives throughupdate/2as%{message: {topic, message}}, once, at each component that subscribed, until the reader leaves the page.open/2: open a Workbench item, a record, a page or a resource.set_title/2: the tab's title, or the page heading.records_changed/1: what the host shows of the records is stale.
Records
Read and write through Alternera.Resource.UI.Records (the page's
own way, through the host) or Ash directly with opts/1. Either way the
call carries the context's tenant and actor, so the resource's policies
decide; a package domain is authorize: :always by default, so a call
cannot skip them. Work on the system's behalf uses system_opts/1.
Look
use imports Alternera.UIKit (icon, callout, badge,
copy_button, disclosure) and Alternera.Resource.UI.Components
(button, card, empty, value, record_table, actions,
action_button). Write a status message as <.callout>, never as a
tinted block.
What the proxy draws
The Resources page draws a screen, cells, panels, actions, views and
package pages, and the Workbench draws Workbench items. Form fields and
forms are declared and checked (Alternera.Resource.UI.Check) but
not drawn yet.
Summary
Functions
Sends message to the components subscribed to topic, in this package and tenant, on every node.
What a module declares itself as (use Alternera.Resource.UI, kind):
{:ok, %{kind, version}}, or :error for a module that is not a
component of this contract (or is not loaded).
Sends a notification: kind is :info, :success, :warning or
:error. The host shows it wherever the reader is, also after they
moved on from the page that sent it.
Opens something else: {:page, page_id} (a page of the package) or
{:resource, module} (a resource's page). {:workbench, item_id, arg}
and {:record, resource, id} are reserved for the Workbench; the
Resources page answers them with an error notification until it opens them.
The options an Ash call takes for the viewer: [tenant:, actor:].
Keeps params where a reload restores them (the URL, the tab); they come
back as params, with string keys and string values (%{page: 2} comes
back as %{"page" => "2"}). A value is a string, a number, an atom or
nil (nil and "" leave the key out); anything else raises
ArgumentError here, in the component, rather than in the host.
A record's id: the value of its resource's primary key (the first field
of a composite one), as {:id, id} in Alternera.Resource.UI.Records
and a button's phx-value-record take it.
Tells the host that what it shows of the records (a count, the table) is stale.
The Workbench tab's title, or the page's heading.
Receives what broadcast/3 sends to topic in this package and tenant,
through update/2 as %{message: {topic, message}}. Pass the
component's own socket: the host delivers to that component alone, once
however often it subscribes, until the reader leaves the page (the
Workbench tab closes).
The options an Ash call takes for work on the system's behalf in the context's tenant: the system actor, which every resource's policies let through. Use it only for what the viewer did not ask for directly (a count shown to everyone, a scheduled refresh), never to get round a refusal.
The contract version this package implements.
Functions
@spec broadcast(Alternera.Resource.UI.Context.t(), String.t(), term()) :: :ok
Sends message to the components subscribed to topic, in this package and tenant, on every node.
@spec kind(module()) :: {:ok, %{kind: atom(), version: pos_integer()}} | :error
What a module declares itself as (use Alternera.Resource.UI, kind):
{:ok, %{kind, version}}, or :error for a module that is not a
component of this contract (or is not loaded).
@spec notify( Alternera.Resource.UI.Context.t(), :info | :success | :warning | :error, String.t() ) :: :ok
Sends a notification: kind is :info, :success, :warning or
:error. The host shows it wherever the reader is, also after they
moved on from the page that sent it.
@spec open(Alternera.Resource.UI.Context.t(), tuple()) :: :ok
Opens something else: {:page, page_id} (a page of the package) or
{:resource, module} (a resource's page). {:workbench, item_id, arg}
and {:record, resource, id} are reserved for the Workbench; the
Resources page answers them with an error notification until it opens them.
@spec opts(Alternera.Resource.UI.Context.t()) :: keyword()
The options an Ash call takes for the viewer: [tenant:, actor:].
@spec put_params(Alternera.Resource.UI.Context.t(), map()) :: :ok
Keeps params where a reload restores them (the URL, the tab); they come
back as params, with string keys and string values (%{page: 2} comes
back as %{"page" => "2"}). A value is a string, a number, an atom or
nil (nil and "" leave the key out); anything else raises
ArgumentError here, in the component, rather than in the host.
A record's id: the value of its resource's primary key (the first field
of a composite one), as {:id, id} in Alternera.Resource.UI.Records
and a button's phx-value-record take it.
@spec records_changed(Alternera.Resource.UI.Context.t()) :: :ok
Tells the host that what it shows of the records (a count, the table) is stale.
@spec set_title(Alternera.Resource.UI.Context.t(), String.t()) :: :ok
The Workbench tab's title, or the page's heading.
@spec subscribe(Phoenix.LiveView.Socket.t(), String.t()) :: :ok
Receives what broadcast/3 sends to topic in this package and tenant,
through update/2 as %{message: {topic, message}}. Pass the
component's own socket: the host delivers to that component alone, once
however often it subscribes, until the reader leaves the page (the
Workbench tab closes).
def handle_event("watch", _params, socket) do
UI.subscribe(socket, "rates")
{:noreply, socket}
end
@spec system_opts(Alternera.Resource.UI.Context.t()) :: keyword()
The options an Ash call takes for work on the system's behalf in the context's tenant: the system actor, which every resource's policies let through. Use it only for what the viewer did not ask for directly (a count shown to everyone, a scheduled refresh), never to get round a refusal.
@spec version() :: pos_integer()
The contract version this package implements.