Add the update action for the Book FormComponent.

dev
KKlochko 3 months ago
parent 72875c20d2
commit 5f786cfb4b

@ -47,12 +47,12 @@ defmodule DecentralisedBookIndex.Metadata.AuthorRole do
update :update do update :update do
primary? true primary? true
accept [:order, :role] accept [:order, :role]
argument :author_id, :uuid argument :author_id, :uuid, allow_nil?: true
require_atomic? false require_atomic? false
change fn changeset, _ -> change fn changeset, _ ->
author_id = changeset.arguments.author_id author_id =
Map.get(changeset.arguments, :author_id, nil)
if author_id != nil do if author_id != nil do
{:ok, author} = DecentralisedBookIndex.Metadata.get_author_by_id(author_id) {:ok, author} = DecentralisedBookIndex.Metadata.get_author_by_id(author_id)

@ -13,7 +13,7 @@ defmodule DecentralisedBookIndex.Metadata.Book do
end end
actions do actions do
defaults [:read, :update, :destroy] defaults [:read, :destroy]
create :create do create :create do
primary? true primary? true
@ -112,6 +112,29 @@ defmodule DecentralisedBookIndex.Metadata.Book do
pagination offset?: true, default_limit: 10 pagination offset?: true, default_limit: 10
end end
update :update do
primary? true
require_atomic? false
accept [:title, :description, :format, :language, :page_count, :published, :publisher_id, :cover_image_url, :book_editions_registry_id]
argument :bids, {:array, :map}
argument :author_roles, {:array, :map}
change fn changeset, _ ->
registry_id = Ash.Changeset.get_attribute(changeset, :book_editions_registry_id)
if registry_id == nil do
{:ok, registry} = DecentralisedBookIndex.Metadata.create_book_editions_registry()
Ash.Changeset.force_change_attribute(changeset, :book_editions_registry_id, registry.id)
else
changeset
end
end
change manage_relationship(:bids, type: :direct_control, order_is_key: :order)
change manage_relationship(:author_roles, type: :direct_control, order_is_key: :order)
end
update :assign_cover_image do update :assign_cover_image do
accept [:cover_image_url] accept [:cover_image_url]
end end

@ -42,6 +42,28 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
/> />
<% end %> <% end %>
<%= if @form.source.type == :update do %> <%= if @form.source.type == :update do %>
<.input field={@form[:title]} type="text" label="Title" />
<.input field={@form[:cover_image_url]} type="text" label="Cover image url" />
<.input
field={@form[:description]}
type="textarea"
label="Description"
/>
<.bids_inputs form={@form} myself={@myself} />
<.author_roles_inputs form={@form} myself={@myself} />
<.input field={@form[:format]} type="text" label="Format" />
<.input
field={@form[:language]}
type="text"
label="Language"
/>
<.input field={@form[:page_count]} type="number" label="Page count" />
<.input field={@form[:publisher_id]} type="text" label="Publisher" />
<.input
field={@form[:published]}
type="date"
label="Published"
/>
<% end %> <% end %>
<:actions> <:actions>
@ -253,6 +275,10 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
defp assign_form(%{assigns: %{book: book}} = socket) do defp assign_form(%{assigns: %{book: book}} = socket) do
form = form =
if book do if book do
book =
book
|> Ash.load!([:bids, :author_roles, :publisher])
AshPhoenix.Form.for_update(book, :update, as: "book", actor: socket.assigns.current_user) AshPhoenix.Form.for_update(book, :update, as: "book", actor: socket.assigns.current_user)
else else
AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.Book, :create, AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.Book, :create,

Loading…
Cancel
Save