From d82e4c20fc8382c7f8f2c5c793e3e396c07bf4b9 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Wed, 7 May 2025 18:33:38 +0300 Subject: [PATCH] Update Book and SelectedPublisher component to require Publisher. --- lib/decentralised_book_index/metadata/book.ex | 1 + .../my_components/selected_publisher.ex | 19 +++++++++++++++++-- .../live/book_live/form_component.ex | 4 ++-- .../metadata/forms/book_form_test.exs | 6 ++++-- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/decentralised_book_index/metadata/book.ex b/lib/decentralised_book_index/metadata/book.ex index 6d4115f..d30cff5 100644 --- a/lib/decentralised_book_index/metadata/book.ex +++ b/lib/decentralised_book_index/metadata/book.ex @@ -363,6 +363,7 @@ defmodule DecentralisedBookIndex.Metadata.Book do end belongs_to :publisher, Metadata.Publisher do + allow_nil? false public? true end diff --git a/lib/decentralised_book_index_web/components/my_components/selected_publisher.ex b/lib/decentralised_book_index_web/components/my_components/selected_publisher.ex index 8ff91e9..1eded02 100644 --- a/lib/decentralised_book_index_web/components/my_components/selected_publisher.ex +++ b/lib/decentralised_book_index_web/components/my_components/selected_publisher.ex @@ -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""" - + + {msg} """ end end 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 d294bac..3d2c87c 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 @@ -36,7 +36,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do /> <.input field={@form[:page_count]} type="number" label="Page count" />
- <.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} />
<.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" />
- <.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} />
<.input field={@form[:published]} type="date" label="Published" /> diff --git a/test/decentralised_book_index/metadata/forms/book_form_test.exs b/test/decentralised_book_index/metadata/forms/book_form_test.exs index d87440c..ebea1f3 100644 --- a/test/decentralised_book_index/metadata/forms/book_form_test.exs +++ b/test/decentralised_book_index/metadata/forms/book_form_test.exs @@ -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 =