add 記事の編集,削除
This commit is contained in:
parent
1f3124f3f8
commit
bf15423a0f
3 changed files with 30 additions and 10 deletions
|
|
@ -10,11 +10,20 @@ defmodule PhoenixRealWorldWeb.ArticleLive.Show do
|
|||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
#↓追加&変更
|
||||
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"
|
||||
defp page_title(:edit), do: "Edit Article"
|
||||
|
|
|
|||
|
|
@ -1,10 +1,16 @@
|
|||
<.header>
|
||||
Article {@article.id}
|
||||
<:subtitle>This is a article record from your database.</:subtitle>
|
||||
<:actions>
|
||||
<.link patch={~p"/articles/#{@article}/show/edit"} phx-click={JS.push_focus()}>
|
||||
<.button>Edit article</.button>
|
||||
<!--変更 ログインしているかどうか&ログインユーザーと記事の作者が同じかどうか-->
|
||||
<:actions :if={@current_user && @current_user.id == @article.author.id}>
|
||||
<.link patch={~p"/articles/#{@article}/edit"} phx-click={JS.focus()}>
|
||||
<button>Edit article</button>
|
||||
</.link>
|
||||
<.link phx-click={JS.push("delete")} data-confirm="Are you sure?">
|
||||
<button>Delete article</button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue