From 3947ee4e3c1dd429aa6f537637a59f097f1ddd86 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 16 Mar 2025 09:42:18 +0200 Subject: [PATCH] Add the currect user as an actor for Author's LiveViews. --- .../live/author_live/form_component.ex | 10 ++++++++-- .../live/author_live/index.ex | 20 +++++++++++++++---- .../live/author_live/show.ex | 6 +++++- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/decentralised_book_index_web/live/author_live/form_component.ex b/lib/decentralised_book_index_web/live/author_live/form_component.ex index 4f14031..6178c91 100644 --- a/lib/decentralised_book_index_web/live/author_live/form_component.ex +++ b/lib/decentralised_book_index_web/live/author_live/form_component.ex @@ -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)) diff --git a/lib/decentralised_book_index_web/live/author_live/index.ex b/lib/decentralised_book_index_web/live/author_live/index.ex index d4c0684..9e13420 100644 --- a/lib/decentralised_book_index_web/live/author_live/index.ex +++ b/lib/decentralised_book_index_web/live/author_live/index.ex @@ -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 diff --git a/lib/decentralised_book_index_web/live/author_live/show.ex b/lib/decentralised_book_index_web/live/author_live/show.ex index 8d0d809..42a0ee1 100644 --- a/lib/decentralised_book_index_web/live/author_live/show.ex +++ b/lib/decentralised_book_index_web/live/author_live/show.ex @@ -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"