jearbear

jearbear

I have forms in my app modeled using LiveComponents (they’re in modals) that mostly manage their own state. The following is an example of one:

    <.live_component
      module={TransactionForm}
      id="..."
      transaction={...}
      open={@live_action in [:create, :edit]}
      on_success={&push_patch(...)}
      on_cancel={JS.patch(...)}
    />

The live component will fetch data in the mount callback that’s required for things like select inputs (e.g. list of merchants, accounts, etc.).

I haven’t really figured out a good pattern to handle refreshing of data managed by live components. For example, if I have another form in the live view that creates a new merchant, I would want the other forms in the page to refresh their state to reflect the new merchant. It’s easy to do this at the live view level using Phoenix PubSub. I can emit an event when the change is triggered and refresh the live view’s state in a handle_info callback.

Since live components don’t have this capability, I’ve identified a few alternatives:

  1. Manage all this state at the live view level. I tried this but it makes the component really hard to work with since it ends up requiring each live view to load a bunch of state just to pass through to the live component. To me it kind of defeats the point of the live component in the first place.

  2. Load data in update callback instead of on mount. This will ensure that the live component will always have the latest data since it will just refetch it every time it opens / closes. The downside is that most of the time the refetching was not needed.

  3. Use send_update as a mechanism to send an event to the live component to tell it to reload state. I don’t like the idea of the caller literally settings assigns that it shouldn’t otherwise know about, but I did consider exposing a function from the live component module that could be called in the live view’s handle_info callback. It’s a little manual, but probably would work? An example:

defmodule TransactionForm do
  def refresh_data(id) do
    merchants = ...
    send_update(pid, id, merchants: ...)
  end
end

If live components could handle their own events I think that would solve the problem for me, but I understand the limitations of that given they share the process with the parent live view. Very interested in hearing how others are addressing this problem!

Showing Posts 1 to 2

axelclark

axelclark

I use send_update. It is the example in the Live View docs for your scenario.

jearbear

jearbear OP

Makes sense! I ended up going with a hook that’s defined in the live component as to avoid leaking implementation details about internal state.

  def attach_refresh_hooks(socket, id) do
    attach_hook(socket, :refresh_transaction_form_data, :handle_info, fn _, socket ->
      send_update(__MODULE__, id: id, _refresh: true)
      {:cont, socket}
    end)
  end

Which gets called in the mount callback of live views that use it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews