Skip to content

Commit

Permalink
Rename last activity to last updated at and fixing sorting of null va…
Browse files Browse the repository at this point in the history
…lues (#2653)
  • Loading branch information
elias-ba authored Nov 11, 2024
1 parent fdcf3d8 commit 8ca0d72
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
8 changes: 4 additions & 4 deletions lib/lightning/projects.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ defmodule Lightning.Projects do
:role,
:workflows_count,
:collaborators_count,
:last_activity
:last_updated_at
]
end

def get_projects_overview(%User{id: user_id}, opts \\ []) do
order_by = Keyword.get(opts, :order_by, {:asc, :name})
order_by = Keyword.get(opts, :order_by, {:asc_nulls_last, :name})

from(p in Project,
join: pu in assoc(p, :project_users),
Expand All @@ -64,7 +64,7 @@ defmodule Lightning.Projects do
role: pu.role,
workflows_count: count(w.id, :distinct),
collaborators_count: count(pu_all.user_id, :distinct),
last_activity: max(w.updated_at)
last_updated_at: max(w.updated_at)
},
order_by: ^dynamic_order_by(order_by)
)
Expand All @@ -75,7 +75,7 @@ defmodule Lightning.Projects do
{direction, dynamic([p, _pu, _w, _pu_all], field(p, :name))}
end

defp dynamic_order_by({direction, :last_activity}) do
defp dynamic_order_by({direction, :last_updated_at}) do
{direction, dynamic([_p, _pu, w, _pu_all], max(w.updated_at))}
end

Expand Down
20 changes: 12 additions & 8 deletions lib/lightning_web/live/dashboard_live/components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,18 @@ defmodule LightningWeb.DashboardLive.Components do
end

def user_projects_table(assigns) do
next_sort_icon = %{asc: "hero-chevron-up", desc: "hero-chevron-down"}
next_sort_icon = %{
asc_nulls_last: "hero-chevron-up",
desc_nulls_last: "hero-chevron-down"
}

assigns =
assign(assigns,
projects_count: assigns.projects |> Enum.count(),
empty?: assigns.projects |> Enum.empty?(),
name_sort_icon: next_sort_icon[assigns.name_direction],
last_activity_sort_icon: next_sort_icon[assigns.last_activity_direction]
last_updated_at_sort_icon:
next_sort_icon[assigns.last_updated_at_direction]
)

