Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression in 0.20 when resetting a stream that contains the same values, but reordered. #2826

Closed
Malian opened this issue Sep 26, 2023 · 11 comments

Comments

@Malian
Copy link
Contributor

Malian commented Sep 26, 2023

Environment

  • Elixir version (elixir -v): 1.14
  • Phoenix version (mix deps): 1.7.7
  • Phoenix LiveView version (mix deps): 0.20
  • Operating system: Ubuntu 20.04
  • Browsers you attempted to reproduce this bug on (the more the merrier): Firefox, Chrome
  • Does the problem persist after removing "assets/node_modules" and trying again? Yes/no: Yes

Actual behavior

With the previous version of Phoenix Live View (0.19.5), when I reset a stream with stream(socket, :my_stream, values, reset: true) in handle_params or handle_event where values are the same as the previous stream but reordered, the stream was reinitialized. It is useful with a table when you order by a column (asc/desc).

With the latest version of Phoenix Live View (0.20.0), the stream is not reset, and the table does not change.

Here is the code that reproduces the behaviour. Below, you will find the simple app that reproduces the bug.

defmodule StreamResetWeb.ResetStreamLive do
  use StreamResetWeb, :live_view

  @users [
    %{id: 1, name: "John", age: 27},
    %{id: 2, name: "Mary", age: 25},
    %{id: 3, name: "Peter", age: 30}
  ]

  def handle_params(_params, _uri, socket) do
    new_stream =
      @users
      |> Enum.shuffle()
      |> IO.inspect(label: "New stream (patch)")

    socket =
      socket
      |> stream(:users, new_stream)

    {:noreply, socket}
  end

  def render(assigns) do
    ~H"""
    <.button phx-click={JS.patch(~p"/reset_stream")}>
      Shuffle (patch)
    </.button>

    <.table id="users" rows={@streams.users}>
      <:col :let={{_id, user}} label="id"><%= user.id %></:col>
      <:col :let={{_id, user}} label="name"><%= user.name %></:col>
      <:col :let={{_id, user}} label="age"><%= user.age %></:col>
    </.table>
    """
  end
end

Here is a working application (0.19.5): https://github.com/Malian/stream_reset_bug
Here is the application upgraded to Phoenix Live View (0.20.0): Malian/stream_reset_bug#1

Expected behavior

The stream has been reset.

@Malian
Copy link
Contributor Author

Malian commented Oct 10, 2023

For those who wants to upgrade to 0.20, here's a workaround that works for me. In my case, I know that all the elements are present on the page, so deleting and adding each element "solves" the problem.

socket
|> then(fn socket ->
  Enum.reduce(items, socket, fn item, socket ->
    socket
    |> stream_delete(:items, item)
    |> stream_insert(:items, item)
  end)
end)

However, this won't work on pages where I use pagination. Not sure how to quickly fix this without tracking the items on the page.

@SteffenDE
Copy link
Collaborator

We stumbled across this as well trying to update to LV 0.20.1 from 0.19.5. We rely on reset: true for rendering paginated, sortable tables. Staying on 0.19 for now.

@chrismccord
Copy link
Member

Closed via ddccaa6

@chrismccord
Copy link
Member

Thanks!

@Malian
Copy link
Contributor Author

Malian commented Oct 17, 2023

I can confirm the commit fixes this issue. Thanks ! ❤️

@cheerfulstoic
Copy link

I seem to be having an issue with reset: true with infinite scroll where the page doesn't get reset when I change filter options for my query. Instead it seems to append the results to the next page.

I might be able to reproduce it later, but I was able to confirm that the problem exists on 0.20.1 and with {:phoenix_live_view, git: "https://github.com/phoenixframework/phoenix_live_view.git"}, (which I believe should be main), but the problem goes away when I downgrade to 0.19.5.

@ivank
Copy link

ivank commented Dec 23, 2023

I was able to replicate this with normal pagination as well (offset/limit)
downgrading back to 0.19.5 also solved the issue for me thanks!

@Malian
Copy link
Contributor Author

Malian commented Dec 23, 2023

@ivank the version 0.20.2 should solve this issue.

@barkerja
Copy link

@ivank the version 0.20.2 should solve this issue.

I've confirmed I still see the issue in 0.20.2

@sodapopcan
Copy link
Contributor

I'm having an issue with regular old offset pagination and filtering. It remembers the positions of the filtered elements and they stick around when filters are reset.

For example, consider this list:

  • Hi there
  • Hello
  • Hello there
  • Oh hi

If I filter on "Hello" the results are:

  • Hello
  • Hello there

And when reseting the filters, which also does a stream reset, I get:

  • Hello
  • Hello there
  • Hi there
  • Oh hi

This does not happen on 0.19.x.

@SteffenDE
Copy link
Collaborator

@sodapopcan #2969 should fix this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants