Update Book and SelectedPublisher component to require Publisher.

dev
KKlochko 2 months ago
parent 61338cd404
commit d82e4c20fc

@ -363,6 +363,7 @@ defmodule DecentralisedBookIndex.Metadata.Book do
end
belongs_to :publisher, Metadata.Publisher do
allow_nil? false
public? true
end

@ -2,6 +2,8 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents.SelectedPublisher do
use Phoenix.Component
use DecentralisedBookIndexWeb, :verified_routes
alias DecentralisedBookIndexWeb.CoreComponents
attr :publisher_form, :map, default: nil
def selected_publisher(assigns) do
@ -14,16 +16,29 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents.SelectedPublisher do
DecentralisedBookIndex.Metadata.get_publisher_by_id!(publisher_id)
end
assigns = assign(assigns, :publisher, publisher)
errors =
if Phoenix.Component.used_input?(assigns.publisher_form), do: assigns.publisher_form.errors, else: []
assigns =
assigns
|> assign(:publisher, publisher)
|> assign(:errors, Enum.map(errors, &CoreComponents.translate_error(&1)))
~H"""
<button type="button" phx-click="open-select-publisher" 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">
<button
type="button"
phx-click="open-select-publisher"
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"
>
<%= if @publisher != nil do %>
{@publisher.name}
<% else %>
Select a publisher
<% end %>
</button>
<CoreComponents.error :for={msg <- @errors}>{msg}</CoreComponents.error>
"""
end
end

@ -36,7 +36,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
/>
<.input field={@form[:page_count]} type="number" label="Page count" />
<div>
<.input field={@form[:publisher_id]} type="text" label="Publisher" type="hidden" />
<.input field={@form[:publisher_id]} type="text" label="Publisher" type="hidden" show_errors?={false} />
<.selected_publisher publisher_form={@form[:publisher_id]} notify_component={@myself} />
</div>
<.input field={@form[:published]} type="date" label="Published" />
@ -63,7 +63,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
/>
<.input field={@form[:page_count]} type="number" label="Page count" />
<div>
<.input field={@form[:publisher_id]} type="text" label="Publisher" type="hidden" />
<.input field={@form[:publisher_id]} type="text" label="Publisher" type="hidden" show_errors?={false} />
<.selected_publisher publisher_form={@form[:publisher_id]} notify_component={@myself} />
</div>
<.input field={@form[:published]} type="date" label="Published" />

@ -63,8 +63,9 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
end
describe "create new book via the form" do
test "just name and description", %{user: user} do
test "with all required attributes", %{user: user} do
bids = bids(actor: user)
publisher = generate(publisher())
author_roles = author_roles(actor: user)
valid_params = %{
@ -75,7 +76,8 @@ defmodule DecentralisedBookIndex.Metadata.Forms.BookFormTest do
"page_count" => 600,
"published" => ~D[2025-03-06],
"author_roles" => author_roles,
"bids" => bids
"bids" => bids,
"publisher_id" => publisher.id
}
form =

Loading…
Cancel
Save