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.