Edit notes instead of record notes

This commit is contained in:
Claudio Ortolina
2025-09-07 12:22:45 +03:00
parent 824517dcba
commit 45610a4a9a
4 changed files with 80 additions and 23 deletions
@@ -1,23 +1,36 @@
defmodule MusicLibraryWeb.NotesComponent do defmodule MusicLibraryWeb.NotesComponent do
use MusicLibraryWeb, :live_component use MusicLibraryWeb, :live_component
alias MusicLibrary.Records alias MusicLibrary.Notes
alias MusicLibrary.Notes.Note
def open(id), do: Fluxon.open_dialog(id) def open(id), do: Fluxon.open_dialog(id)
@impl true @impl true
def update(assigns, socket) do def update(assigns, socket) do
note = find_or_initialize_note(assigns)
changeset = changeset =
assigns.record Note.changeset(note, %{})
|> Records.change_record()
{:ok, {:ok,
socket socket
|> assign(assigns) |> assign(assigns)
|> assign(:mode, initial_mode(assigns.record)) |> assign(:note, note)
|> assign(:mode, initial_mode(changeset))
|> assign(:form, to_form(changeset))} |> assign(:form, to_form(changeset))}
end end
defp find_or_initialize_note(%{entity: entity, musicbrainz_id: musicbrainz_id}) do
case Notes.get_note(entity, musicbrainz_id) do
nil ->
%Note{entity: entity, musicbrainz_id: musicbrainz_id, content: ""}
note ->
note
end
end
@impl true @impl true
def render(assigns) do def render(assigns) do
~H""" ~H"""
@@ -38,19 +51,19 @@ defmodule MusicLibraryWeb.NotesComponent do
</.tabs_list> </.tabs_list>
<.tabs_panel active={@mode == "read"} name="read"> <.tabs_panel active={@mode == "read"} name="read">
<div class="w-full mt-5 text-sm/8"> <div class="w-full mt-5 text-sm/8">
{render_notes(@form[:notes].value)} {render_notes(@form[:content].value)}
</div> </div>
</.tabs_panel> </.tabs_panel>
<.tabs_panel active={@mode == "edit"} name="edit"> <.tabs_panel active={@mode == "edit"} name="edit">
<.simple_form <.simple_form
for={@form} for={@form}
id="record-notes-form" id="notes-form"
phx-target={@myself} phx-target={@myself}
phx-change="validate" phx-change="validate"
phx-auto-recover="recover_form" phx-auto-recover="recover_form"
phx-submit="save" phx-submit="save"
> >
<.textarea class="w-full h-96 font-mono text-sm/8" field={@form[:notes]} /> <.textarea class="w-full h-96 font-mono text-sm/8" field={@form[:content]} />
<:actions> <:actions>
<div class="w-full md:flex md:justify-center"> <div class="w-full md:flex md:justify-center">
@@ -72,20 +85,16 @@ defmodule MusicLibraryWeb.NotesComponent do
end end
@impl true @impl true
def handle_event("validate", %{"record" => record_params}, socket) do def handle_event("validate", %{"note" => note_params}, socket) do
changeset = Records.change_record(socket.assigns.record, record_params) changeset = Notes.change_note(socket.assigns.note, note_params)
{:noreply, assign(socket, form: to_form(changeset, action: :validate))} {:noreply, assign(socket, form: to_form(changeset, action: :validate))}
end end
def handle_event("save", %{"record" => record_params}, socket) do def handle_event("save", %{"note" => note_params}, socket) do
case Records.update_record(socket.assigns.record, record_params) do if socket.assigns.note.id do
{:ok, _record} -> update_note(note_params, socket)
{:noreply, else
socket create_note(note_params, socket)
|> put_toast(:info, gettext("Record updated successfully"))}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end end
end end
@@ -97,8 +106,37 @@ defmodule MusicLibraryWeb.NotesComponent do
{:noreply, assign(socket, :mode, mode)} {:noreply, assign(socket, :mode, mode)}
end end
defp initial_mode(record) when record.notes in [nil, ""], do: "edit" defp update_note(note_params, socket) do
defp initial_mode(_record), do: "read" case Notes.update_note(socket.assigns.note, note_params) do
{:ok, _record} ->
{:noreply,
socket
|> put_toast(:info, gettext("Note updated successfully"))}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end
defp create_note(note_params, socket) do
case Notes.create_note(socket.assigns.note, note_params) do
{:ok, _record} ->
{:noreply,
socket
|> put_toast(:info, gettext("Note created successfully"))}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end
defp initial_mode(changeset) do
if Ecto.Changeset.get_field(changeset, :content) in [nil, ""] do
"edit"
else
"read"
end
end
defp render_notes(notes) do defp render_notes(notes) do
add_a_classes = add_a_classes =
@@ -336,7 +336,8 @@
id="record-notes" id="record-notes"
sheet_id="record-notes-sheet" sheet_id="record-notes-sheet"
module={MusicLibraryWeb.NotesComponent} module={MusicLibraryWeb.NotesComponent}
record={@record} entity={:record}
musicbrainz_id={@record.musicbrainz_id}
/> />
<.structured_modal <.structured_modal
+10 -1
View File
@@ -156,7 +156,6 @@ msgstr ""
msgid "Record imported successfully" msgid "Record imported successfully"
msgstr "" msgstr ""
#: lib/music_library_web/components/notes_component.ex
#: lib/music_library_web/components/record_form_component.ex #: lib/music_library_web/components/record_form_component.ex
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Record updated successfully" msgid "Record updated successfully"
@@ -1324,3 +1323,13 @@ msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Read" msgid "Read"
msgstr "" msgstr ""
#: lib/music_library_web/components/notes_component.ex
#, elixir-autogen, elixir-format
msgid "Note created successfully"
msgstr ""
#: lib/music_library_web/components/notes_component.ex
#, elixir-autogen, elixir-format
msgid "Note updated successfully"
msgstr ""
+10 -1
View File
@@ -156,7 +156,6 @@ msgstr ""
msgid "Record imported successfully" msgid "Record imported successfully"
msgstr "" msgstr ""
#: lib/music_library_web/components/notes_component.ex
#: lib/music_library_web/components/record_form_component.ex #: lib/music_library_web/components/record_form_component.ex
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Record updated successfully" msgid "Record updated successfully"
@@ -1324,3 +1323,13 @@ msgstr ""
#, elixir-autogen, elixir-format, fuzzy #, elixir-autogen, elixir-format, fuzzy
msgid "Read" msgid "Read"
msgstr "" msgstr ""
#: lib/music_library_web/components/notes_component.ex
#, elixir-autogen, elixir-format, fuzzy
msgid "Note created successfully"
msgstr ""
#: lib/music_library_web/components/notes_component.ex
#, elixir-autogen, elixir-format, fuzzy
msgid "Note updated successfully"
msgstr ""