From 42cba07009970aa45df8dd0b40f3ef8864e2b57a Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 21 Mar 2025 10:58:09 +0200 Subject: [PATCH] Add the author roles to the Book form. --- .../metadata/author_role.ex | 23 ++++++- .../live/book_live/form_component.ex | 65 +++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/lib/decentralised_book_index/metadata/author_role.ex b/lib/decentralised_book_index/metadata/author_role.ex index 4c519a6..85a6bd6 100644 --- a/lib/decentralised_book_index/metadata/author_role.ex +++ b/lib/decentralised_book_index/metadata/author_role.ex @@ -12,7 +12,7 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRole do end actions do - defaults [:read, :update, :destroy] + defaults [:read, :destroy] create :create do primary? true @@ -20,7 +20,8 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRole do argument :author_id, :uuid change fn changeset, _ -> - author_id = changeset.arguments.author_id + author_id = + Map.get(changeset.arguments, :author_id, nil) if author_id != nil do {:ok, author} = DecentralisedBookIndex.Metadata.get_author_by_id(author_id) @@ -37,6 +38,24 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRole do get? true filter expr(id == ^arg(:id)) end + + update :update do + primary? true + accept [:order, :role] + argument :author_id, :uuid + + change fn changeset, _ -> + author_id = changeset.arguments.author_id + + if author_id != nil do + {:ok, author} = DecentralisedBookIndex.Metadata.get_author_by_id(author_id) + + Ash.Changeset.force_change_attribute(changeset, :author_id, author.id) + else + changeset + end + end + end end attributes do diff --git a/lib/decentralised_book_index_web/live/book_live/form_component.ex b/lib/decentralised_book_index_web/live/book_live/form_component.ex index c25722d..5c602ec 100644 --- a/lib/decentralised_book_index_web/live/book_live/form_component.ex +++ b/lib/decentralised_book_index_web/live/book_live/form_component.ex @@ -25,6 +25,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do label="Description" /> <.bids_inputs form={@form} myself={@myself} /> + <.author_roles_inputs form={@form} myself={@myself} /> <.input field={@form[:format]} type="text" label="Format" /><.input field={@form[:language]} type="text" @@ -110,6 +111,70 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do {:noreply, socket} end + def author_roles_inputs(assigns) do + ~H""" +

Authors

+ + + + + + + + + + <.inputs_for :let={author_roles_form} field={@form[:author_roles]}> + + + + + + + +
AuthorRole
+ + <.input field={author_roles_form[:author_id]} /> + + + <.input field={author_roles_form[:role]} /> +
+ + <.button_link phx-click="add-author-role" phx-target={@myself} kind="primary" size="sm" inverse> + Add Author + + """ + end + + @impl true + def handle_event("add-author-role", params, socket) do + socket = + update(socket, :form, fn form -> + AshPhoenix.Form.add_form(form, :author_roles) + end) + + {:noreply, socket} + end + + @impl true + def handle_event("remove-author-role", %{"path" => path}, socket) do + socket = + update(socket, :form, fn form -> + AshPhoenix.Form.remove_form(form, path) + end) + + {:noreply, socket} + end + + @impl true + def handle_event("reorder-author-role", %{"order" => order}, socket) do + socket = + update(socket, :form, fn form -> + AshPhoenix.Form.sort_forms(form, [:author_roles], order) + end) + + {:noreply, socket} + end + @impl true def update(assigns, socket) do {:ok,