This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
defmodule DecentralisedBookIndexWeb.Components.MyComponents.SelectAuthorAlias do
|
||||
use DecentralisedBookIndexWeb, :live_component
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div>
|
||||
<.header>
|
||||
Select another alias of the author
|
||||
</.header>
|
||||
|
||||
<form class="flex items-center" phx-change="search" phx-target={@myself}>
|
||||
<label for="simple-search" class="sr-only">Search</label>
|
||||
<div class="relative w-full">
|
||||
<div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
|
||||
<svg
|
||||
aria-hidden="true"
|
||||
class="w-5 h-5 text-gray-500 dark:text-gray-400"
|
||||
fill="currentColor"
|
||||
viewBox="0 0 20 20"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<path
|
||||
fill-rule="evenodd"
|
||||
d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z"
|
||||
clip-rule="evenodd"
|
||||
>
|
||||
</path>
|
||||
</svg>
|
||||
</div>
|
||||
<input
|
||||
name="query"
|
||||
type="text"
|
||||
phx-debounce="300"
|
||||
autocomplete="off"
|
||||
id="simple-search"
|
||||
class="block w-full p-2 pl-10 text-sm text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500"
|
||||
placeholder="Search"
|
||||
value=""
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<%= for author_alias <- @page.results do %>
|
||||
<div
|
||||
phx-click="select-author-alias"
|
||||
phx-target={@notify_component}
|
||||
phx-value-author-alias={author_alias.author_alias_registry_id}
|
||||
class="w-full bg-white border border-gray-200 rounded-lg shadow-sm dark:bg-gray-800 dark:border-gray-700 mx-auto my-3"
|
||||
>
|
||||
<h5 class="mb-1 text-lg font-medium text-gray-900 dark:text-white px-2 pt-1">
|
||||
{author_alias.name}
|
||||
</h5>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
"""
|
||||
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
|
||||
@@ -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"""
|
||||
<button
|
||||
type="button"
|
||||
phx-click="open-select-author-alias"
|
||||
phx-target={@notify_component}
|
||||
class={"text-white bg-blue-700 hover:bg-blue-800 focus:outline-none focus:ring-4 focus:ring-blue-300 font-medium rounded-full text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"}
|
||||
>
|
||||
Change the author alias
|
||||
</button>
|
||||
"""
|
||||
end
|
||||
end
|
||||
@@ -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" />
|
||||
<div>
|
||||
<.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} />
|
||||
</div>
|
||||
<% 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" />
|
||||
<div>
|
||||
<.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} />
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<:actions>
|
||||
@@ -56,6 +46,20 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do
|
||||
</.cancel_button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
|
||||
<.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}
|
||||
/>
|
||||
</.modal>
|
||||
</div>
|
||||
"""
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user