dmitriid

dmitriid

Per-element actions for elements in LiveView stream

Hi all

My go to pattern for handling data recently has been: have a Delete button on item. When user clicks the button, overlay the item with “Are you sure Yes/No”, and only delete the item when the user clicks “Yes”.

For non-streams this is easy.

socket
|> assign(a_list_of_items: items)
|> assign(requested_for_removal: nil)


# .... and then in a handle_event

socket |> assign(requested_for_removal: selected_item_id)

# .... and in heex

<div :for={ item <- @items}>
   <div :if={ item.id != @requested_for_removal }>item data</div>
   <div :if={ item.id == @requested_for_removal}>Are you sure? etc.</div>
</div>

For streams, however, this presents a bit of a problem. Since items are not retained in memory, I need to:

  • fetch the item when delete is clicked
  • find its index in the stream
  • update the stream at that position with data like “requested for removal”
  • and if, for example, I want to only have one such overlay on page, I need to somehow remove it from a previous element

Has someone implemented something similar, and is there a better way?

Perhaps render hidden divs and show them with JS by toggling class names on elements?

Most Liked

DaAnalyst

DaAnalyst

Yes, you could do it the way you suggest - have a hidden deletion element and toggle between the two.

Last Post!

dmitriid

dmitriid

However, in order to update the state of that entry I need to retrieve the full entry, and set deletion_requested: true on it :slight_smile:

So far I’ve gone the JS route with a hidden element.

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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

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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement