Update AuthorRole and SelectedAuthor component to require Author.

dev
KKlochko 2 months ago
parent d82e4c20fc
commit 59cc87cd97

@ -27,7 +27,7 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRole do
create :create do
primary? true
accept [:id, :order, :role]
argument :author_id, :uuid
argument :author_id, :uuid, allow_nil?: false
change fn changeset, _ ->
author_id =
@ -57,7 +57,7 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRole do
update :update do
primary? true
accept [:order, :role]
argument :author_id, :uuid, allow_nil?: true
argument :author_id, :uuid, allow_nil?: false
require_atomic? false
change fn changeset, _ ->
@ -121,7 +121,7 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRole do
end
belongs_to :author, Metadata.Author do
allow_nil? true
allow_nil? false
public? true
end
end

@ -2,6 +2,8 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents.SelectedAuthor do
use Phoenix.Component
use DecentralisedBookIndexWeb, :verified_routes
alias DecentralisedBookIndexWeb.CoreComponents
attr :author_form, :map, default: nil
attr :form_path, :string, default: ""
attr :class, :string, default: ""
@ -16,16 +18,32 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents.SelectedAuthor do
DecentralisedBookIndex.Metadata.get_author_by_id!(author_id)
end
assigns = assign(assigns, :author, author)
errors =
if Phoenix.Component.used_input?(assigns.author_form), do: assigns.author_form.errors, else: []
IO.inspect("errors")
IO.inspect(errors)
assigns =
assigns
|> assign(:author, author)
|> assign(:errors, Enum.map(errors, &CoreComponents.translate_error(&1)))
~H"""
<button type="button" phx-click="open-select-author" phx-target={@notify_component} phx-value-path={@form_path} 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 #{@class}"}>
<button
type="button"
phx-click="open-select-author"
phx-target={@notify_component}
phx-value-path={@form_path}
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 #{@class}"}
>
<%= if @author != nil do %>
{@author.name}
<% else %>
Select an author
<% end %>
</button>
<CoreComponents.error :for={msg <- @errors}>{msg}</CoreComponents.error>
"""
end
end

@ -236,7 +236,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
</td>
<td class="px-3 w-48">
<label for={author_roles_form[:author_id].id} class="hidden">Type</label>
<.input field={author_roles_form[:author_id]} type="hidden" />
<.input field={author_roles_form[:author_id]} type="hidden" show_errors?={false} />
<.selected_author
author_form={author_roles_form[:author_id]}
notify_component={@notify_component}

Loading…
Cancel
Save