~H"""
Expand Down Expand Up @@ -163,14 +167,14 @@ defmodule LightningWeb.DashboardLive.Components do
<.th>Collaborators</.th>
<.th>
<div class="group inline-flex items-center">
Last Activity
Last Updated
<span
phx-click="sort"
phx-value-by="last_activity"
phx-value-by="last_updated_at"
phx-target={@target}
class="cursor-pointer align-middle ml-2 flex-none rounded text-gray-400 group-hover:visible group-focus:visible"
>
<.icon name={@last_activity_sort_icon} />
<.icon name={@last_updated_at_sort_icon} />
</span>
</div>
</.th>
Expand Down Expand Up @@ -207,13 +211,13 @@ defmodule LightningWeb.DashboardLive.Components do
</.link>
</.td>
<.td>
<%= if project.last_activity do %>
<%= if project.last_updated_at do %>
<%= Lightning.Helpers.format_date(
project.last_activity,
project.last_updated_at,
"%d/%m/%Y %H:%M:%S"
) %>
<% else %>
No activity
N/A
<% end %>
</.td>
<.td class="text-right">
Expand Down
14 changes: 7 additions & 7 deletions lib/lightning_web/live/dashboard_live/user_projects_section.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ defmodule LightningWeb.DashboardLive.UserProjectsSection do
socket
|> assign(assigns)
|> assign(:projects, projects)
|> assign(:name_sort_direction, :asc)
|> assign(:last_activity_sort_direction, :asc)}
|> assign(:name_sort_direction, :asc_nulls_last)
|> assign(:last_updated_at_sort_direction, :asc_nulls_last)}
end

@impl true
Expand All @@ -27,7 +27,7 @@ defmodule LightningWeb.DashboardLive.UserProjectsSection do

sort_direction =
socket.assigns
|> Map.get(sort_key, :asc)
|> Map.get(sort_key, :asc_nulls_last)
|> switch_sort_direction()

projects =
Expand All @@ -41,11 +41,11 @@ defmodule LightningWeb.DashboardLive.UserProjectsSection do
|> assign(sort_key, sort_direction)}
end

defp switch_sort_direction(:asc), do: :desc
defp switch_sort_direction(:desc), do: :asc
defp switch_sort_direction(:asc_nulls_last), do: :desc_nulls_last
defp switch_sort_direction(:desc_nulls_last), do: :asc_nulls_last

defp projects_for_user(%User{} = user, opts \\ []) do
order_by = Keyword.get(opts, :order_by, {:asc, :name})
order_by = Keyword.get(opts, :order_by, {:asc_nulls_last, :name})

Projects.get_projects_overview(user, order_by: order_by)
end
Expand All @@ -59,7 +59,7 @@ defmodule LightningWeb.DashboardLive.UserProjectsSection do
user={@current_user}
target={@myself}
name_direction={@name_sort_direction}
last_activity_direction={@last_activity_sort_direction}
last_updated_at_direction={@last_updated_at_sort_direction}
>
<:empty_state>
<button
Expand Down
14 changes: 9 additions & 5 deletions test/lightning/projects_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1618,7 +1618,7 @@ defmodule Lightning.ProjectsTest do
] = result
end

test "orders projects by last_activity (updated_at of workflows) descending when specified" do
test "orders projects by last_updated_at (updated_at of workflows) descending when specified" do
user = insert(:user)

project_a =
Expand All @@ -1640,15 +1640,17 @@ defmodule Lightning.ProjectsTest do
)

result =
Projects.get_projects_overview(user, order_by: {:desc, :last_activity})
Projects.get_projects_overview(user,
order_by: {:desc_nulls_last, :last_updated_at}
)

assert [
%ProjectOverviewRow{id: ^project_b_id, name: "Project B"},
%ProjectOverviewRow{id: ^project_a_id, name: "Project A"}
] = result
end

test "orders projects by last_activity ascending when specified" do
test "orders projects by last_updated_at ascending when specified" do
user = insert(:user)

project_a =
Expand All @@ -1670,7 +1672,9 @@ defmodule Lightning.ProjectsTest do
)

result =
Projects.get_projects_overview(user, order_by: {:asc, :last_activity})
Projects.get_projects_overview(user,
order_by: {:asc_nulls_last, :last_updated_at}
)

assert [
%ProjectOverviewRow{id: ^project_a_id, name: "Project A"},
Expand All @@ -1694,7 +1698,7 @@ defmodule Lightning.ProjectsTest do
id: ^project_id,
name: "Project No Workflow",
workflows_count: 0,
last_activity: nil
last_updated_at: nil
}
] = result
end
Expand Down
24 changes: 12 additions & 12 deletions test/lightning_web/live/dashboard_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -218,31 +218,31 @@ defmodule LightningWeb.DashboardLiveTest do

{:ok, view, _html} = live(conn, ~p"/projects")

projects_sorted_by_last_activity =
get_sorted_projects_by_last_activity(projects)
projects_sorted_by_last_updated_at =
get_sorted_projects_by_last_updated_at(projects)

html = render(view)

project_last_activities_from_html =
extract_project_last_activities_from_html(html)

assert project_last_activities_from_html ==
projects_sorted_by_last_activity
projects_sorted_by_last_updated_at

view
|> element("span[phx-click='sort'][phx-value-by='last_activity']")
|> element("span[phx-click='sort'][phx-value-by='last_updated_at']")
|> render_click()

projects_sorted_by_last_activity_desc =
get_sorted_projects_by_last_activity(projects, :desc)
projects_sorted_by_last_updated_at_desc =
get_sorted_projects_by_last_updated_at(projects, :desc)

html = render(view)

project_last_activities_from_html =
extract_project_last_activities_from_html(html)

assert project_last_activities_from_html ==
projects_sorted_by_last_activity_desc
projects_sorted_by_last_updated_at_desc
end

test "Toggles the welcome banner", %{conn: conn, user: user} do
Expand Down Expand Up @@ -353,7 +353,7 @@ defmodule LightningWeb.DashboardLiveTest do
)
end

defp get_sorted_projects_by_last_activity(projects, order \\ :asc) do
defp get_sorted_projects_by_last_updated_at(projects, order \\ :asc) do
projects_with_workflows = Repo.preload(projects, :workflows)

projects_with_workflows
Expand All @@ -367,16 +367,16 @@ defmodule LightningWeb.DashboardLiveTest do
order
)
|> Enum.map(fn project ->
last_activity =
last_updated_at =
project
|> Map.get(:workflows)
|> Enum.map(& &1.updated_at)
|> Enum.max(fn -> nil end)

if last_activity do
Lightning.Helpers.format_date(last_activity, "%d/%m/%Y %H:%M:%S")
if last_updated_at do
Lightning.Helpers.format_date(last_updated_at, "%d/%m/%Y %H:%M:%S")
else
"No activity"
"N/A"
end
end)
end
Expand Down

0 comments on commit 8ca0d72

Please sign in to comment.