add 記事の編集,削除
This commit is contained in:
parent
1f3124f3f8
commit
bf15423a0f
3 changed files with 30 additions and 10 deletions
|
|
@ -10,10 +10,19 @@ defmodule PhoenixRealWorldWeb.ArticleLive.Show do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_params(%{"id" => id}, _, socket) do
|
def handle_params(%{"id" => id}, _, socket) do
|
||||||
{:noreply,
|
#↓追加&変更
|
||||||
socket
|
article = Blogs.get_article!(id)
|
||||||
|> assign(:page_title, page_title(socket.assigns.live_action))
|
%{live_action: action, current_user: user} = socket.assigns
|
||||||
|> assign(:article, Blogs.get_article!(id))}
|
# 編集モードで、ログインユーザーが記事の作者でない場合は、記事一覧にリダイレクト
|
||||||
|
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
|
end
|
||||||
|
|
||||||
defp page_title(:show), do: "Show Article"
|
defp page_title(:show), do: "Show Article"
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,16 @@
|
||||||
<.header>
|
<.header>
|
||||||
Article {@article.id}
|
|
||||||
<:subtitle>This is a article record from your database.</:subtitle>
|
<:subtitle>This is a article record from your database.</:subtitle>
|
||||||
<:actions>
|
<!--変更 ログインしているかどうか&ログインユーザーと記事の作者が同じかどうか-->
|
||||||
<.link patch={~p"/articles/#{@article}/show/edit"} phx-click={JS.push_focus()}>
|
<:actions :if={@current_user && @current_user.id == @article.author.id}>
|
||||||
<.button>Edit article</.button>
|
<.link patch={~p"/articles/#{@article}/edit"} phx-click={JS.focus()}>
|
||||||
|
<button>Edit article</button>
|
||||||
</.link>
|
</.link>
|
||||||
|
<.link phx-click={JS.push("delete")} data-confirm="Are you sure?">
|
||||||
|
<button>Delete article</button>
|
||||||
|
</.link>
|
||||||
|
</:actions>
|
||||||
|
</.header>
|
||||||
|
|
||||||
</:actions>
|
</:actions>
|
||||||
</.header>
|
</.header>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ defmodule PhoenixRealWorldWeb.ArticleLiveTest do
|
||||||
#↓追加
|
#↓追加
|
||||||
{:ok, index_live, _html} =
|
{:ok, index_live, _html} =
|
||||||
conn
|
conn
|
||||||
|> log_in_user(user_fixture())
|
|> log_in_user(user_fixture()) # 第2引数のユーザーでログインした状態にする
|
||||||
|> live(~p"/articles")
|
|> live(~p"/articles")
|
||||||
|
|
||||||
assert index_live |> element("a", "New Article") |> render_click() =~
|
assert index_live |> element("a", "New Article") |> render_click() =~
|
||||||
|
|
@ -93,7 +93,12 @@ defmodule PhoenixRealWorldWeb.ArticleLiveTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test "updates article within modal", %{conn: conn, article: article} do
|
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() =~
|
assert show_live |> element("a", "Edit") |> render_click() =~
|
||||||
"Edit Article"
|
"Edit Article"
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue