pedroperrone

pedroperrone

How to remove elements using temporary assigns and phx-update?

Hello, Elixir Forum

I’ve recently started playing around with Phoenix Live View and read about the use of temporary assigns in sockets to allow us to reduce the amount of memory used by the server. I’m trying to build a todo list and am using the list of tasks as a temporary assign. For my list of tasks component I’m using phx-update=“prepend”. In this scenario, adding and updating tasks work perfectly. However, I got stuck in the delete feature. If the option phx-update=“prepend” only adds and updates elements, how can I remove them from the list? If it can’t be done with the prepend option, how can I have the benefits of temporary assigns and be able to perform deletions at the same time?

Thanks in advanced :wink:

Most Liked

sfusato

sfusato

In this case, where updating happens frequently and you have a large collection as opposed to a delete every once in a while on a rather small collection, I would remove the item within an updated method hook placed on the list. Something like:

updated() {
    this.el.firstElementChild.remove()
    // or
    this.el.lastElementChild.remove()
}

Since updated gets called every time you add a new item, it works beautifully as you only have to remove either the first or last depending if you either append or prepend.

To arbitrarily remove a specific item, you could add a data attribute to the hook passing in the selector of the node to delete: data-deleted="<%= @deleted %>"

sfusato

sfusato

You could fetch the (new) to-do list and use phx-update="replace". Then on all the other actions you would go back to phx-update="prepend".

srowley

srowley

I have been thinking about the same thing in the context of charting. Imagine a line plot of time series data that is updated every second, and I want to show the last 24 hours. For that use case I really do not want to replace all 86,400 data points every second. Temporary assigns seem like the obvious solution, except for the fact that I don’t think that I can delete the oldest data point after appending a new one. If there is a way to do that/work around it I would be really interested in that.

Last Post!

snap

snap

I’m trying something similar with rendering a list of (unseen) notifications that can be marked as seen.
When I try rendering only unseen notifications using unless notification.seen it doesn’t update the list because of phx-update="prepend".

When I set a seen class applying display: none; to the notification hides, but I would prefer to remove it and hit the else statement rendering the “No notifications” message.

What this require fetching the whole notifications list again and update it by temporary setting phx-update="replace"?

Recommendations are welcome, thanks in advance!

<div id="notifications-dropdown">
  <%= if Enum.any? @notifications do %>
  
    <ul phx-update="prepend" id="notification-list">
      <%= for notification <- @notifications do %>
        <%= unless notification.seen do %>
          <li id="notification-#{notification.id}" class="notification">
            <p><%= notification.message %></p>
            <button phx-click="toggle-seen"
                    phx-value-id="<%= notification.id %>">
              Mark as seen
            </button>
          </li>
        <% end %>
      <% end %>
    </ul>

  <% else %>
    <p>No notifications</p>
  <% end %>
</div>
def handle_event("toggle-seen", %{"id" => notification_id}, socket) do
  notification = Repo.get(Notification, notification_id)

  {:ok, notification} =
    Notifications.toggle_seen_status(
      notification,
      %{seen: !notification.seen},
      socket.assigns.user
    )
    
  socket =
    update(
      socket,
      :notifications,
      fn notifications -> [notification | notifications] end
    )

  {:noreply, socket}
end

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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

We're in Beta

About us Mission Statement