Hermanverschooten

Hermanverschooten

Problem with life cycle hooks

I am a bit stumped…

I am trying to attach a life cycle hook to close flash messages automatically after 5 seconds.

I have this code:

defmodule MailManWeb.FlashLive do
  def on_mount(:default, _params, _session, socket) do
    {:cont,
     socket
     |> Phoenix.LiveView.attach_hook(:flash, :handle_params, &send_close/3)
     |> Phoenix.LiveView.attach_hook(:flash, :handle_info, &info/2)}
  end

  def send_close(_params, _uri, socket) do
    Enum.each(Phoenix.LiveView.Utils.get_flash(socket), fn {key, _msg} ->
      Process.send_after(self(), {:close_flash, key}, 5000)
    end)

    {:cont, socket}
  end

  defp info({:close_flash, key}, socket) do
    {:cont, socket |> Phoenix.LiveView.clear_flash(key)}
  end

  defp info(_, socket), do: {:cont, socket}
end

I initially had the Enum.each in the on_mount, but then it wasn’t executed when using a patch navigation.
Problem is now, it is not working on navigate navigation, I get a no function clause matching error on handle_info, it is as if the hook isn’t set.

I tried moving the attach_hook into the send_close function, but then it explodes on patch because the hook is already set.

What am I missing?

Marked As Solved

Hermanverschooten

Hermanverschooten

OMG, I found it and once I did it is so logical!

I thought it might have been a bug introduced in rc of live_view, so I created a test app with 0.20.17. I created the hook added 2 pages with just a button that put_flash and push_navigate, immediately I noticed that upon clicking the flash would close after 5 seonds - great - but I also got

[debug] warning: undefined handle_info in TestHookWeb.Page2Live. Unhandled message: {:close_flash, "info"}

This decided me to try and return {:halt, ...} from info/2 and that fixed it!

Returning {:cont, ...} made live_view continue the event where it failed because I have an handle_info/2 but not for that event.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

We're in Beta

About us Mission Statement