From 43fd570d1071a0cbf1aa84f89d3a32315126fda0 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Mon, 9 Feb 2026 11:09:45 +0000 Subject: [PATCH] Extract pagination helpers --- .../live/collection_live/index.ex | 25 +--------- .../live/record_set_live/index.ex | 46 +----------------- .../live/scrobble_rules_live/index.ex | 38 +-------------- .../live/scrobbled_tracks_live/index.ex | 44 ++--------------- .../live/wishlist_live/index.ex | 25 +--------- lib/music_library_web/live_helpers/params.ex | 47 +++++++++++++++++++ 6 files changed, 56 insertions(+), 169 deletions(-) create mode 100644 lib/music_library_web/live_helpers/params.ex diff --git a/lib/music_library_web/live/collection_live/index.ex b/lib/music_library_web/live/collection_live/index.ex index ebdb435e..7957ac5a 100644 --- a/lib/music_library_web/live/collection_live/index.ex +++ b/lib/music_library_web/live/collection_live/index.ex @@ -3,6 +3,7 @@ defmodule MusicLibraryWeb.CollectionLive.Index do import MusicLibraryWeb.Components.BarcodeScanner, only: [barcode_icon: 1] import MusicLibraryWeb.Components.Pagination + import MusicLibraryWeb.LiveHelpers.Params import MusicLibraryWeb.RecordComponents alias MusicLibrary.Collection @@ -158,30 +159,6 @@ defmodule MusicLibraryWeb.CollectionLive.Index do |> 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, order) - end - - defp merge_pagination(record_list_params, params, total_records) do - record_list_params - |> Map.put(:page, parse_int_or_default(params["page"], record_list_params.page)) - |> Map.put( - :page_size, - parse_int_or_default(params["page_size"], record_list_params.page_size) - ) - |> Map.put(:total_entries, total_records) - end - - defp parse_int_or_default(nil, default), do: default - - defp parse_int_or_default(value, _default) when is_binary(value) do - String.to_integer(value) - end - defp parse_order("alphabetical"), do: :alphabetical defp parse_order("purchase"), do: :purchase defp parse_order("release"), do: :release diff --git a/lib/music_library_web/live/record_set_live/index.ex b/lib/music_library_web/live/record_set_live/index.ex index 44ca0385..7f42e441 100644 --- a/lib/music_library_web/live/record_set_live/index.ex +++ b/lib/music_library_web/live/record_set_live/index.ex @@ -2,6 +2,7 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do use MusicLibraryWeb, :live_view import MusicLibraryWeb.Components.Pagination + import MusicLibraryWeb.LiveHelpers.Params import MusicLibraryWeb.RecordComponents, only: [artist_links: 1] alias MusicLibrary.RecordSets @@ -69,42 +70,6 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do end end - defp merge_pagination(params, url_params, total_records) do - page = parse_page(url_params["page"]) - page_size = parse_page_size(url_params["page_size"]) - - params - |> Map.put(:page, page) - |> Map.put(:page_size, page_size) - |> Map.put(:total_records, total_records) - end - - defp merge_query(params, query) do - Map.put(params, :query, query) - end - - defp parse_page(nil), do: 1 - - defp parse_page(page) when is_binary(page) do - case Integer.parse(page) do - {num, ""} when num > 0 -> num - _ -> 1 - end - end - - defp parse_page(_), do: 1 - - defp parse_page_size(nil), do: 20 - - defp parse_page_size(page_size) when is_binary(page_size) do - case Integer.parse(page_size) do - {num, ""} when num in [20, 50, 100] -> num - _ -> 20 - end - end - - defp parse_page_size(_), do: 20 - defp load_and_assign_sets(socket, list_params) do offset = page_to_offset(list_params.page, list_params.page_size) @@ -115,11 +80,8 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do order: list_params.order ) - list_params_with_total = - Map.put(list_params, :total_entries, list_params.total_records) - socket - |> assign(:list_params, list_params_with_total) + |> assign(:list_params, list_params) |> assign(:page_title, gettext("Record Sets")) |> assign(:record_set, nil) |> stream(:record_sets, sets, reset: true) @@ -207,10 +169,6 @@ defmodule MusicLibraryWeb.RecordSetLive.Index do defp parse_order("updated_at"), do: :updated_at defp parse_order(_), do: :updated_at - defp merge_order(params, order) do - Map.put(params, :order, order) - end - defp order_path(list_params, order) do qs = list_params diff --git a/lib/music_library_web/live/scrobble_rules_live/index.ex b/lib/music_library_web/live/scrobble_rules_live/index.ex index cb556149..f95488ba 100644 --- a/lib/music_library_web/live/scrobble_rules_live/index.ex +++ b/lib/music_library_web/live/scrobble_rules_live/index.ex @@ -2,6 +2,7 @@ defmodule MusicLibraryWeb.ScrobbleRulesLive.Index do use MusicLibraryWeb, :live_view import MusicLibraryWeb.Components.Pagination + import MusicLibraryWeb.LiveHelpers.Params alias MusicLibrary.ScrobbleRules alias MusicLibrary.ScrobbleRules.ScrobbleRule @@ -56,38 +57,6 @@ defmodule MusicLibraryWeb.ScrobbleRulesLive.Index do end end - defp merge_pagination(params, url_params, total_records) do - page = parse_page(url_params["page"]) - page_size = parse_page_size(url_params["page_size"]) - - params - |> Map.put(:page, page) - |> Map.put(:page_size, page_size) - |> Map.put(:total_records, total_records) - end - - defp parse_page(nil), do: 1 - - defp parse_page(page) when is_binary(page) do - case Integer.parse(page) do - {num, ""} when num > 0 -> num - _ -> 1 - end - end - - defp parse_page(_), do: 1 - - defp parse_page_size(nil), do: 20 - - defp parse_page_size(page_size) when is_binary(page_size) do - case Integer.parse(page_size) do - {num, ""} when num in [20, 50, 100] -> num - _ -> 20 - end - end - - defp parse_page_size(_), do: 20 - defp load_and_assign_rules(socket, list_params) do offset = page_to_offset(list_params.page, list_params.page_size) @@ -97,11 +66,8 @@ defmodule MusicLibraryWeb.ScrobbleRulesLive.Index do limit: list_params.page_size ) - list_params_with_total = - Map.put(list_params, :total_entries, list_params.total_records) - socket - |> assign(:list_params, list_params_with_total) + |> assign(:list_params, list_params) |> assign(:page_title, gettext("Scrobble Rules")) |> assign(:scrobble_rule, nil) |> stream(:scrobble_rules, rules, reset: true) diff --git a/lib/music_library_web/live/scrobbled_tracks_live/index.ex b/lib/music_library_web/live/scrobbled_tracks_live/index.ex index 7ab31a33..7c297966 100644 --- a/lib/music_library_web/live/scrobbled_tracks_live/index.ex +++ b/lib/music_library_web/live/scrobbled_tracks_live/index.ex @@ -2,6 +2,7 @@ defmodule MusicLibraryWeb.ScrobbledTracksLive.Index do use MusicLibraryWeb, :live_view import MusicLibraryWeb.Components.Pagination + import MusicLibraryWeb.LiveHelpers.Params import MusicLibraryWeb.ScrobbleComponents alias LastFm.Track @@ -55,7 +56,7 @@ defmodule MusicLibraryWeb.ScrobbledTracksLive.Index do @default_tracks_list_params |> merge_query(query) |> merge_order(order) - |> merge_pagination(params, total_tracks) + |> merge_pagination(params, total_tracks, allowed_page_sizes: [20, 50, 100, 200, 500]) load_and_assign_tracks(socket, track_list_params) end @@ -110,51 +111,12 @@ defmodule MusicLibraryWeb.ScrobbledTracksLive.Index do defp parse_order("album"), do: :album defp parse_order(_), do: :scrobbled_at - defp merge_query(params, query), do: Map.put(params, :query, query) - defp merge_order(params, order), do: Map.put(params, :order, order) - - defp merge_pagination(params, url_params, total_records) do - page = parse_page(url_params["page"]) - page_size = parse_page_size(url_params["page_size"]) - - params - |> Map.put(:page, page) - |> Map.put(:page_size, page_size) - |> Map.put(:total_records, total_records) - end - - defp parse_page(nil), do: 1 - - defp parse_page(page) when is_binary(page) do - case Integer.parse(page) do - {num, ""} when num > 0 -> num - _ -> 1 - end - end - - defp parse_page(_), do: 1 - - defp parse_page_size(nil), do: 20 - - defp parse_page_size(page_size) when is_binary(page_size) do - case Integer.parse(page_size) do - {num, ""} when num in [20, 50, 100, 200, 500] -> num - _ -> 20 - end - end - - defp parse_page_size(_), do: 20 - defp load_and_assign_tracks(socket, track_list_params) do tracks = ScrobbleActivity.list_tracks(track_list_params) tracks_empty? = tracks == [] - # Add total_entries for pagination component - track_list_params_with_total = - Map.put(track_list_params, :total_entries, track_list_params.total_records) - socket - |> assign(:track_list_params, track_list_params_with_total) + |> assign(:track_list_params, track_list_params) |> assign(:tracks_empty?, tracks_empty?) |> assign(:page_title, gettext("Scrobbled Tracks")) |> stream(:tracks, tracks, reset: true) diff --git a/lib/music_library_web/live/wishlist_live/index.ex b/lib/music_library_web/live/wishlist_live/index.ex index 8cd4330b..85b0e7ad 100644 --- a/lib/music_library_web/live/wishlist_live/index.ex +++ b/lib/music_library_web/live/wishlist_live/index.ex @@ -2,6 +2,7 @@ defmodule MusicLibraryWeb.WishlistLive.Index do use MusicLibraryWeb, :live_view import MusicLibraryWeb.Components.Pagination + import MusicLibraryWeb.LiveHelpers.Params import MusicLibraryWeb.RecordComponents alias MusicLibrary.Records @@ -170,30 +171,6 @@ defmodule MusicLibraryWeb.WishlistLive.Index do |> 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, order) - end - - defp merge_pagination(record_list_params, params, total_records) do - record_list_params - |> Map.put(:page, parse_int_or_default(params["page"], record_list_params.page)) - |> Map.put( - :page_size, - parse_int_or_default(params["page_size"], record_list_params.page_size) - ) - |> Map.put(:total_entries, total_records) - end - - defp parse_int_or_default(nil, default), do: default - - defp parse_int_or_default(value, _default) when is_binary(value) do - String.to_integer(value) - end - defp parse_order("alphabetical"), do: :alphabetical defp parse_order("insertion"), do: :insertion defp parse_order("release"), do: :release diff --git a/lib/music_library_web/live_helpers/params.ex b/lib/music_library_web/live_helpers/params.ex new file mode 100644 index 00000000..3b2098c5 --- /dev/null +++ b/lib/music_library_web/live_helpers/params.ex @@ -0,0 +1,47 @@ +defmodule MusicLibraryWeb.LiveHelpers.Params do + def parse_page(nil), do: 1 + + def parse_page(page) when is_binary(page) do + case Integer.parse(page) do + {num, ""} when num > 0 -> num + _ -> 1 + end + end + + def parse_page(_), do: 1 + + def parse_page_size(nil, _allowed, default), do: default + + def parse_page_size(page_size, allowed, default) when is_binary(page_size) do + case Integer.parse(page_size) do + {num, ""} when num > 0 -> + if allowed == nil or num in allowed, do: num, else: default + + _ -> + default + end + end + + def parse_page_size(_, _allowed, default), do: default + + def merge_pagination(params, url_params, total_entries, opts \\ []) do + allowed = Keyword.get(opts, :allowed_page_sizes) + default_page_size = params[:page_size] || 20 + + page = parse_page(url_params["page"]) + page_size = parse_page_size(url_params["page_size"], allowed, default_page_size) + + params + |> Map.put(:page, page) + |> Map.put(:page_size, page_size) + |> Map.put(:total_entries, total_entries) + end + + def merge_query(params, query) do + Map.put(params, :query, query) + end + + def merge_order(params, order) do + Map.put(params, :order, order) + end +end