Skip to content
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

Merged
merged 5 commits into from
Aug 28, 2014
Merged

Handle multiple flash messages on same key #304

merged 5 commits into from
Aug 28, 2014

Conversation

hugo
Copy link
Contributor

@hugo hugo commented Aug 25, 2014

No description provided.

persist(conn, put_in(get(conn), [key], message))
messages = case get_all(conn, key) do
[] -> [message]
messages -> messages ++ [message]
Copy link
Member

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

@hugo
Copy link
Contributor Author

hugo commented Aug 25, 2014

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)
Copy link
Member

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 :)

@hugo
Copy link
Contributor Author

hugo commented Aug 25, 2014

You would never guess I only started with this language a week ago... 😖

@chrismccord chrismccord merged commit 8f15779 into phoenixframework:master Aug 28, 2014
@chrismccord
Copy link
Member

<3 <3 <3

Gazler pushed a commit to Gazler/phoenix that referenced this pull request Sep 17, 2015
…l-callbacks

added section on working with lifecycle callbacks
@lustrousgorilla
Copy link

lustrousgorilla commented Sep 13, 2018

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 get_all or its equivalent as a function in the Phoenix.Controller module.

@evaldobratti
Copy link

@chrismccord this PR got deleted in #554
Was that intentional?

@alizain
Copy link

alizain commented Sep 11, 2019

@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.

@PJUllrich
Copy link
Contributor

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 %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants