dmitriid

dmitriid

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?

Showing Posts 1 to 3

DaAnalyst

DaAnalyst

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

rhcarvalho

rhcarvalho

This shouldn’t be necessary, because the stream helpers can update items in-place based on the item ID.

You could use that to update the state of the single stream entry with a deletion_requested: true, causing the template for the item to re-render and a minimal diff be sent to the client.

In fact this pattern would allow multiple items to be in this pending state simultaneously.

This also reminds me that probably you wouldn’t want to accidently re-add items to a stream during that event handler, or perhaps think about what’s the desired behavior - see stream_insert update_only aka stream_update by SteffenDE · Pull Request #3573 · phoenixframework/phoenix_live_view · GitHub & Add `stream_update` to LiveView Streams

dmitriid

dmitriid OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews