When updating records, correctly refresh the list view

This commit is contained in:
Claudio Ortolina
2025-01-24 12:12:58 +00:00
parent 95870aa054
commit ed5447660d
3 changed files with 53 additions and 49 deletions
@@ -63,7 +63,7 @@ defmodule MusicLibraryWeb.CollectionLive.Index do
defp apply_action(socket, :index, params) do
query = params["query"] || ""
order = params["order"] || "alphabetical"
order = parse_order(params["order"] || "alphabetical")
total_records = Collection.search_records_count(query)
record_list_params =
@@ -72,27 +72,12 @@ defmodule MusicLibraryWeb.CollectionLive.Index do
|> merge_order(order)
|> merge_pagination(params, total_records)
offset = page_to_offset(record_list_params.page, record_list_params.page_size)
opts = [limit: record_list_params.page_size, offset: offset, order: parse_order(order)]
records =
Collection.search_records(query, opts)
socket
|> assign(:page_title, gettext("Collection"))
|> assign(:record, nil)
|> assign(:record_list_params, record_list_params)
|> stream(:records, records, reset: true)
load_and_assign_records(socket, record_list_params)
end
@impl true
def handle_info({MusicLibraryWeb.RecordLive.FormComponent, {:saved, record}}, socket) do
# TODO: when a record is updated, there's no guarantee that 1) it will end
# up in the same position and 2) it would still be visible given current
# filters. Instead of inserting into the stream, we should reload the
# collection with the same params.
{:noreply, stream_insert(socket, :records, record)}
def handle_info({MusicLibraryWeb.RecordLive.FormComponent, {:saved, _record}}, socket) do
{:noreply, load_and_assign_records(socket, socket.assigns.record_list_params)}
end
@impl true
@@ -142,12 +127,31 @@ defmodule MusicLibraryWeb.CollectionLive.Index do
end
end
defp load_and_assign_records(socket, record_list_params) do
offset = page_to_offset(record_list_params.page, record_list_params.page_size)
opts = [
limit: record_list_params.page_size,
offset: offset,
order: record_list_params.order
]
records =
Collection.search_records(record_list_params.query, opts)
socket
|> assign(:page_title, gettext("Collection"))
|> assign(:record, nil)
|> assign(:record_list_params, record_list_params)
|> stream(:records, records, reset: true)
end
defp merge_query(record_list_params, query) do
Map.put(record_list_params, :query, query)
end
defp merge_order(record_list_params, order) do
Map.put(record_list_params, :order, parse_order(order))
Map.put(record_list_params, :order, order)
end
defp merge_pagination(record_list_params, params, total_records) do
@@ -70,29 +70,12 @@ defmodule MusicLibraryWeb.WishlistLive.Index do
|> merge_query(query)
|> merge_pagination(params, total_records)
offset = page_to_offset(record_list_params.page, record_list_params.page_size)
records =
Wishlist.search_records(query,
limit: record_list_params.page_size,
offset: offset,
order: record_list_params.order
)
socket
|> assign(:page_title, gettext("Wishlist"))
|> assign(:record, nil)
|> assign(:record_list_params, record_list_params)
|> stream(:records, records, reset: true)
load_and_assign_records(socket, record_list_params)
end
@impl true
def handle_info({MusicLibraryWeb.RecordLive.FormComponent, {:saved, record}}, socket) do
# TODO: when a record is updated, there's no guarantee that 1) it will end
# up in the same position and 2) it would still be visible given current
# filters. Instead of inserting into the stream, we should reload the
# wishlist with the same params.
{:noreply, stream_insert(socket, :records, record)}
def handle_info({MusicLibraryWeb.RecordLive.FormComponent, {:saved, _record}}, socket) do
{:noreply, load_and_assign_records(socket, socket.assigns.record_list_params)}
end
@impl true
@@ -157,6 +140,23 @@ defmodule MusicLibraryWeb.WishlistLive.Index do
end
end
defp load_and_assign_records(socket, record_list_params) do
offset = page_to_offset(record_list_params.page, record_list_params.page_size)
records =
Wishlist.search_records(record_list_params.query,
limit: record_list_params.page_size,
offset: offset,
order: record_list_params.order
)
socket
|> assign(:page_title, gettext("Wishlist"))
|> assign(:record, nil)
|> assign(:record_list_params, record_list_params)
|> stream(:records, records, reset: true)
end
defp merge_query(record_list_params, query) do
Map.put(record_list_params, :query, query)
end
+9 -9
View File
@@ -31,7 +31,7 @@ msgstr ""
#: lib/music_library_web/components/layouts/app.html.heex:14
#: lib/music_library_web/live/artist_live/show.html.heex:64
#: lib/music_library_web/live/collection_live/index.ex:83
#: lib/music_library_web/live/collection_live/index.ex:143
#: lib/music_library_web/live/collection_live/show.ex:115
#: lib/music_library_web/live/collection_live/show.ex:132
#, elixir-autogen, elixir-format
@@ -59,12 +59,12 @@ msgstr ""
msgid "Edit"
msgstr ""
#: lib/music_library_web/live/collection_live/index.ex:133
#: lib/music_library_web/live/collection_live/index.ex:140
#: lib/music_library_web/live/collection_live/index.ex:118
#: lib/music_library_web/live/collection_live/index.ex:125
#: lib/music_library_web/live/stats_live/index.ex:53
#: lib/music_library_web/live/stats_live/index.ex:59
#: lib/music_library_web/live/wishlist_live/index.ex:132
#: lib/music_library_web/live/wishlist_live/index.ex:139
#: lib/music_library_web/live/wishlist_live/index.ex:115
#: lib/music_library_web/live/wishlist_live/index.ex:122
#, elixir-autogen, elixir-format
msgid "Error importing record"
msgstr ""
@@ -188,15 +188,15 @@ msgstr ""
msgid "Purchased on"
msgstr ""
#: lib/music_library_web/live/collection_live/index.ex:125
#: lib/music_library_web/live/collection_live/index.ex:110
#: lib/music_library_web/live/stats_live/index.ex:45
#: lib/music_library_web/live/wishlist_live/index.ex:124
#: lib/music_library_web/live/wishlist_live/index.ex:107
#, elixir-autogen, elixir-format
msgid "Record imported successfully"
msgstr ""
#: lib/music_library_web/live/record_live/form_component.ex:124
#: lib/music_library_web/live/wishlist_live/index.ex:152
#: lib/music_library_web/live/wishlist_live/index.ex:135
#: lib/music_library_web/live/wishlist_live/show.ex:106
#, elixir-autogen, elixir-format
msgid "Record updated successfully"
@@ -307,7 +307,7 @@ msgstr ""
#: lib/music_library_web/components/layouts/app.html.heex:20
#: lib/music_library_web/live/artist_live/show.html.heex:73
#: lib/music_library_web/live/wishlist_live/index.ex:83
#: lib/music_library_web/live/wishlist_live/index.ex:154
#: lib/music_library_web/live/wishlist_live/show.ex:130
#, elixir-autogen, elixir-format
msgid "Wishlist"