I’m currently using two separate flash keys to display dynamic message content in a flash message (one key for part of the flash message and another key for the “body”).
I noticed that it works doing it that way, but then I have to either remove the phx-value-key
in order to clear all the flash messages using the lv:clear-flash
. This then removes other flash messages that may be present but perhaps I don’t want them to also be cleared.
(apologies I’m very tired and writing this before crashing)
So, I noticed this in channel.ex:
defp view_handle_event(%Socket{} = socket, "lv:clear-flash", val) do
case val do
%{"key" => key} -> {:noreply, Utils.clear_flash(socket, key)}
_ -> {:noreply, Utils.clear_flash(socket)}
end
end
So, would it be worthwhile to handle a list of keys in, say, the Utils.clear_flash/2?
def clear_flash(%Socket{} = socket, key) do
key = flash_key(key)
new_flash = Map.delete(socket.assigns.flash, key)
socket = assign(socket, :flash, new_flash)
update_in(socket.private.__changed__[:flash], &Map.delete(&1 || %{}, key))
end
# Add this function to handle keys map (%{"key" => "info", "key2" => "info-body"})
def clear_flash(%Socket{} = socket, keys) do
keys = Enum.into(keys, [], fn {_key, val}-> flash_key(val) end)
new_flash = Map.drop(socket.assigns.flash, key) end)
socket = assign(socket, :flash, new_flash)
update_in(socket.private.__changed__[:flash], &Map.delete(&1 || %{}, key))
end
And be passing in the key map in channel.ex
:
# Update to pass the keys map (%{"key" => "info, "key2" => "info-body"})
defp view_handle_event(%Socket{} = socket, "lv:clear-flash", val) do
case val do
%{"key" => key} -> {:noreply, Utils.clear_flash(socket, key)
keys -> {:noreply, Utils.clear_flash(socket, keys)}
end
end
Anyway, what do people think about being able to handle multiple keys in the lv:clear-flash
? There’s probably a better way.