Surface scan+import errors

This commit is contained in:
Claudio Ortolina
2025-03-07 11:48:18 +00:00
parent fee2799d50
commit 8a80062c80
3 changed files with 37 additions and 8 deletions
+13 -1
View File
@@ -26,7 +26,19 @@ defmodule MusicLibrary.BarcodeScan do
end
end
def import(scan_result, current_time) do
def import_results(scan_results, current_time) do
Enum.reduce(scan_results, [], fn scan_result, errors ->
case import_result(scan_result, current_time) do
{:error, reason} ->
[{scan_result.number, reason} | errors]
_ ->
errors
end
end)
end
defp import_result(scan_result, current_time) do
case scan_result.status do
:new ->
Records.import_from_musicbrainz_release(scan_result.release.id,
@@ -193,18 +193,30 @@ defmodule MusicLibraryWeb.BarcodeScannerComponent do
def handle_event("import_releases", _params, socket) do
current_time = DateTime.utc_now()
# TODO: error handling when a release fails to import
:ok =
Enum.each(socket.assigns.scan_results, fn scan_result ->
BarcodeScan.import(scan_result, current_time)
end)
socket =
case BarcodeScan.import_results(socket.assigns.scan_results, current_time) do
[] ->
put_flash(socket, :info, gettext("Records imported successfully"))
errors ->
errors_summary =
Enum.map_join(errors, "\n", fn {number, reason} ->
"#{number}: #{inspect(reason)}"
end)
put_flash(
socket,
:error,
gettext("Some records could not be imported: %{summary}", summary: errors_summary)
)
end
qs = %{order: :purchase}
{:noreply,
socket
|> assign(:scan_results, [])
|> put_flash(:info, gettext("Records imported successfully"))
|> push_patch(to: ~p"/collection?#{qs}")}
end
+6 -1
View File
@@ -641,7 +641,7 @@ msgstr ""
msgid "Importing..."
msgstr ""
#: lib/music_library_web/components/barcode_scanner_component.ex:207
#: lib/music_library_web/components/barcode_scanner_component.ex:200
#, elixir-autogen, elixir-format
msgid "Records imported successfully"
msgstr ""
@@ -690,3 +690,8 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "Barcode not found"
msgstr ""
#: lib/music_library_web/components/barcode_scanner_component.ex:211
#, elixir-autogen, elixir-format
msgid "Some records could not be imported: %{summary}"
msgstr ""