Add the currect user as an actor for Author's LiveViews.

dev
KKlochko 3 months ago
parent 1e8619de40
commit 3947ee4e3c

@ -74,9 +74,15 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do
defp assign_form(%{assigns: %{author: author}} = socket) do defp assign_form(%{assigns: %{author: author}} = socket) do
form = form =
if author do if author do
AshPhoenix.Form.for_update(author, :update, as: "author") AshPhoenix.Form.for_update(author, :update,
as: "author",
actor: socket.assigns.current_user
)
else else
AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.Author, :create, as: "author") AshPhoenix.Form.for_create(DecentralisedBookIndex.Metadata.Author, :create,
as: "author",
actor: socket.assigns.current_user
)
end end
assign(socket, form: to_form(form)) assign(socket, form: to_form(form))

@ -54,6 +54,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Index do
module={DecentralisedBookIndexWeb.AuthorLive.FormComponent} module={DecentralisedBookIndexWeb.AuthorLive.FormComponent}
id={(@author && @author.id) || :new} id={(@author && @author.id) || :new}
title={@page_title} title={@page_title}
current_user={@current_user}
action={@live_action} action={@live_action}
author={@author} author={@author}
patch={~p"/authors"} patch={~p"/authors"}
@ -64,7 +65,13 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Index do
@impl true @impl true
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
{:ok, stream(socket, :authors, Ash.read!(DecentralisedBookIndex.Metadata.Author))} {:ok,
socket
|> stream(
:authors,
Ash.read!(DecentralisedBookIndex.Metadata.Author, actor: socket.assigns[:current_user])
)
|> assign_new(:current_user, fn -> nil end)}
end end
@impl true @impl true
@ -75,7 +82,10 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Index do
defp apply_action(socket, :edit, %{"id" => id}) do defp apply_action(socket, :edit, %{"id" => id}) do
socket socket
|> assign(:page_title, "Edit Author") |> assign(:page_title, "Edit Author")
|> assign(:author, Ash.get!(DecentralisedBookIndex.Metadata.Author, id)) |> assign(
:author,
Ash.get!(DecentralisedBookIndex.Metadata.Author, id, actor: socket.assigns.current_user)
)
end end
defp apply_action(socket, :new, _params) do defp apply_action(socket, :new, _params) do
@ -97,8 +107,10 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Index do
@impl true @impl true
def handle_event("delete", %{"id" => id}, socket) do def handle_event("delete", %{"id" => id}, socket) do
author = Ash.get!(DecentralisedBookIndex.Metadata.Author, id) author =
Ash.destroy!(author) Ash.get!(DecentralisedBookIndex.Metadata.Author, id, actor: socket.assigns.current_user)
Ash.destroy!(author, actor: socket.assigns.current_user)
{:noreply, stream_delete(socket, :authors, author)} {:noreply, stream_delete(socket, :authors, author)}
end end

@ -38,6 +38,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do
id={@author.id} id={@author.id}
title={@page_title} title={@page_title}
action={@live_action} action={@live_action}
current_user={@current_user}
author={@author} author={@author}
patch={~p"/authors/#{@author}"} patch={~p"/authors/#{@author}"}
/> />
@ -55,7 +56,10 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do
{:noreply, {:noreply,
socket socket
|> assign(:page_title, page_title(socket.assigns.live_action)) |> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:author, Ash.get!(DecentralisedBookIndex.Metadata.Author, id))} |> assign(
:author,
Ash.get!(DecentralisedBookIndex.Metadata.Author, id, actor: socket.assigns.current_user)
)}
end end
defp page_title(:show), do: "Show Author" defp page_title(:show), do: "Show Author"

Loading…
Cancel
Save