From bf15423a0fc5bc9848afc6b21a7360c4ed96c60c Mon Sep 17 00:00:00 2001
From: ReyoKatsu <38076269+Honekatsu@users.noreply.github.com>
Date: Sat, 19 Apr 2025 18:54:30 +0900
Subject: [PATCH] =?UTF-8?q?add=20=E8=A8=98=E4=BA=8B=E3=81=AE=E7=B7=A8?=
=?UTF-8?q?=E9=9B=86=EF=BC=8C=E5=89=8A=E9=99=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../live/article_live/show.ex | 17 +++++++++++++----
.../live/article_live/show.html.heex | 14 ++++++++++----
.../live/article_live_test.exs | 9 +++++++--
3 files changed, 30 insertions(+), 10 deletions(-)
diff --git a/lib/phoenixRealWorld_web/live/article_live/show.ex b/lib/phoenixRealWorld_web/live/article_live/show.ex
index 0305869..2d73106 100644
--- a/lib/phoenixRealWorld_web/live/article_live/show.ex
+++ b/lib/phoenixRealWorld_web/live/article_live/show.ex
@@ -10,10 +10,19 @@ defmodule PhoenixRealWorldWeb.ArticleLive.Show do
@impl true
def handle_params(%{"id" => id}, _, socket) do
- {:noreply,
- socket
- |> assign(:page_title, page_title(socket.assigns.live_action))
- |> assign(:article, Blogs.get_article!(id))}
+ #↓追加&変更
+ article = Blogs.get_article!(id)
+ %{live_action: action, current_user: user} = socket.assigns
+ # 編集モードで、ログインユーザーが記事の作者でない場合は、記事一覧にリダイレクト
+ if action = :edit && article.author.id != user.id do
+ {:noreply, push_navigate(socket, to: ~p"/articles")}
+ # 編集モードで、ログインユーザーが記事の作者である場合は、記事を表示
+ else
+ {:noreply,
+ socket
+ |> assign(:page_title, page_title(socket.assigns.live_action))
+ |> assign(:article, Blogs.get_article!(id))}
+ end
end
defp page_title(:show), do: "Show Article"
diff --git a/lib/phoenixRealWorld_web/live/article_live/show.html.heex b/lib/phoenixRealWorld_web/live/article_live/show.html.heex
index 90ec52a..4c0b8dd 100644
--- a/lib/phoenixRealWorld_web/live/article_live/show.html.heex
+++ b/lib/phoenixRealWorld_web/live/article_live/show.html.heex
@@ -1,10 +1,16 @@
<.header>
- Article {@article.id}
<:subtitle>This is a article record from your database.
- <:actions>
- <.link patch={~p"/articles/#{@article}/show/edit"} phx-click={JS.push_focus()}>
- <.button>Edit article
+
+ <:actions :if={@current_user && @current_user.id == @article.author.id}>
+ <.link patch={~p"/articles/#{@article}/edit"} phx-click={JS.focus()}>
+
+ <.link phx-click={JS.push("delete")} data-confirm="Are you sure?">
+
+
+
+
+
diff --git a/test/phoenixRealWorld_web/live/article_live_test.exs b/test/phoenixRealWorld_web/live/article_live_test.exs
index 4458021..eea39b6 100644
--- a/test/phoenixRealWorld_web/live/article_live_test.exs
+++ b/test/phoenixRealWorld_web/live/article_live_test.exs
@@ -28,7 +28,7 @@ defmodule PhoenixRealWorldWeb.ArticleLiveTest do
#↓追加
{:ok, index_live, _html} =
conn
- |> log_in_user(user_fixture())
+ |> log_in_user(user_fixture()) # 第2引数のユーザーでログインした状態にする
|> live(~p"/articles")
assert index_live |> element("a", "New Article") |> render_click() =~
@@ -93,7 +93,12 @@ defmodule PhoenixRealWorldWeb.ArticleLiveTest do
end
test "updates article within modal", %{conn: conn, article: article} do
- {:ok, show_live, _html} = live(conn, ~p"/articles/#{article}")
+ # {:ok, show_live, _html} = live(conn, ~p"/articles/#{article}")
+ #↓追加
+ {:ok, show_live, _html} =
+ conn
+ |> log_in_user(PhoenixRealWorld.Repo.preload(article, :author).author)
+ |> live(~p"/articles/#{article}")
assert show_live |> element("a", "Edit") |> render_click() =~
"Edit Article"