Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show helpful message when uploading duplicate firmware #1627

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading