neurodynamic

neurodynamic

I have an app with posts and comments, and a “Post a comment” form. The “Post a comment” form is a LiveComponent and most of the code in it has been copied from the default implementation for the way the new/update form works on default-generated index and show pages created via the phx.gen.live generator.

However, one piece of behavior that works fine with generated index/show pages is not working for the comment form and I can’t figure out why. When a user creates a new comment, this code should trigger a flash message:

      {:ok, comment} ->
        notify_parent({:comment_saved, comment})

        {:noreply,
         socket
         |> put_flash(:info, "Comment created successfully")}

but it doesn’t.

If I instead trigger a flash message in the Post show.ex file that the comment form is rendered from, it works fine. That is, this works:

  @impl true
  def handle_info({FormComponent, {:comment_saved, comment}}, socket) do
    {:noreply,
     socket
     |> stream_insert(:comments, comment)
     |> put_flash(:info, "Comment saved successfully")}
  end

But I’d rather the code for that flash message stay in the relevant form_component file.

The LiveComponent is invoked in the show.html.heex file like this:

      <.live_component
        module={FormComponent}
        id={"post-#{@post.id}-new-comment"}
        post={@post}
        current_user={@current_user}
        action={:new}
        comment={@new_comment}
      />

If there’s any other code that would be helpful to provide to help track down why this isn’t working in this case, let me know.

Showing Posts 1 to 5

mindok

mindok

The socket for the live component is different to the socket for the parent live view.

To update the live view socket you will need to send a message to the parent liveview and handle the message there to update the correct socket.

In the live component (in place of put_flash):

 send self(), {:update_flash, {:info, "Comment saved successfully"}}

In the live view:

  def handle_info({:update_flash, {flash_type, msg}}, socket) do
    update_socket = put_flash(socket, flash_type, msg)
    {:noreply, updated_socket}
  end
neurodynamic

neurodynamic OP

That makes sense, but if that’s true, why do flash messages work fine without that in the standard generated phx.gen.live code?

The only obvious difference I can see is that the standard generated code is calling push_patch after the flash message is set. Does that call make a difference to how this works?

ivanhercaz

ivanhercaz

Interesting and really good post by @sevenseacat about how to update messages from a LiveComponen that maybe useful for you:

mindok

mindok

Good question - have you tried commenting out the push_patch from the generated code to see what happens?

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews