Skip to content

Commit edefdbc

Browse files
authored
Fix LiveView warning when uploading device certificates, rehydrate state properly (#2280)
`Phoenix.LiveView. consume_uploaded_entry/3` expects the function passed to return `{:ok, _return}` or `{:postpone, _return}`. We were returning a socket, causing a warning in the logs: ``` iex(21)> warning: consuming uploads requires a return signature matching: {:ok, value} | {:postpone, value} got: #Phoenix.LiveView.Socket<...> (phoenix_live_view 1.1.3) lib/phoenix_live_view/upload_channel.ex:33: Phoenix.LiveView.UploadChannel.consume/3 (elixir 1.18.3) lib/enum.ex:1714: Enum."-map/2-lists^map/1-1-"/2 (phoenix_live_view 1.1.3) lib/phoenix_live_view/upload.ex:264: Phoenix.LiveView.Upload.consume_uploaded_entry/3 (nerves_hub 2.0.0+afbd8e1f) lib/nerves_hub_web/components/device_page/settings_tab.ex:424: NervesHubWeb.Components.DevicePage.SettingsTab.handle_progress/3 (phoenix_live_view 1.1.3) lib/phoenix_live_view/channel.ex:181: anonymous fn/4 in Phoenix.LiveView.Channel.handle_info/2 (phoenix_live_view 1.1.3) lib/phoenix_live_view/channel.ex:1525: Phoenix.LiveView.Channel.write_socket/4 (phoenix_live_view 1.1.3) lib/phoenix_live_view/channel.ex:174: Phoenix.LiveView.Channel.handle_info/2 (stdlib 6.2.2) gen_server.erl:2345: :gen_server.try_handle_info/3 (stdlib 6.2.2) gen_server.erl:2433: :gen_server.handle_msg/6 (stdlib 6.2.2) proc_lib.erl:329: :proc_lib.init_p_do_apply/3 ``` I also fixed an issue when uploading a device certificate. We were previously using `update_in/2`, but on an unloaded Ecto association. This was copied over from the old UI, but this code now sits in a component, and we don't preload device certificates in the parent LiveView. Fixing this uncovered another bug if you deleted the device's last cert and then uploaded a new one. `Repo.preload/3` won't preload on a loaded empty association, so `force: true` is needed.
1 parent ef82a9a commit edefdbc

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

lib/nerves_hub_web.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ defmodule NervesHubWeb do
356356

357357
defoverridable tab_params: 3, cleanup: 0
358358

359+
def ok(socket), do: {:ok, socket}
360+
359361
def halt(socket), do: {:halt, socket}
360362

361363
def cont(socket), do: {:cont, socket}

lib/nerves_hub_web/components/device_page/settings_tab.ex

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule NervesHubWeb.Components.DevicePage.SettingsTab do
2222
end
2323

2424
def render(assigns) do
25-
device = Repo.preload(assigns.device, :device_certificates)
25+
device = Repo.preload(assigns.device, :device_certificates, force: true)
2626

2727
changeset = Ecto.Changeset.change(assigns.device)
2828

@@ -431,25 +431,30 @@ defmodule NervesHubWeb.Components.DevicePage.SettingsTab do
431431
defp import_cert(%{assigns: %{device: device}} = socket, path) do
432432
with {:ok, pem_or_der} <- File.read(path),
433433
{:ok, otp_cert} <- Certificate.from_pem_or_der(pem_or_der),
434-
{:ok, db_cert} <- Devices.create_device_certificate(device, otp_cert) do
435-
updated = update_in(device.device_certificates, &[db_cert | &1])
434+
{:ok, _db_cert} <- Devices.create_device_certificate(device, otp_cert) do
435+
updated = Repo.preload(device, :device_certificates)
436436

437437
assign(socket, :device, updated)
438438
|> put_flash(:info, "Certificate Upload Successful")
439+
|> ok()
439440
else
440441
{:error, :malformed} ->
441-
put_flash(socket, :error, "Incorrect filetype or malformed certificate")
442+
{:ok, put_flash(socket, :error, "Incorrect filetype or malformed certificate")}
442443

443444
{:error, %Ecto.Changeset{errors: errors}} ->
444445
formatted =
445446
Enum.map_join(errors, "\n", fn {field, {msg, _}} ->
446447
["* ", to_string(field), " ", msg]
447448
end)
448449

449-
put_flash(socket, :error, IO.iodata_to_binary(["Failed to save:\n", formatted]))
450+
socket
451+
|> put_flash(:error, IO.iodata_to_binary(["Failed to save:\n", formatted]))
452+
|> ok()
450453

451454
err ->
452-
put_flash(socket, :error, "Unknown file error - #{inspect(err)}")
455+
socket
456+
|> put_flash(:error, "Unknown file error - #{inspect(err)}")
457+
|> ok()
453458
end
454459
end
455460

0 commit comments

Comments
 (0)