Add the currect user as an actor for Author's LiveViews.
This commit is contained in:
@@ -74,9 +74,15 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.FormComponent do
|
||||
defp assign_form(%{assigns: %{author: author}} = socket) do
|
||||
form =
|
||||
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
|
||||
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
|
||||
|
||||
assign(socket, form: to_form(form))
|
||||
|
||||
@@ -54,6 +54,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Index do
|
||||
module={DecentralisedBookIndexWeb.AuthorLive.FormComponent}
|
||||
id={(@author && @author.id) || :new}
|
||||
title={@page_title}
|
||||
current_user={@current_user}
|
||||
action={@live_action}
|
||||
author={@author}
|
||||
patch={~p"/authors"}
|
||||
@@ -64,7 +65,13 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Index do
|
||||
|
||||
@impl true
|
||||
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
|
||||
|
||||
@impl true
|
||||
@@ -75,7 +82,10 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Index do
|
||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||
socket
|
||||
|> 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
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
@@ -97,8 +107,10 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Index do
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
author = Ash.get!(DecentralisedBookIndex.Metadata.Author, id)
|
||||
Ash.destroy!(author)
|
||||
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)}
|
||||
end
|
||||
|
||||
@@ -38,6 +38,7 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do
|
||||
id={@author.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
current_user={@current_user}
|
||||
author={@author}
|
||||
patch={~p"/authors/#{@author}"}
|
||||
/>
|
||||
@@ -55,7 +56,10 @@ defmodule DecentralisedBookIndexWeb.AuthorLive.Show do
|
||||
{:noreply,
|
||||
socket
|
||||
|> 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
|
||||
|
||||
defp page_title(:show), do: "Show Author"
|
||||
|
||||
Reference in New Issue
Block a user