# `LiveSlice.Actions`
[🔗](https://github.com/CountlinkX-Solutions/live_slice/blob/v0.2.1/lib/live_slice/actions.ex#L1)

The server actions of a store, defined once and handled in every LiveView
that attaches the module, so a store's queued actions drain on every page
that renders its bridge, not only on the pages whose LiveView declares the
store with `use LiveSlice`.

    defmodule MyAppWeb.TaskActions do
      use LiveSlice.Actions, store: :tasks, actions: [:add_task, :rename_task]

      @impl LiveSlice.Actions
      def handle_action(:add_task, [title], %{id: mutation_id}, socket) do
        # as LiveSlice.handle_action/4
      end
    end

Attach it with one line, in each LiveView:

    on_mount MyAppWeb.TaskActions

or for every LiveView of a `live_session` in the router:

    live_session :default, on_mount: [MyAppWeb.TaskActions] do
      # ...
    end

and render its bridge in every LiveView that attaches it, so the client
sends the queue on those pages:

    <LiveSlice.bridge id="tasks-store" module={MyAppWeb.TaskActions} />

A layout is a good place only when every LiveView that renders it attaches
the module (the `live_session` above, with its own `layout:`). A connected
LiveView that renders the bridge without attaching the module raises an
`ArgumentError` naming the fix, at its first render: without it, every
queued action would be refused. Elsewhere render `<LiveSlice.bridge
id="tasks-store" store={:tasks} />`, which leaves the queue waiting.

A LiveView that uses `use LiveSlice` for the same store (to mirror its
keys) without declaring its actions leaves them to the module; its bridge
names the module so it sends the queue too:

    <LiveSlice.bridge id="tasks-store" module={__MODULE__} actions={MyAppWeb.TaskActions} />

`handle_action/4` receives the same arguments and returns the same replies
as `c:LiveSlice.handle_action/4`, in the LiveView that rendered the bridge
(its socket and assigns). The attached LiveView may `use LiveSlice` for
another store (its mirror, actions and collaborative keys keep working),
or not use it at all; actions for a store no attached module or `use
LiveSlice` handles are refused with `"unknown_store"`, as before. A
LiveView that declares the same store's actions with `use LiveSlice` and
attaches the module raises at mount. Attaching the module twice (from the
`live_session` and the LiveView) attaches it once.

## Options

  * `:store` (required) - the store name, an atom or a string.
  * `:actions` (required) - the action names, as atoms.

# `handle_action`

```elixir
@callback handle_action(
  action :: atom(),
  args :: list(),
  meta :: LiveSlice.action_meta(),
  socket :: Phoenix.LiveView.Socket.t()
) ::
  {:ok, Phoenix.LiveView.Socket.t()}
  | {:ok, map(), Phoenix.LiveView.Socket.t()}
  | {:error, term(), Phoenix.LiveView.Socket.t()}
  | {:conflict, map(), Phoenix.LiveView.Socket.t()}
  | {:conflict, map(), term(), Phoenix.LiveView.Socket.t()}
```

Runs a server action of the store; see `c:LiveSlice.handle_action/4`.

---

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