Skip to content

Commit 87d9a9c

Browse files
authored
Standardize loading a device for the device show page (#1861)
Resolves an error raised by Sentry
1 parent 9f8903d commit 87d9a9c

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

lib/nerves_hub_web/channels/device_channel.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ defmodule NervesHubWeb.DeviceChannel do
170170
{:noreply, socket}
171171
end
172172

173+
@decorate with_span("Channels.DeviceChannel.handle_info:clear-deployment")
174+
def handle_info(
175+
%Broadcast{event: "devices/clear-deployment"},
176+
%{assigns: %{device: device}} = socket
177+
) do
178+
device = %{device | deployment_id: nil, deployment: nil}
179+
180+
{:noreply, update_device(socket, device)}
181+
end
182+
173183
# Update local state and tell the various servers of the new information
174184
@decorate with_span("Channels.DeviceChannel.handle_info:devices-updated")
175185
def handle_info(%Broadcast{event: "devices/updated"}, %{assigns: %{device: device}} = socket) do

lib/nerves_hub_web/live/devices/show.ex

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ defmodule NervesHubWeb.Live.Devices.Show do
3636
def mount(%{"device_identifier" => device_identifier}, _session, socket) do
3737
%{org: org, product: product} = socket.assigns
3838

39-
device = Devices.get_device_by_identifier!(org, device_identifier, :latest_connection)
39+
device = load_device(org, device_identifier)
4040

4141
if connected?(socket) do
4242
socket.endpoint.subscribe("device:#{device.identifier}:internal")
@@ -69,10 +69,9 @@ defmodule NervesHubWeb.Live.Devices.Show do
6969
end
7070

7171
def handle_info(:reload_device, socket) do
72-
device =
73-
socket.assigns.device
74-
|> Repo.reload()
75-
|> Repo.preload(:deployment)
72+
%{org: org, device: device} = socket.assigns
73+
74+
device = load_device(org, device.identifier)
7675

7776
socket
7877
|> assign(:device, device)
@@ -457,6 +456,10 @@ defmodule NervesHubWeb.Live.Devices.Show do
457456
|> noreply()
458457
end
459458

459+
defp load_device(org, identifier) do
460+
Devices.get_device_by_identifier!(org, identifier, :latest_connection)
461+
end
462+
460463
defp scripts_with_output(product) do
461464
product
462465
|> Scripts.all_by_product()

0 commit comments

Comments
 (0)