From 38d6510e9ee7155c9df54bcaefe11c0a4ada8105 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 2 May 2025 15:15:07 +0300 Subject: [PATCH] Add components to change the author alias. --- .../metadata/author.ex | 15 +++- .../components/my_components.ex | 3 + .../my_components/select_author_alias.ex | 84 +++++++++++++++++++ .../my_components/selected_author_alias.ex | 19 +++++ .../live/author_live/form_component.ex | 76 ++++++++++++----- 5 files changed, 176 insertions(+), 21 deletions(-) create mode 100644 lib/decentralised_book_index_web/components/my_components/select_author_alias.ex create mode 100644 lib/decentralised_book_index_web/components/my_components/selected_author_alias.ex diff --git a/lib/decentralised_book_index/metadata/author.ex b/lib/decentralised_book_index/metadata/author.ex index 0c7fa35..0cfdf6f 100644 --- a/lib/decentralised_book_index/metadata/author.ex +++ b/lib/decentralised_book_index/metadata/author.ex @@ -176,7 +176,20 @@ defmodule DecentralisedBookIndex.Metadata.Author do update :update do primary? true - accept [:name, :description, :avatar_url] + accept [:name, :description, :avatar_url, :author_alias_registry_id] + require_atomic? false + + change fn changeset, _ -> + registry_id = Ash.Changeset.get_attribute(changeset, :author_alias_registry_id) + + if registry_id == nil do + {:ok, registry} = DecentralisedBookIndex.Metadata.create_author_alias_registry() + + Ash.Changeset.force_change_attribute(changeset, :author_alias_registry_id, registry.id) + else + changeset + end + end end update :assign_avatar_image do diff --git a/lib/decentralised_book_index_web/components/my_components.ex b/lib/decentralised_book_index_web/components/my_components.ex index dfca610..be296a1 100644 --- a/lib/decentralised_book_index_web/components/my_components.ex +++ b/lib/decentralised_book_index_web/components/my_components.ex @@ -31,6 +31,9 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents do alias MyComponents.SelectBookEdition import MyComponents.SelectedBookEdition, only: [selected_book_edition: 1] + + alias MyComponents.SelectAuthorAlias + import MyComponents.SelectedAuthorAlias, only: [selected_author_alias: 1] end end end diff --git a/lib/decentralised_book_index_web/components/my_components/select_author_alias.ex b/lib/decentralised_book_index_web/components/my_components/select_author_alias.ex new file mode 100644 index 0000000..5d0e565 --- /dev/null +++ b/lib/decentralised_book_index_web/components/my_components/select_author_alias.ex @@ -0,0 +1,84 @@ +defmodule DecentralisedBookIndexWeb.Components.MyComponents.SelectAuthorAlias do + use DecentralisedBookIndexWeb, :live_component + alias DecentralisedBookIndex.Metadata + + @impl true + def render(assigns) do + ~H""" +
+ <.header> + Select another alias of the author + + +
+ +
+
+ +
+ +
+
+ + <%= for author_alias <- @page.results do %> +
+
+ {author_alias.name} +
+
+ <% end %> +
+ """ + end + + @impl true + def update(assigns, socket) do + {:ok, + socket + |> assign(assigns) + |> assign(:author_alias_query, "") + |> search()} + end + + def handle_event("search", %{"query" => query}, socket) do + {:noreply, + socket + |> assign(:author_alias_query, query) + |> search()} + end + + defp search(socket) do + query = Map.get(socket.assigns, :author_alias_query, "") + page = Metadata.search_author!(query) + + socket + |> assign(page: page) + end +end diff --git a/lib/decentralised_book_index_web/components/my_components/selected_author_alias.ex b/lib/decentralised_book_index_web/components/my_components/selected_author_alias.ex new file mode 100644 index 0000000..5c36023 --- /dev/null +++ b/lib/decentralised_book_index_web/components/my_components/selected_author_alias.ex @@ -0,0 +1,19 @@ +defmodule DecentralisedBookIndexWeb.Components.MyComponents.SelectedAuthorAlias do + use Phoenix.Component + use DecentralisedBookIndexWeb, :verified_routes + + attr :author_alias_registry_form, :map, default: nil + + def selected_author_alias(assigns) do + ~H""" + + """ + end +end diff --git a/lib/decentralised_book_index_web/live/author_live/form_component.ex b/lib/decentralised_book_index_web/live/author_live/form_component.ex index 085758d..0536851 100644 --- a/lib/decentralised_book_index_web/live/author_live/form_component.ex +++ b/lib/decentralised_book_index_web/live/author_live/form_component.ex @@ -21,30 +21,20 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do <%= if @form.source.type == :create do %> <.input field={@form[:name]} type="text" label="Name" /> <.input field={@form[:avatar_url]} type="text" label="Cover image url" /> - <.input - field={@form[:description]} - type="textarea" - label="Description" - /> - <.input - field={@form[:author_alias_registry_id]} - type="text" - label="Author alias registry" - /> + <.input field={@form[:description]} type="textarea" label="Description" /> +
+ <.input field={@form[:author_alias_registry_id]} type="hidden" label="Author alias registry" /> + <.selected_author_alias author_alias_registry_form={@form[:author_alias_registry_id]} notify_component={@myself} /> +
<% end %> <%= if @form.source.type == :update do %> <.input field={@form[:name]} type="text" label="Name" /> <.input field={@form[:avatar_url]} type="text" label="Cover image url" /> - <.input - field={@form[:description]} - type="textarea" - label="Description" - /> - <.input - field={@form[:author_alias_registry_id]} - type="text" - label="Author alias registry" - /> + <.input field={@form[:description]} type="textarea" label="Description" /> +
+ <.input field={@form[:author_alias_registry_id]} type="hidden" label="Author alias registry" /> + <.selected_author_alias author_alias_registry_form={@form[:author_alias_registry_id]} notify_component={@myself} /> +
<% end %> <:actions> @@ -56,6 +46,20 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do + + <.modal + :if={@select_author_alias_open? == true} + id="select-author-alias-modal" + show + on_cancel={JS.push("close-select-author-alias", target: @myself)} + > + <.live_component + id="select-author-alias" + module={SelectAuthorAlias} + current_user={@current_user} + notify_component={@myself} + /> + """ end @@ -65,6 +69,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do {:ok, socket |> assign(assigns) + |> assign(:select_author_alias_open?, false) |> assign_form()} end @@ -151,4 +156,35 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do _ -> ~p"/authors" end end + + def handle_event("select-author-alias", %{"author-alias" => author_alias_id}, socket) do + IO.inspect("book_editions_registry_id") + IO.inspect(author_alias_id) + + socket = + socket + |> update(:form, fn form -> + form + |> AshPhoenix.Form.update_params(&Map.put(&1, "author_alias_registry_id", author_alias_id)) + end) + |> assign(:select_author_alias_open?, false) + + {:noreply, socket} + end + + def handle_event("open-select-author-alias", _params, socket) do + socket = + socket + |> assign(:select_author_alias_open?, true) + + {:noreply, socket} + end + + def handle_event("close-select-author-alias", _params, socket) do + socket = + socket + |> assign(:select_author_alias_open?, false) + + {:noreply, socket} + end end