Commit 66f83fb956494edf10feaefb5b59844143d3bc42

Thomas de Grivel 2022-01-28T17:15:40

[admin] user index pagination

diff --git a/lib/kmxgit/user_manager.ex b/lib/kmxgit/user_manager.ex
index 99d0005..0eea9ff 100644
--- a/lib/kmxgit/user_manager.ex
+++ b/lib/kmxgit/user_manager.ex
@@ -6,6 +6,7 @@ defmodule Kmxgit.UserManager do
   import Ecto.Query, warn: false
 
   alias Kmxgit.IndexParams
+  alias Kmxgit.Pagination
   alias Kmxgit.Repo
   alias Kmxgit.SlugManager.Slug
   alias Kmxgit.UserManager.{Avatar, User, UserToken, UserNotifier}
@@ -14,10 +15,9 @@ defmodule Kmxgit.UserManager do
     update_disk_usage()
     from(u in User)
     |> join(:inner, [u], s in Slug, on: s.user_id == u.id)
-    |> preload([:owned_repositories, :slug])
     |> search(params)
     |> index_order_by(params)
-    |> Repo.all()
+    |> Pagination.page(params, preload: [:owned_repositories, :slug])
   end
 
   def search(query, %IndexParams{search: search}) do
diff --git a/lib/kmxgit_web/controllers/admin/user_controller.ex b/lib/kmxgit_web/controllers/admin/user_controller.ex
index 0670186..4116d11 100644
--- a/lib/kmxgit_web/controllers/admin/user_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/user_controller.ex
@@ -10,15 +10,17 @@ defmodule KmxgitWeb.Admin.UserController do
 
   def index(conn, params) do
     index_params = %IndexParams{}
-    |> KmxgitWeb.Admin.sort_param(params["sort"])
+    |> KmxgitWeb.Admin.page_params(params["page"], params["per"])
     |> KmxgitWeb.Admin.search_param(params["search"])
-    users = UserManager.list_users(index_params)
+    |> KmxgitWeb.Admin.sort_param(params["sort"])
+    pagination = UserManager.list_users(index_params)
     conn
     |> assign(:index, index_params)
     |> assign(:page_title, gettext("Users"))
+    |> assign(:pagination, pagination)
     |> assign(:search, params["search"])
     |> assign(:search_action, Routes.admin_user_path(conn, :index, sort: params["sort"], search: params["search"]))
-    |> assign(:users, users)
+    |> assign(:sort, params["sort"])
     |> render("index.html")
   end
 
diff --git a/lib/kmxgit_web/templates/admin/user/index.html.heex b/lib/kmxgit_web/templates/admin/user/index.html.heex
index 86d8529..0737836 100644
--- a/lib/kmxgit_web/templates/admin/user/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/user/index.html.heex
@@ -8,6 +8,7 @@
     </div>
   </div>
   <%= render(KmxgitWeb.LayoutView, "search.html", assigns) %>
+  <%= render("pagination.html", assigns) %>
   <table class="table admin-index">
     <thead>
       <tr>
@@ -23,7 +24,7 @@
       </tr>
     </thead>
     <tbody>
-      <%= Enum.map @users, fn(user) -> %>
+      <%= for user <- @pagination.result do %>
         <tr>
           <td><%= link user.id, to: Routes.admin_user_path(@conn, :show, user) %></td>
           <td><%= link user.name, to: Routes.admin_user_path(@conn, :show, user) %></td>
@@ -38,4 +39,5 @@
       <% end %>
     </tbody>
   </table>
+  <%= render("pagination.html", assigns) %>
 </div>
diff --git a/lib/kmxgit_web/templates/admin/user/pagination.html.heex b/lib/kmxgit_web/templates/admin/user/pagination.html.heex
new file mode 100644
index 0000000..4e16e8c
--- /dev/null
+++ b/lib/kmxgit_web/templates/admin/user/pagination.html.heex
@@ -0,0 +1,21 @@
+<%= if @pagination.first_page do %>
+  <%= page_link @conn, raw("<i class=\"fa fa-angle-double-left\"></i>"), @pagination.first_page %>
+<% end %>
+<%= if @pagination.prev_page do %>
+  <%= page_link @conn, raw("<i class=\"fa fa-angle-left\"></i>"), @pagination.prev_page %>
+<% end %>
+<span class="btn btn-primary disabled">
+  <%= @pagination.page %>
+</span>
+<%= if @pagination.next_page do %>
+  <%= page_link @conn, raw("<i class=\"fa fa-angle-right\"></i>"), @pagination.next_page %>
+<% end %>
+<%= if @pagination.last_page do %>
+  <%= page_link @conn, raw("<i class=\"fa fa-angle-double-right\"></i>"), @pagination.last_page %>
+<% end %>
+&nbsp;
+<%= @pagination.first %>
+-
+<%= @pagination.last %>
+<%= gettext("out of") %>
+<%= @pagination.count %>
diff --git a/lib/kmxgit_web/views/admin/user_view.ex b/lib/kmxgit_web/views/admin/user_view.ex
index 3d175c7..6639850 100644
--- a/lib/kmxgit_web/views/admin/user_view.ex
+++ b/lib/kmxgit_web/views/admin/user_view.ex
@@ -3,4 +3,8 @@ defmodule KmxgitWeb.Admin.UserView do
 
   alias Kmxgit.RepositoryManager.Repository
   alias Kmxgit.UserManager.User
+
+  def page_link(conn, title \\ nil, page) do
+    link title || page, to: Routes.admin_user_path(conn, :index, page: page, per: conn.assigns.pagination.per, search: conn.assigns.search, sort: conn.assigns.sort), class: "btn btn-primary"
+  end
 end