parent
f210fb0ff1
commit
4f86571579
@ -0,0 +1,53 @@
|
|||||||
|
defmodule DecentralisedBookIndexWeb.BookLive.Edit do
|
||||||
|
use DecentralisedBookIndexWeb, :live_view
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def render(assigns) do
|
||||||
|
~H"""
|
||||||
|
<.live_component
|
||||||
|
module={DecentralisedBookIndexWeb.BookLive.FormComponent}
|
||||||
|
id={(@book && @book.id) || :new}
|
||||||
|
title={@page_title}
|
||||||
|
current_user={@current_user}
|
||||||
|
action={@live_action}
|
||||||
|
book={@book}
|
||||||
|
/>
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def mount(_params, _session, socket) do
|
||||||
|
{:ok,
|
||||||
|
socket
|
||||||
|
|> stream(
|
||||||
|
:books,
|
||||||
|
Ash.read!(DecentralisedBookIndex.Metadata.Book, actor: socket.assigns[:current_user])
|
||||||
|
)
|
||||||
|
|> assign_new(:current_user, fn -> nil end)}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_params(params, _url, socket) do
|
||||||
|
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||||
|
socket
|
||||||
|
|> assign(:page_title, "Edit Book")
|
||||||
|
|> assign(
|
||||||
|
:book,
|
||||||
|
Ash.get!(DecentralisedBookIndex.Metadata.Book, id, actor: socket.assigns.current_user)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp apply_action(socket, :new, _params) do
|
||||||
|
socket
|
||||||
|
|> assign(:page_title, "New Book")
|
||||||
|
|> assign(:book, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_info({DecentralisedBookIndexWeb.BookLive.FormComponent, {:saved, book}}, socket) do
|
||||||
|
{:noreply, stream_insert(socket, :books, book)}
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in new issue