Show user on admin devices table (#598)

* Show user on admin devices table

* add link and test
This commit is contained in:
Po Chen
2022-05-18 13:04:33 +10:00
committed by GitHub
parent 4b978959cd
commit 4922ff143c
5 changed files with 31 additions and 4 deletions

View File

@@ -4,7 +4,8 @@
<%= render FzHttpWeb.SharedView, "flash.html", assigns %>
<div class="block">
<%= render FzHttpWeb.SharedView, "devices_table.html", devices: @devices, socket: @socket %>
<%= render FzHttpWeb.SharedView, "devices_table.html",
devices: @devices, show_user: true, socket: @socket %>
</div>
<p>

View File

@@ -3,13 +3,18 @@ defmodule FzHttpWeb.DeviceLive.Admin.Index do
Handles Device LiveViews.
"""
use FzHttpWeb, :live_view
alias FzHttp.Devices
alias FzHttp.{Devices, Repo}
@impl Phoenix.LiveView
def mount(_params, _session, socket) do
devices =
Devices.list_devices()
|> Repo.preload(:user)
|> Enum.sort_by(& &1.user_id)
{:ok,
socket
|> assign(:devices, Devices.list_devices())
|> assign(:devices, devices)
|> assign(:page_title, "All Devices")}
end

View File

@@ -45,7 +45,8 @@
<div class="block">
<%= if length(@devices) > 0 do %>
<%= render FzHttpWeb.SharedView, "devices_table.html", devices: @devices, socket: @socket %>
<%= render FzHttpWeb.SharedView, "devices_table.html",
devices: @devices, show_user: false, socket: @socket %>
<% else %>
No devices.
<% end %>

View File

@@ -2,6 +2,7 @@
<thead>
<tr>
<th>Name</th>
<%= if @show_user do %><th>User</th><% end %>
<th>WireGuard IP</th>
<th>Remote IP</th>
<th>Latest Handshake</th>
@@ -17,6 +18,11 @@
<td>
<%= live_patch(device.name, to: Routes.device_admin_show_path(@socket, :show, device)) %>
</td>
<%= if @show_user do %>
<td>
<%= link(device.user.email, to: Routes.user_show_path(@socket, :show, device.user)) %>
</td>
<% end %>
<td class="code">
<%= device.ipv4 %>
<br>

View File

@@ -14,6 +14,20 @@ defmodule FzHttpWeb.DeviceLive.Admin.IndexTest do
assert html =~ device.name
end
end
test "includes the user in the list", %{admin_conn: conn, devices: devices} do
path = Routes.device_admin_index_path(conn, :index)
{:ok, _view, html} = live(conn, path)
assert html =~ "User"
devices = FzHttp.Repo.preload(devices, :user)
for device <- devices do
assert html =~ device.user.email
assert html =~ ~s[href="/users/#{device.user.id}"]
end
end
end
describe "authenticated but user deleted" do