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?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 1- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
brettinternet
Yes, sending a message to the LiveView is the only method to asynchronously patch assigns.
send_updatewill not work for this.This is unfortunate because I had hoped to manage state in a LiveComponent.