-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle multiple flash messages on same key #304
Conversation
persist(conn, put_in(get(conn), [key], message)) | ||
messages = case get_all(conn, key) do | ||
[] -> [message] | ||
messages -> messages ++ [message] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use [message | messages]
here
I shouldn't write code at 1*am. This should be better. *now 2 |
@@ -53,7 +53,8 @@ defmodule Phoenix.Controller.Flash do | |||
|
|||
""" | |||
def put(conn, key, message) do | |||
persist(conn, put_in(get(conn), [key], message)) | |||
messages = [message] ++ get_all(conn, key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should still be using [head | tail] style composition here :)
You would never guess I only started with this language a week ago... 😖 |
<3 <3 <3 |
…l-callbacks added section on working with lifecycle callbacks
Was this feature subsequently removed? I stumbled across this issue when trying to figure out how to add multiple flash messages per key, but in the current version of Phoenix I don't see |
@chrismccord this PR got deleted in #554 |
@chrismccord would you be willing to accept the change back in? I read through #554, and I'm at the point where I do need it. |
I ran into this issue as well handle it by storing a list of flash messages per key. Here an example: # In Phoenix
previous_messages = get_flash(conn, key) || []
put_flash(
conn,
key,
[message | previous_messages]
)
# In Phoenix LiveView
import Phoenix.LiveView.Utils, only: [get_flash: 1, get_flash: 2]
messages = get_flash(socket) |> get_flash("#{type}") || []
put_flash(
socket,
type,
[message | messages]
) Then, in my template, I iterate over the messages per key: # In *.eex templates
<%= for type <- [:success, :info, :warning, :error] do %>
<%= for msg <- get_flash(@conn, type) || [] do %>
<p class="alert-<%= type %>"><%= msg %></p>
<% end %>
<% end %>
# In *.leex templates
<%= for type <- [:success, :info, :warning, :error] do %>
<%= for msg <- live_flash(@flash, type) || [] do %>
<p class="alert-<%= type %>"><%= msg %></p>
<% end %>
<% end %> |
No description provided.