Add Articles

mix phx.gen.live Articles Article articles title:string\                                                            /Crucial/git/phoenix-liveview-book
  content:text url:string language:string\
  date:utc_datetime author_id:references:authors
This commit is contained in:
Thelonius Kort
2022-12-26 18:02:29 +01:00
parent 005a9d9337
commit f7f1e1a284
13 changed files with 582 additions and 0 deletions

View File

@ -0,0 +1,85 @@
defmodule OutlookWeb.ArticleLive.FormComponent do
use OutlookWeb, :live_component
alias Outlook.Articles
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage article records in your database.</:subtitle>
</.header>
<.simple_form
:let={f}
for={@changeset}
id="article-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<.input field={{f, :title}} type="text" label="title" />
<.input field={{f, :content}} type="text" label="content" />
<.input field={{f, :url}} type="text" label="url" />
<.input field={{f, :language}} type="text" label="language" />
<.input field={{f, :date}} type="datetime-local" label="date" />
<:actions>
<.button phx-disable-with="Saving...">Save Article</.button>
</:actions>
</.simple_form>
</div>
"""
end
@impl true
def update(%{article: article} = assigns, socket) do
changeset = Articles.change_article(article)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"article" => article_params}, socket) do
changeset =
socket.assigns.article
|> Articles.change_article(article_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"article" => article_params}, socket) do
save_article(socket, socket.assigns.action, article_params)
end
defp save_article(socket, :edit, article_params) do
case Articles.update_article(socket.assigns.article, article_params) do
{:ok, _article} ->
{:noreply,
socket
|> put_flash(:info, "Article updated successfully")
|> push_navigate(to: socket.assigns.navigate)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_article(socket, :new, article_params) do
case Articles.create_article(article_params) do
{:ok, _article} ->
{:noreply,
socket
|> put_flash(:info, "Article created successfully")
|> push_navigate(to: socket.assigns.navigate)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end

View File

@ -0,0 +1,46 @@
defmodule OutlookWeb.ArticleLive.Index do
use OutlookWeb, :live_view
alias Outlook.Articles
alias Outlook.Articles.Article
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :articles, list_articles())}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Article")
|> assign(:article, Articles.get_article!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Article")
|> assign(:article, %Article{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Articles")
|> assign(:article, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
article = Articles.get_article!(id)
{:ok, _} = Articles.delete_article(article)
{:noreply, assign(socket, :articles, list_articles())}
end
defp list_articles do
Articles.list_articles()
end
end

View File

@ -0,0 +1,43 @@
<.header>
Listing Articles
<:actions>
<.link patch={~p"/articles/new"}>
<.button>New Article</.button>
</.link>
</:actions>
</.header>
<.table id="articles" rows={@articles} row_click={&JS.navigate(~p"/articles/#{&1}")}>
<:col :let={article} label="Title"><%= article.title %></:col>
<:col :let={article} label="Content"><%= article.content %></:col>
<:col :let={article} label="Url"><%= article.url %></:col>
<:col :let={article} label="Language"><%= article.language %></:col>
<:col :let={article} label="Date"><%= article.date %></:col>
<:action :let={article}>
<div class="sr-only">
<.link navigate={~p"/articles/#{article}"}>Show</.link>
</div>
<.link patch={~p"/articles/#{article}/edit"}>Edit</.link>
</:action>
<:action :let={article}>
<.link phx-click={JS.push("delete", value: %{id: article.id})} data-confirm="Are you sure?">
Delete
</.link>
</:action>
</.table>
<.modal
:if={@live_action in [:new, :edit]}
id="article-modal"
show
on_cancel={JS.navigate(~p"/articles")}
>
<.live_component
module={OutlookWeb.ArticleLive.FormComponent}
id={@article.id || :new}
title={@page_title}
action={@live_action}
article={@article}
navigate={~p"/articles"}
/>
</.modal>

View File

@ -0,0 +1,21 @@
defmodule OutlookWeb.ArticleLive.Show do
use OutlookWeb, :live_view
alias Outlook.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(:article, Articles.get_article!(id))}
end
defp page_title(:show), do: "Show Article"
defp page_title(:edit), do: "Edit Article"
end

View File

@ -0,0 +1,30 @@
<.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>
</.link>
</:actions>
</.header>
<.list>
<:item title="Title"><%= @article.title %></:item>
<:item title="Content"><%= @article.content %></:item>
<:item title="Url"><%= @article.url %></:item>
<:item title="Language"><%= @article.language %></:item>
<:item title="Date"><%= @article.date %></:item>
</.list>
<.back navigate={~p"/articles"}>Back to articles</.back>
<.modal :if={@live_action == :edit} id="article-modal" show on_cancel={JS.patch(~p"/articles/#{@article}")}>
<.live_component
module={OutlookWeb.ArticleLive.FormComponent}
id={@article.id}
title={@page_title}
action={@live_action}
article={@article}
navigate={~p"/articles/#{@article}"}
/>
</.modal>