From e78ec9fee45801d73b6669e51c115782294136c2 Mon Sep 17 00:00:00 2001 From: Gavin McDonald Date: Tue, 16 Dec 2025 18:27:39 -0500 Subject: [PATCH] now with pagination --- lib/labelmaker_web/constants.ex | 3 + .../controllers/labels_controller.ex | 37 ++++++++++- lib/labelmaker_web/controllers/labels_html.ex | 20 ++++++ .../controllers/labels_html/index.html.heex | 66 ++++++++++++++++++- .../controllers/labels_controller_test.exs | 4 +- 5 files changed, 122 insertions(+), 8 deletions(-) diff --git a/lib/labelmaker_web/constants.ex b/lib/labelmaker_web/constants.ex index c558717..8926cf6 100644 --- a/lib/labelmaker_web/constants.ex +++ b/lib/labelmaker_web/constants.ex @@ -71,6 +71,8 @@ defmodule LabelmakerWeb.Constants do "verdana" => "Verdana" } + @labels_per_page 4 + @max_height 1024 @max_width 1024 @@ -99,6 +101,7 @@ defmodule LabelmakerWeb.Constants do |> Enum.map(fn color -> color |> String.replace("-MS", "") |> String.replace("-", " ") end) def font_map, do: @font_map + def labels_per_page, do: @labels_per_page def max_height, do: @max_height def max_width, do: @max_width def max_label_length, do: @max_label_length diff --git a/lib/labelmaker_web/controllers/labels_controller.ex b/lib/labelmaker_web/controllers/labels_controller.ex index ceba131..38fe6b9 100644 --- a/lib/labelmaker_web/controllers/labels_controller.ex +++ b/lib/labelmaker_web/controllers/labels_controller.ex @@ -1,11 +1,42 @@ defmodule LabelmakerWeb.LabelsController do use LabelmakerWeb, :controller + alias LabelmakerWeb.Constants @label_dir Path.join(:code.priv_dir(:labelmaker), "static/labels") - def index(conn, _params) do - labels = list_labels() - render(conn, :index, labels: labels) + def index(conn, params) do + page = parse_page(params["page"]) + all_labels = list_labels() + total_count = length(all_labels) + total_pages = ceil(total_count / Constants.labels_per_page()) + + # Ensure page is within valid range + page = max(1, min(page, max(total_pages, 1))) + + labels = paginate(all_labels, page) + + render(conn, :index, + labels: labels, + page: page, + total_pages: total_pages, + total_count: total_count, + per_page: Constants.labels_per_page() + ) + end + + defp parse_page(nil), do: 1 + + defp parse_page(page_str) do + case Integer.parse(page_str) do + {page, _} when page > 0 -> page + _ -> 1 + end + end + + defp paginate(labels, page) do + labels + |> Enum.drop((page - 1) * Constants.labels_per_page()) + |> Enum.take(Constants.labels_per_page()) end defp list_labels do diff --git a/lib/labelmaker_web/controllers/labels_html.ex b/lib/labelmaker_web/controllers/labels_html.ex index edd4d20..b8aab58 100644 --- a/lib/labelmaker_web/controllers/labels_html.ex +++ b/lib/labelmaker_web/controllers/labels_html.ex @@ -13,4 +13,24 @@ defmodule LabelmakerWeb.LabelsHTML do defp pad(num) when num < 10, do: "0#{num}" defp pad(num), do: "#{num}" + + def pagination_range(current_page, total_pages) do + cond do + total_pages <= 7 -> + # Show all pages if there are 7 or fewer + 1..total_pages + + current_page <= 4 -> + # Near the beginning: show first 5, then last + [1, 2, 3, 4, 5, :ellipsis, total_pages] + + current_page >= total_pages - 3 -> + # Near the end: show first, then last 5 + [1, :ellipsis, total_pages - 4, total_pages - 3, total_pages - 2, total_pages - 1, total_pages] + + true -> + # In the middle: show first, current +/- 1, and last + [1, :ellipsis, current_page - 1, current_page, current_page + 1, :ellipsis, total_pages] + end + end end diff --git a/lib/labelmaker_web/controllers/labels_html/index.html.heex b/lib/labelmaker_web/controllers/labels_html/index.html.heex index 0769aa1..64b1df9 100644 --- a/lib/labelmaker_web/controllers/labels_html/index.html.heex +++ b/lib/labelmaker_web/controllers/labels_html/index.html.heex @@ -4,7 +4,7 @@ ← Back to Home - <%= if Enum.empty?(@labels) do %> + <%= if @total_count == 0 do %>

No labels generated yet. @@ -17,8 +17,15 @@

<% else %> -
- Total labels: <%= length(@labels) %> +
+
+ Showing <%= (@page - 1) * @per_page + 1 %>-<%= min(@page * @per_page, @total_count) %> of <%= @total_count %> labels +
+ <%= if @total_pages > 1 do %> +
+ Page <%= @page %> of <%= @total_pages %> +
+ <% end %>
@@ -70,5 +77,58 @@
<% end %>
+ + <%= if @total_pages > 1 do %> +
+ <%= if @page > 1 do %> + + ← Previous + + <% else %> + + ← Previous + + <% end %> + +
+ <%= for page_num <- pagination_range(@page, @total_pages) do %> + <%= if page_num == :ellipsis do %> + + ... + + <% else %> + <%= if page_num == @page do %> + + <%= page_num %> + + <% else %> + + <%= page_num %> + + <% end %> + <% end %> + <% end %> +
+ + <%= if @page < @total_pages do %> + + Next → + + <% else %> + + Next → + + <% end %> +
+ <% end %> <% end %> diff --git a/test/labelmaker_web/controllers/labels_controller_test.exs b/test/labelmaker_web/controllers/labels_controller_test.exs index fed2fb5..ed3a0b7 100644 --- a/test/labelmaker_web/controllers/labels_controller_test.exs +++ b/test/labelmaker_web/controllers/labels_controller_test.exs @@ -29,7 +29,7 @@ defmodule LabelmakerWeb.LabelsControllerTest do html = html_response(conn, 200) assert html =~ "Generated Labels" - assert html =~ "Total labels: 3" + assert html =~ "Showing 1-3 of 3 labels" end test "displays label information correctly", %{conn: conn} do @@ -59,7 +59,7 @@ defmodule LabelmakerWeb.LabelsControllerTest do html = html_response(conn, 200) # Should show all labels - assert html =~ "Total labels: 3" + assert html =~ "Showing 1-3 of 3 labels" end test "handles missing labels directory gracefully", %{conn: conn} do