Skip to content

Commit

Permalink
Show helpful message when uploading duplicate firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
nshoes committed Nov 4, 2024
1 parent b7efd90 commit 02de318
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
24 changes: 24 additions & 0 deletions lib/nerves_hub_web/live/firmware.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ defmodule NervesHubWeb.Live.Firmware do
}} ->
error_feedback(socket, "No matching product could be found.")

{:error,
%Ecto.Changeset{
errors: [
uuid:
{"has already been taken",
[constraint: :unique, constraint_name: "firmwares_product_id_uuid_index"]}
]
} = changeset} ->
error_feedback(socket, changeset)

{:error, %Ecto.Changeset{}} ->
error_feedback(socket, "Unknown error uploading firmware.")

Expand All @@ -151,6 +161,20 @@ defmodule NervesHubWeb.Live.Firmware do
end
end

defp error_feedback(
socket,
%Ecto.Changeset{errors: [uuid: {"has already been taken", _}]} = changeset
) do
socket
|> assign(:error_message, "Duplicate Firmware Is Not Allowed")
|> assign(:error_code, :duplicate_firmware)
|> assign(
:firmware_path,
"/org/#{socket.assigns.org.name}/#{socket.assigns.product.name}/firmware/#{changeset.changes.uuid}"
)
|> noreply()
end

defp error_feedback(socket, %Ecto.Changeset{} = changeset) do
error_message =
changeset.errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
<.live_file_input upload={@uploads.firmware} required />

<div class="has-error">
<span :if={@error_message} class="help-block"><%= @error_message %></span>
<span :if={@error_message} class="help-block">
<%= case @error_code do %>
<% :duplicate_firmware -> %>
<%= @error_message %> <a href={@firmware_path} target="_blank">View Firmware</a>
<% _ -> %>
<%= @error_message %>
<% end %>
</span>
</div>

<div :for={entry <- @uploads.firmware.entries} class="mt-1">
Expand Down

0 comments on commit 02de318

Please sign in to comment.