Commit eb410d96939b918445de053d2aece87203478d13

Thomas de Grivel 2022-01-27T10:00:12

[admin] sort organisation index by columns

diff --git a/lib/kmxgit_web/controllers/admin/organisation_controller.ex b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
index 15745e4..eeb82d7 100644
--- a/lib/kmxgit_web/controllers/admin/organisation_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/organisation_controller.ex
@@ -6,14 +6,43 @@ defmodule KmxgitWeb.Admin.OrganisationController do
   alias Kmxgit.Repo
   alias KmxgitWeb.ErrorView
 
-  def index(conn, _params) do
-    orgs = OrganisationManager.list_organisations
+  def index(conn, params) do
+    {sort, rev} = case String.split(params["sort"], "-") do
+                    [sort, _] -> {sort, true}
+                    [sort] -> {sort, false}
+                    _ -> {nil, false}
+                  end
+    orgs = OrganisationManager.list_organisations()
     |> OrganisationManager.put_disk_usage()
+    |> sort_organisations(sort)
+    orgs = if rev, do: Enum.reverse(orgs), else: orgs
     conn
     |> assign(:orgs, orgs)
+    |> assign(:rev, rev)
+    |> assign(:sort, sort)
     |> render("index.html")
   end
 
+  defp sort_organisations(orgs, "id") do
+    orgs
+    |> Enum.sort(fn a, b -> a.id < b.id end)
+  end
+  defp sort_organisations(orgs, "name") do
+    orgs
+    |> Enum.sort_by(fn org ->
+      String.downcase(org.name || "")
+    end)
+  end
+  defp sort_organisations(orgs, "slug") do
+    orgs
+    |> Enum.sort(fn a, b -> String.downcase(a.slug.slug) < String.downcase(b.slug.slug) end)
+  end
+  defp sort_organisations(orgs, "du") do
+    orgs
+    |> Enum.sort(fn a, b -> a.disk_usage < b.disk_usage end)
+  end
+  defp sort_organisations(orgs, _), do: orgs
+
   def new(conn, _params) do
     changeset = OrganisationManager.change_organisation
     conn
diff --git a/lib/kmxgit_web/templates/admin/organisation/index.html.heex b/lib/kmxgit_web/templates/admin/organisation/index.html.heex
index ce77433..7379ef3 100644
--- a/lib/kmxgit_web/templates/admin/organisation/index.html.heex
+++ b/lib/kmxgit_web/templates/admin/organisation/index.html.heex
@@ -10,11 +10,11 @@
   <table class="table admin-index">
     <thead>
       <tr>
-        <th><%= gettext "Id" %></th>
-        <th><%= gettext "Name" %></th>
-        <th><%= gettext "Slug" %></th>
-        <th><%= gettext "Disk usage" %></th>
-        <th><%= gettext "Actions" %></th>
+        <th><%= link gettext("Id"), to: Routes.admin_organisation_path(@conn, :index, sort: "id#{if @sort == "id" && !@rev, do: "-"}") %><%= if @sort == "id" do %><%= if @rev do %> <i class="fa fa-angle-down"></i><% else %> <i class="fa fa-angle-up"></i><% end %><% end %></th>
+        <th><%= link gettext("Name"), to: Routes.admin_organisation_path(@conn, :index, sort: "name#{if @sort == "name" && !@rev, do: "-"}") %><%= if @sort == "name" do %><%= if @rev do %> <i class="fa fa-angle-down"></i><% else %> <i class="fa fa-angle-up"></i><% end %><% end %></th>
+        <th><%= link gettext("Slug"), to: Routes.admin_organisation_path(@conn, :index, sort: "slug#{if @sort == "slug" && !@rev, do: "-"}") %><%= if @sort == "slug" do %><%= if @rev do %> <i class="fa fa-angle-down"></i><% else %> <i class="fa fa-angle-up"></i><% end %><% end %></th>
+        <th><%= link gettext("Disk usage"), to: Routes.admin_organisation_path(@conn, :index, sort: "du#{if @sort != "du" || (@sort == "du" && !@rev), do: "-"}") %><%= if @sort == "du" do %><%= if @rev do %> <i class="fa fa-angle-down"></i><% else %> <i class="fa fa-angle-up"></i><% end %><% end %></th>
+        <th><%= gettext("Actions") %></th>
       </tr>
     </thead>
     <tbody>