Commit 5dd01de2f15a49954db83e0bf9248f8d1d2ac4d6

Thomas de Grivel 2022-01-28T17:09:20

[admin] organisation index pagination

diff --git a/lib/kmxgit/organisation_manager.ex b/lib/kmxgit/organisation_manager.ex
index dde5de1..e3f5274 100644
--- a/lib/kmxgit/organisation_manager.ex
+++ b/lib/kmxgit/organisation_manager.ex
@@ -4,6 +4,7 @@ defmodule Kmxgit.OrganisationManager do
 
   alias Kmxgit.IndexParams
   alias Kmxgit.OrganisationManager.Organisation
+  alias Kmxgit.Pagination
   alias Kmxgit.Repo
   alias Kmxgit.SlugManager.Slug
   alias Kmxgit.UserManager
@@ -12,10 +13,9 @@ defmodule Kmxgit.OrganisationManager do
     update_disk_usage()
     from(org in Organisation)
     |> join(:inner, [org], s in Slug, on: s.organisation_id == org.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.ex b/lib/kmxgit_web.ex
index 525dabb..5c56545 100644
--- a/lib/kmxgit_web.ex
+++ b/lib/kmxgit_web.ex
@@ -63,10 +63,6 @@ defmodule KmxgitWeb do
         |> FileSize.to_string()
       end
 
-      def page_link(conn, title \\ nil, page) do
-        link title || page, to: Routes.admin_repository_path(conn, :index, page: page, per: conn.assigns.pagination.per, search: conn.assigns.search, sort: conn.assigns.sort), class: "btn btn-primary"
-      end
-
       def recaptcha_site_key do
         Application.get_env :kmxgit, :recaptcha_site_key
       end
diff --git a/lib/kmxgit_web/controllers/admin/organisation_controller.ex b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
index 04124ff..14eda1e 100644
--- a/lib/kmxgit_web/controllers/admin/organisation_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
@@ -9,14 +9,16 @@ defmodule KmxgitWeb.Admin.OrganisationController 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"])
-    orgs = OrganisationManager.list_organisations(index_params)
+    |> KmxgitWeb.Admin.sort_param(params["sort"])
+    pagination = OrganisationManager.list_organisations(index_params)
     conn
     |> assign(:index, index_params)
-    |> assign(:orgs, orgs)
+    |> assign(:pagination, pagination)
     |> assign(:search, params["search"])
     |> assign(:search_action, Routes.admin_organisation_path(conn, :index, sort: params["sort"], search: params["search"]))
+    |> assign(:sort, params["sort"])
     |> render("index.html")
   end
 
diff --git a/lib/kmxgit_web/templates/admin/organisation/index.html.heex b/lib/kmxgit_web/templates/admin/organisation/index.html.heex
index 4368936..7ff9178 100644
--- a/lib/kmxgit_web/templates/admin/organisation/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/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>
@@ -19,7 +20,7 @@
       </tr>
     </thead>
     <tbody>
-      <%= for org <- @orgs do %>
+      <%= for org <- @pagination.result do %>
         <tr>
           <td><%= link org.id, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
           <td><%= link org.name, to: Routes.admin_organisation_path(@conn, :show, org) %></td>
@@ -32,4 +33,5 @@
       <% end %>
     </tbody>
   </table>
+  <%= render("pagination.html", assigns) %>
 </div>
diff --git a/lib/kmxgit_web/templates/admin/organisation/pagination.html.heex b/lib/kmxgit_web/templates/admin/organisation/pagination.html.heex
new file mode 100644
index 0000000..4e16e8c
--- /dev/null
+++ b/lib/kmxgit_web/templates/admin/organisation/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/templates/admin/repository/index.html.heex b/lib/kmxgit_web/templates/admin/repository/index.html.heex
index e2f6950..c962b7e 100644
--- a/lib/kmxgit_web/templates/admin/repository/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/repository/index.html.heex
@@ -1,7 +1,7 @@
 <div class="container-fluid">
   <h1>Repositories</h1>
   <%= render(KmxgitWeb.LayoutView, "search.html", assigns) %>
-  <%= render(KmxgitWeb.LayoutView, "pagination.html", assigns) %>
+  <%= render("pagination.html", assigns) %>
   <table class="table admin-index">
     <thead>
       <tr>
@@ -33,5 +33,5 @@
       <% end %>
     </tbody>
   </table>
-  <%= render(KmxgitWeb.LayoutView, "pagination.html", assigns) %>
+  <%= render("pagination.html", assigns) %>
 </div>
diff --git a/lib/kmxgit_web/templates/admin/repository/pagination.html.heex b/lib/kmxgit_web/templates/admin/repository/pagination.html.heex
new file mode 100644
index 0000000..4e16e8c
--- /dev/null
+++ b/lib/kmxgit_web/templates/admin/repository/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/templates/layout/pagination.html.heex b/lib/kmxgit_web/templates/layout/pagination.html.heex
deleted file mode 100644
index 4e16e8c..0000000
--- a/lib/kmxgit_web/templates/layout/pagination.html.heex
+++ /dev/null
@@ -1,21 +0,0 @@
-<%= 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/organisation_view.ex b/lib/kmxgit_web/views/admin/organisation_view.ex
index acd51c5..99140eb 100644
--- a/lib/kmxgit_web/views/admin/organisation_view.ex
+++ b/lib/kmxgit_web/views/admin/organisation_view.ex
@@ -2,4 +2,8 @@ defmodule KmxgitWeb.Admin.OrganisationView do
   use KmxgitWeb, :view
 
   alias Kmxgit.RepositoryManager.Repository
+
+  def page_link(conn, title \\ nil, page) do
+    link title || page, to: Routes.admin_organisation_path(conn, :index, page: page, per: conn.assigns.pagination.per, search: conn.assigns.search, sort: conn.assigns.sort), class: "btn btn-primary"
+  end
 end
diff --git a/lib/kmxgit_web/views/admin/repository_view.ex b/lib/kmxgit_web/views/admin/repository_view.ex
index 280cdc0..3208bbf 100644
--- a/lib/kmxgit_web/views/admin/repository_view.ex
+++ b/lib/kmxgit_web/views/admin/repository_view.ex
@@ -4,4 +4,8 @@ defmodule KmxgitWeb.Admin.RepositoryView do
   alias Kmxgit.OrganisationManager.Organisation
   alias Kmxgit.RepositoryManager.Repository
   alias Kmxgit.UserManager.User
+
+  def page_link(conn, title \\ nil, page) do
+    link title || page, to: Routes.admin_repository_path(conn, :index, page: page, per: conn.assigns.pagination.per, search: conn.assigns.search, sort: conn.assigns.sort), class: "btn btn-primary"
+  end
 end