Add components to change the book edition.
This commit is contained in:
@@ -28,6 +28,9 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents do
|
|||||||
|
|
||||||
alias MyComponents.SelectAuthor
|
alias MyComponents.SelectAuthor
|
||||||
import MyComponents.SelectedAuthor, only: [selected_author: 1]
|
import MyComponents.SelectedAuthor, only: [selected_author: 1]
|
||||||
|
|
||||||
|
alias MyComponents.SelectBookEdition
|
||||||
|
import MyComponents.SelectedBookEdition, only: [selected_book_edition: 1]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
defmodule DecentralisedBookIndexWeb.Components.MyComponents.SelectBookEdition do
|
||||||
|
use DecentralisedBookIndexWeb, :live_component
|
||||||
|
alias DecentralisedBookIndex.Metadata
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def render(assigns) do
|
||||||
|
~H"""
|
||||||
|
<div>
|
||||||
|
<.header>
|
||||||
|
Select another edition of the book
|
||||||
|
</.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 book_edition <- @page.results do %>
|
||||||
|
<div
|
||||||
|
phx-click="select-book-edition"
|
||||||
|
phx-target={@notify_component}
|
||||||
|
phx-value-book-edition={book_edition.book_editions_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">
|
||||||
|
{book_edition.title}
|
||||||
|
</h5>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def update(assigns, socket) do
|
||||||
|
{:ok,
|
||||||
|
socket
|
||||||
|
|> assign(assigns)
|
||||||
|
|> assign(:book_edition_query, "")
|
||||||
|
|> search()}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("search", %{"query" => query}, socket) do
|
||||||
|
{:noreply,
|
||||||
|
socket
|
||||||
|
|> assign(:book_edition_query, query)
|
||||||
|
|> search()}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp search(socket) do
|
||||||
|
query = Map.get(socket.assigns, :book_edition_query, "")
|
||||||
|
page = Metadata.search_book!(query)
|
||||||
|
|
||||||
|
socket
|
||||||
|
|> assign(page: page)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
defmodule DecentralisedBookIndexWeb.Components.MyComponents.SelectedBookEdition do
|
||||||
|
use Phoenix.Component
|
||||||
|
use DecentralisedBookIndexWeb, :verified_routes
|
||||||
|
|
||||||
|
attr :book_editions_registry_form, :map, default: nil
|
||||||
|
|
||||||
|
def selected_book_edition(assigns) do
|
||||||
|
~H"""
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
phx-click="open-select-book-edition"
|
||||||
|
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 book edition
|
||||||
|
</button>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -37,16 +37,13 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
|
|||||||
<.input field={@form[:page_count]} type="number" label="Page count" />
|
<.input field={@form[:page_count]} type="number" label="Page count" />
|
||||||
<div>
|
<div>
|
||||||
<.input field={@form[:publisher_id]} type="text" label="Publisher" type="hidden" />
|
<.input field={@form[:publisher_id]} type="text" label="Publisher" type="hidden" />
|
||||||
<.selected_publisher
|
<.selected_publisher publisher_form={@form[:publisher_id]} notify_component={@myself} />
|
||||||
publisher_form={@form[:publisher_id]}
|
</div>
|
||||||
notify_component={@myself}
|
<.input field={@form[:published]} type="date" label="Published" />
|
||||||
/>
|
<div>
|
||||||
|
<.input field={@form[:book_editions_registry_id]} type="hidden" label="Book Editions" />
|
||||||
|
<.selected_book_edition book_editions_registry_form={@form[:book_editions_registry_id]} notify_component={@myself} />
|
||||||
</div>
|
</div>
|
||||||
<.input
|
|
||||||
field={@form[:published]}
|
|
||||||
type="date"
|
|
||||||
label="Published"
|
|
||||||
/>
|
|
||||||
<% 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[:title]} type="text" label="Title" />
|
||||||
@@ -67,16 +64,13 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
|
|||||||
<.input field={@form[:page_count]} type="number" label="Page count" />
|
<.input field={@form[:page_count]} type="number" label="Page count" />
|
||||||
<div>
|
<div>
|
||||||
<.input field={@form[:publisher_id]} type="text" label="Publisher" type="hidden" />
|
<.input field={@form[:publisher_id]} type="text" label="Publisher" type="hidden" />
|
||||||
<.selected_publisher
|
<.selected_publisher publisher_form={@form[:publisher_id]} notify_component={@myself} />
|
||||||
publisher_form={@form[:publisher_id]}
|
</div>
|
||||||
notify_component={@myself}
|
<.input field={@form[:published]} type="date" label="Published" />
|
||||||
/>
|
<div>
|
||||||
|
<.input field={@form[:book_editions_registry_id]} type="hidden" label="Book Editions" />
|
||||||
|
<.selected_book_edition book_editions_registry_form={@form[:book_editions_registry_id]} notify_component={@myself} />
|
||||||
</div>
|
</div>
|
||||||
<.input
|
|
||||||
field={@form[:published]}
|
|
||||||
type="date"
|
|
||||||
label="Published"
|
|
||||||
/>
|
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<:actions>
|
<:actions>
|
||||||
@@ -103,6 +97,20 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
|
|||||||
/>
|
/>
|
||||||
</.modal>
|
</.modal>
|
||||||
|
|
||||||
|
<.modal
|
||||||
|
:if={@select_book_edition_open? == true}
|
||||||
|
id="select-book-edition-modal"
|
||||||
|
show
|
||||||
|
on_cancel={JS.push("close-select-book-edition", target: @myself)}
|
||||||
|
>
|
||||||
|
<.live_component
|
||||||
|
id="select-book-edition"
|
||||||
|
module={SelectBookEdition}
|
||||||
|
current_user={@current_user}
|
||||||
|
notify_component={@myself}
|
||||||
|
/>
|
||||||
|
</.modal>
|
||||||
|
|
||||||
<.modal
|
<.modal
|
||||||
:if={@select_author_open? == true}
|
:if={@select_author_open? == true}
|
||||||
id="select-author-modal"
|
id="select-author-modal"
|
||||||
@@ -302,6 +310,7 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
|
|||||||
|> assign(assigns)
|
|> assign(assigns)
|
||||||
|> assign(:select_publisher_open?, false)
|
|> assign(:select_publisher_open?, false)
|
||||||
|> assign(:select_author_open?, false)
|
|> assign(:select_author_open?, false)
|
||||||
|
|> assign(:select_book_edition_open?, false)
|
||||||
|> assign(:form_path, nil)
|
|> assign(:form_path, nil)
|
||||||
|> assign_form()}
|
|> assign_form()}
|
||||||
end
|
end
|
||||||
@@ -448,4 +457,32 @@ defmodule DecentralisedBookIndexWeb.BookLive.FormComponent do
|
|||||||
|
|
||||||
{:noreply, socket}
|
{:noreply, socket}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_event("select-book-edition", %{"book-edition" => book_edition_id}, socket) do
|
||||||
|
socket =
|
||||||
|
socket
|
||||||
|
|> update(:form, fn form ->
|
||||||
|
form
|
||||||
|
|> AshPhoenix.Form.update_params(&Map.put(&1, "book_editions_registry_id", book_edition_id))
|
||||||
|
end)
|
||||||
|
|> assign(:select_book_edition_open?, false)
|
||||||
|
|
||||||
|
{:noreply, socket}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("open-select-book-edition", _params, socket) do
|
||||||
|
socket =
|
||||||
|
socket
|
||||||
|
|> assign(:select_book_edition_open?, true)
|
||||||
|
|
||||||
|
{:noreply, socket}
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("close-select-book-edition", _params, socket) do
|
||||||
|
socket =
|
||||||
|
socket
|
||||||
|
|> assign(:select_book_edition_open?, false)
|
||||||
|
|
||||||
|
{:noreply, socket}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user