From 8d6e2b98f804a63abbcd5343c2763e5e3e67d241 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Sat, 15 Feb 2025 18:06:59 +0000 Subject: [PATCH] Simple import without error handling --- .../record_live/barcode_scanner_component.ex | 43 +++++++++++-------- priv/gettext/default.pot | 20 ++++++--- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/lib/music_library_web/live/record_live/barcode_scanner_component.ex b/lib/music_library_web/live/record_live/barcode_scanner_component.ex index e6268b71..c14afed7 100644 --- a/lib/music_library_web/live/record_live/barcode_scanner_component.ex +++ b/lib/music_library_web/live/record_live/barcode_scanner_component.ex @@ -11,21 +11,7 @@ defmodule MusicLibraryWeb.RecordLive.BarcodeScannerComponent do {:ok, socket |> assign(:camera, :pending) - |> assign(:releases, %{ - "639842709422" => %MusicBrainz.ReleaseSearchResult{ - id: "dc393148-be34-4056-be66-b2b95905c5c1", - title: "Equally Cursed and Blessed", - release_group: %{ - id: "c35fc446-65cc-3645-939b-1b3782e60639", - type: :album, - title: "Equally Cursed and Blessed" - }, - artists: "Catatonia", - date: "1999-04-12", - barcode: "639842709422", - media: [%{format: "CD", disc_count: 5, track_count: 11}] - } - })} + |> assign(:releases, %{})} end @impl true @@ -68,7 +54,10 @@ defmodule MusicLibraryWeb.RecordLive.BarcodeScannerComponent do
0} class="mt-4 flex justify-center"> - <.button phx-click={JS.push("import_releases", target: "#barcode-scanner")}> + <.button + phx-disable-with={gettext("Importing...")} + phx-click={JS.push("import_releases", target: "#barcode-scanner")} + > {gettext("Import releases")}
@@ -121,7 +110,6 @@ defmodule MusicLibraryWeb.RecordLive.BarcodeScannerComponent do socket = case Records.search_release_by_barcode(number) do {:ok, [best_match_release | _other_releases]} -> - best_match_release |> IO.inspect() assign(socket, :releases, Map.put(socket.assigns.releases, number, best_match_release)) {:ok, []} -> @@ -142,6 +130,27 @@ defmodule MusicLibraryWeb.RecordLive.BarcodeScannerComponent do {:noreply, socket} end + def handle_event("import_releases", _params, socket) do + current_time = DateTime.utc_now() + # TODO: error handling when a release fails to import + :ok = + socket.assigns.releases + |> Map.values() + |> Enum.each(fn release -> + Records.import_from_musicbrainz_release(release.id, + format: MusicBrainz.ReleaseSearchResult.format(release), + purchased_at: current_time + ) + end) + + qs = %{order: :purchase} + + {:noreply, + socket + |> put_flash(:info, gettext("Records imported successfully")) + |> push_patch(to: ~p"/collection?#{qs}")} + end + defp release_format_label(release) do release |> MusicBrainz.ReleaseSearchResult.format() diff --git a/priv/gettext/default.pot b/priv/gettext/default.pot index 139ea2a7..e7dd94ee 100644 --- a/priv/gettext/default.pot +++ b/priv/gettext/default.pot @@ -630,27 +630,37 @@ msgstr "" msgid "Scan barcodes ยท Collection" msgstr "" -#: lib/music_library_web/live/record_live/barcode_scanner_component.ex:37 +#: lib/music_library_web/live/record_live/barcode_scanner_component.ex:23 #, elixir-autogen, elixir-format msgid "Scan one or more barcodes" msgstr "" -#: lib/music_library_web/live/record_live/barcode_scanner_component.ex:65 +#: lib/music_library_web/live/record_live/barcode_scanner_component.ex:51 #, elixir-autogen, elixir-format msgid "Open camera" msgstr "" -#: lib/music_library_web/live/record_live/barcode_scanner_component.ex:72 +#: lib/music_library_web/live/record_live/barcode_scanner_component.ex:61 #, elixir-autogen, elixir-format msgid "Import releases" msgstr "" -#: lib/music_library_web/live/record_live/barcode_scanner_component.ex:138 +#: lib/music_library_web/live/record_live/barcode_scanner_component.ex:126 #, elixir-autogen, elixir-format msgid "Failed to search release for barcode %{number}" msgstr "" -#: lib/music_library_web/live/record_live/barcode_scanner_component.ex:131 +#: lib/music_library_web/live/record_live/barcode_scanner_component.ex:119 #, elixir-autogen, elixir-format msgid "No release found for barcode %{number}" msgstr "" + +#: lib/music_library_web/live/record_live/barcode_scanner_component.ex:58 +#, elixir-autogen, elixir-format +msgid "Importing..." +msgstr "" + +#: lib/music_library_web/live/record_live/barcode_scanner_component.ex:150 +#, elixir-autogen, elixir-format +msgid "Records imported successfully" +msgstr ""