sreyansjain

sreyansjain

I am trying to use the main branch of phoenix liveview to test the new reset feature on streams
In my mix.exs I have

{:phoenix_live_view,
       github: "phoenixframework/phoenix_live_view", branch: "main", override: true}

I have created a simple liveview example for a phonebook.
I have a list of names and an index at the top. Clicking on the index should load the contacts that start with the Index letter.
Whenever the index letter is clicked, i load all the relevant contacts, put them in the stream and set the reset flag to true.
This is working fine.

The Problem

Clicking on each contact opens up a modal with the contact details.
This works fine for the first time. Click on the modal, the modal appears.

But whenever the stream gets reset (after clicking a index letter), clicking on the contact brings up the modal, but the stream gets cleared and all contacts disappear.
I am not able to figure out why this is happening.
I tried to replace the stream, with normal assigns and everything works like it should (the modal appears and the contacts do not get cleared).

Can some one please advise as to why is this happening?

Full code is below

defmodule StreamsTestWeb.UserLive.Index do
  use StreamsTestWeb, :live_view

  @impl true
  def render(assigns) do
    ~H"""
    <div class="h-20 flex items-center space-x-12 border-b border-zinc-200 px-4 lg:px-8">
      <div class="text-lg font-bold">Users</div>
    </div>
    <div class="tabs flex items-center gap-2 text-xs font-medium px-r lg:px-8 mt-8">
      <.tab :for={tab <- @tabs} tab={tab} selected_tab={@selected_tab} />
    </div>
    <div :if={@term != ""} class="flex items-center gap-4 text-sm px-4 lg:px-8 mt-6">
      <div class="text-zinc-500">Searching <em class="font-bold text-zinc-700"><%= @term %></em></div>
      <button
        type="button"
        class="flex items-center gap-2 text-xs text-zinc-400 hover:text-zinc-600"
        phx-click="clear_search"
      >
        <.icon name="hero-x-circle" class="w-5 h-5" />
      </button>
    </div>
    <div class="px-4 lg:px-8">
      <.table
        id="users"
        rows={@streams.users}
        row_click={fn {_id, u} -> JS.push("show_user", value: %{id: u.id}) end}
      >
        <:col :let={{_id, s}} label="Name">
          <%= s.name %>
        </:col>
      </.table>
    </div>
    <div id="modal_box">
      <.modal :if={@user} id="detail-modal" show on_cancel={JS.push("hide_user")}>
        <.header><%= @user.name %></.header>
      </.modal>
    </div>
    """
  end

  @impl true
  def mount(_params, _session, socket) do
    socket =
      socket
      |> assign(:tabs, get_tabs())
      |> assign(:selected_tab, "All")
      |> assign(:term, "")
      |> assign(:user, nil)
      |> stream(:users, get_users)

    {:ok, stream(socket, :users, [])}
  end

  attr(:tab, :string, required: true)
  attr(:selected_tab, :string, required: true)

  def tab(assigns) do
    ~H"""
    <div
      class={[
        "w-6 h-6 inline-flex items-center justify-center rounded cursor-pointer",
        @tab == @selected_tab && "text-green-700 bg-green-100",
        @tab != @selected_tab && "text-zinc-500 bg-transparent hover:text-zinc-700 hover:bg-zinc-100"
      ]}
      phx-click="load_contacts"
      phx-value-index={@tab}
    >
      <%= @tab %>
    </div>
    """
  end

  @impl true
  def handle_event("load_contacts", %{"index" => index}, socket) do
    users = get_users() |> Enum.filter(fn s -> String.starts_with?(s.name, index) end)

    socket =
      socket
      |> assign(:selected_tab, index)
      |> assign(:term, "")
      |> assign(:user, nil)
      |> stream(:users, users, reset: true)

    {:noreply, socket}
  end

  def handle_event("show_user", %{"id" => id}, socket) do
    socket =
      assign(
        socket,
        :user,
        Enum.find(get_users, fn u -> u.id == id end)
      )

    {:noreply, socket}
  end

  def handle_event("hide_user", _params, socket) do
    socket =
      assign(
        socket,
        :user,
        nil
      )

    {:noreply, socket}
  end

  defp get_tabs() do
    [
      "A",
      "B",
      "C",
      "D",
      "E",
      "F",
      "G",
      "H",
      "I",
      "J",
      "K",
      "L",
      "M",
      "N",
      "O",
      "P",
      "Q",
      "R",
      "S",
      "T",
      "U",
      "V",
      "W",
      "X",
      "Y",
      "Z"
    ]
  end

  def get_users() do
    [
      %{id: 1, name: "A Name"},
      %{id: 2, name: "B Name"},
      %{id: 3, name: "C Name"},
      %{id: 4, name: "D Name"},
      %{id: 5, name: "E Name"},
      %{id: 6, name: "F Name"},
      %{id: 7, name: "G Name"},
      %{id: 8, name: "H Name"},
      %{id: 9, name: "I Name"},
      %{id: 10, name: "J Name"}
    ]
  end
end

Showing Posts 1 to 9

sreyansjain

sreyansjain OP

A video to better understand the issue

sreyansjain

sreyansjain OP

This is happening

  1. Do some action to reset the stream using the reset: true option in the stream function. The stream would update and the DOM would be changed accordingly.
  2. Do any other action that updates the socket (other than the stream), if that causes dom changes, then the stream would get cleared.
sreyansjain

sreyansjain OP

One workaround is to make stream a part of the assigns by doing a delete operation for a non existent item.

def handle_event("show_user", %{"id" => id}, socket) do
    socket =
      assign(
        socket,
        :user,
        Enum.find(get_users, fn u -> u.id == id end)
      )
      |> stream_delete(:users, %{id: -1})

    {:noreply, socket}
  end

Doing this makes the contacts not go away.

But i am still not able to understand why is this happening?

chrismccord

chrismccord

Creator of Phoenix

Are you positive the table’s tbody has phx-update="stream" ?

sreyansjain

sreyansjain OP

Yes.

<tbody id="users" phx-update="stream" class="relative divide-y divide-zinc-100 border-t border-zinc-200 text-sm leading-6 text-zinc-700">
      
    
      <tr id="users-32" class="group hover:bg-zinc-50" data-phx-stream="0">
        <td class="relative p-0 ">
          <div class="block py-4 pr-6 ">
            <span class="absolute -inset-y-px right-0 -left-4 group-hover:bg-zinc-50 sm:rounded-l-xl"></span>
            <span class="relative font-semibold text-zinc-900">
              
      Ajay-Accounts
    
            </span>
          </div>
        </td>
      </tr>
    </tbody>
cjbottaro

cjbottaro

chrismccord

chrismccord

Creator of Phoenix

This is fixed on main. Thank you!

sreyansjain

sreyansjain OP

Thank you so much @chrismccord
More power to you :pray:

Alvinkariuki

Alvinkariuki

This aged well, thank you

— 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
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
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
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
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews