Skip to content

Commit

Permalink
Show helpful message when uploading duplicate firmware (#1627)
Browse files Browse the repository at this point in the history
  • Loading branch information
nshoes committed Nov 8, 2024
1 parent e77692c commit 75a3521
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
22 changes: 22 additions & 0 deletions lib/nerves_hub_web/live/firmware.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule NervesHubWeb.Live.Firmware do
socket
|> page_title("Upload Firmware - #{product.name}")
|> assign(:error_message, nil)
|> assign(:error_code, nil)
|> allow_upload(:firmware,
accept: ~w(.fw),
max_entries: 1,
Expand Down Expand Up @@ -140,6 +141,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 +162,17 @@ 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(:uuid, 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={"/org/#{@org.name}/#{@product.name}/firmware/#{@uuid}"} 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 75a3521

Please sign in to comment.