From 6d5b29a230008c5cf1ed243143679de0489086be Mon Sep 17 00:00:00 2001 From: KKlochko Date: Tue, 25 Mar 2025 22:04:03 +0200 Subject: [PATCH] Add the Pagination component. --- .../components/my_components.ex | 2 + .../components/my_components/pagination.ex | 125 ++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 lib/decentralised_book_index_web/components/my_components/pagination.ex diff --git a/lib/decentralised_book_index_web/components/my_components.ex b/lib/decentralised_book_index_web/components/my_components.ex index d763714..b1cb0ae 100644 --- a/lib/decentralised_book_index_web/components/my_components.ex +++ b/lib/decentralised_book_index_web/components/my_components.ex @@ -5,6 +5,8 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents do quote do import MyComponents.AuthorCard, only: [author_card: 1] import MyComponents.AuthorSearch, only: [author_search: 1] + + import MyComponents.Pagination, only: [pagination: 1] end end end diff --git a/lib/decentralised_book_index_web/components/my_components/pagination.ex b/lib/decentralised_book_index_web/components/my_components/pagination.ex new file mode 100644 index 0000000..8753e0f --- /dev/null +++ b/lib/decentralised_book_index_web/components/my_components/pagination.ex @@ -0,0 +1,125 @@ +defmodule DecentralisedBookIndexWeb.Components.MyComponents.Pagination do + use Phoenix.Component + use DecentralisedBookIndexWeb, :verified_routes + + attr :endpoint, :string, required: true + attr :page, :map, required: true + attr :page_params, :list, required: true + attr :params, :list, required: true + + def pagination(assigns) do + assigns = assign(assigns, :current_page_number, get_current_page(assigns.page)) + + ~H""" + + """ + end + + attr :kind, :string, + values: ~w(base primary error), + default: "base" + + attr :inverse, :boolean, default: false + attr :class, :string, default: "" + attr :rest, :global, include: ~w(navigate disabled patch) + + slot :inner_block + + defp button_link(assigns) do + ~H""" + <.link + class={[ + @rest[:disabled] && "opacity-60 grayscale pointer-events-none", + @class + ]} + {@rest} + > + {render_slot(@inner_block)} + + """ + end + + defp get_page_numbers(pages) do + %{count: count, limit: limit} = pages + + max_number = + count/limit + |> :math.ceil() + |> Kernel.trunc() + + 1..max_number + end + + defp get_current_page(pages) do + %{offset: offset, limit: limit} = pages + + Kernel.trunc(offset/limit) +1 + end + + def page_params_which(page, params, which) do + page_params = + case AshPhoenix.LiveView.page_link_params(page, which) do + :invalid -> [] + list -> list + end + + page_params + |> Enum.into(params, fn {k, v} -> {Atom.to_string(k), v} end) + |> to_uri_params() + end + + def page_params_number(page_params, params, number) do + limit = Keyword.get(page_params, :limit) + offset = limit*(number-1) + + params + |> Map.put("limit", limit) + |> Map.put("offset", offset) + |> to_uri_params() + end + + defp to_uri_params(params) do + URI.encode_query(params) + end +end