brettinternet

brettinternet

Working in a LiveComponent, is there a way to merge updates to assigns with the existing assigns once the data arrives?

For example, if I have two inputs, a URL input url and a text input title. I’m handling a blur event on the URL input which will asynchronous populate the title text input, but I’d like to allow the user to continue to modify the form as they desire.

def handle_event("url_blur", %{"value" => url}, socket) do
  pid = self()
  
  Task.async(fn ->
    title = fetch_title(url)

    send_update(
      pid,
      __MODULE__,
      socket.assigns
      |> assign(id: socket.assigns.id, loading: false)
      |> update(:changeset, fn c ->
        c
        |> Ecto.Changeset.put_change(:title, title)
      end)
    )
  end)
  
  {:noreply, assign(socket, loading: true)}
end

I set loading to true so I can indicate that to the user and I send an asynchronous update to assigns with a change to the changeset. However, if there is user input on other inputs in the same form/changeset before the asynchronous changeset update arrives, that user input is unfortunately wiped once the change to title finally occurs.

I have attempted a few variations of this, such as adding the values to assigns and then performing the changeset update in the live_component’s update/2. However, I’m not able to find a way to merge the asynchronous changeset. Is there another approach I’m not thinking of?

I haven’t yet tried the independent pubsub approach because I’m not certain it will work any differently, and it will require significant refactoring because my form as a live_component is used in multiple contexts. Will I be required to use a live_view with a pubsub?

Showing Posts 1 to 1

brettinternet

brettinternet OP

Yes, sending a message to the LiveView is the only method to asynchronously patch assigns. send_update will not work for this.

def handle_event("url_blur", %{"value" => url}, socket) do
  pid = self()
  
  Task.async(fn ->
    title = fetch_title(url)

    send(
      pid,
      {:update_changeset, title}
    )
  end)
  
  {:noreply, assign(socket, loading: true)}
end

def handle_info({:update_changeset, title}, socket) do
  changeset = socket.assigns.changeset
    |> Ecto.Changeset.put_change(:title, title)

  {:noreply, socket |> assign(changeset: changeset, loading: false)}
end

def handle_info(_, socket) do
  {:noreply, socket}
end

This is unfortunate because I had hoped to manage state in a LiveComponent.

— 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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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

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
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews