defmodule OutlookWeb.AuthorLive.Show do
  use OutlookWeb, :live_view

  alias Outlook.{Authors,Articles}

  @impl true
  def mount(_params, _session, socket) do
    {:ok, socket}
  end

  @impl true
  def handle_params(%{"id" => id}, _, socket) do
    {:noreply,
     socket
     |> assign(:page_title, page_title(socket.assigns.live_action))
     |> assign(:author, Authors.get_author_with_articles!(id))}
  end

  @impl true
  def handle_event("delete_article", %{"id" => id}, socket) do
    article = Articles.get_article!(id)
    {:ok, _} = Articles.delete_article(article)

    {:noreply, socket
      |> assign(:author, Authors.get_author_with_articles!(socket.assigns.author.id))}
  end

  defp page_title(:show), do: "Show Author"
  defp page_title(:edit), do: "Edit Author"
end
