pedroperrone

pedroperrone

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:

Showing Posts 1 to 4

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.

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

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
— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews