Skip to content

Commit

Permalink
order by pinned devices when showing devices list
Browse files Browse the repository at this point in the history
  • Loading branch information
elinol committed Feb 12, 2025
1 parent 9f2e6ed commit 2f66a30
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
14 changes: 12 additions & 2 deletions lib/nerves_hub/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ defmodule NervesHub.Devices do
|> Repo.one!()
end

@spec filter(Product.t(), map()) :: %{
@spec filter(Product.t(), User.t(), map()) :: %{
entries: list(Device.t()),
current_page: non_neg_integer(),
page_size: non_neg_integer(),
total_pages: non_neg_integer(),
total_count: non_neg_integer()
}
def filter(product, opts) do
def filter(product, user, opts) do
pagination = Map.get(opts, :pagination, %{})
sorting = Map.get(opts, :sort, {:asc, :identifier})
filters = Map.get(opts, :filters, %{})
Expand All @@ -125,12 +125,22 @@ defmodule NervesHub.Devices do
|> Repo.exclude_deleted()
|> join(:left, [d], dc in assoc(d, :latest_connection), as: :latest_connection)
|> join(:left, [d, dc], dh in assoc(d, :latest_health), as: :latest_health)
|> join(:left, [d, dc, dh], pd in PinnedDevice,
on: pd.device_id == d.id and pd.user_id == ^user.id
)
|> preload([latest_connection: lc], latest_connection: lc)
|> preload([latest_health: lh], latest_health: lh)
|> select([d, dc, dh, pd], device: d, pinned: not is_nil(pd.device_id))
|> order_by([d, dc, dh, pd], desc_nulls_last: pd.device_id)
|> Filtering.build_filters(filters)
|> sort_devices(sorting)
|> Flop.run(flop)
|> then(fn {entries, meta} ->
entries =
Enum.map(entries, fn [device: device, pinned: pinned] ->
Map.put(device, :pinned, pinned)
end)

meta
|> Map.take([
:current_page,
Expand Down
1 change: 1 addition & 0 deletions lib/nerves_hub_web/live/devices/index-new.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
<.link navigate={~p"/org/#{@org.name}/#{@product.name}/devices/#{device.identifier}"} class={"ff-m #{firmware_update_status(device)}"} title={firmware_update_title(device)}>
{device.identifier}
</.link>
<.icon :if={device.pinned} name="pinned" class="size-1 stroke-zinc-500" />
<span class={["flex items-center gap-1 ml-2 pl-2.5 pr-2.5 py-0.5 border border-zinc-700 rounded-full bg-zinc-800", !@progress[device.id] && "invisible"]}>
<span class="text-xs text-zinc-300 tracking-tight">updating</span>
</span>
Expand Down
4 changes: 2 additions & 2 deletions lib/nerves_hub_web/live/devices/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
end

defp assign_display_devices(
%{assigns: %{product: product, paginate_opts: paginate_opts}} = socket
%{assigns: %{product: product, user: user, paginate_opts: paginate_opts}} = socket
) do
opts = %{
pagination: %{page: paginate_opts.page_number, page_size: paginate_opts.page_size},
Expand All @@ -456,7 +456,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
filters: socket.assigns.current_filters
}

page = Devices.filter(product, opts)
page = Devices.filter(product, user, opts)

statuses =
Enum.into(page.entries, %{}, fn device ->
Expand Down

0 comments on commit 2f66a30

Please sign in to comment.