Add Authors

mix phx.gen.live Authors Author authors name:string description:text homepage_name:string homepage_url:string
This commit is contained in:
Thelonius Kort
2022-12-26 17:12:46 +01:00
parent 08481a12a8
commit 005a9d9337
12 changed files with 565 additions and 0 deletions

View File

@ -0,0 +1,84 @@
defmodule OutlookWeb.AuthorLive.FormComponent do
use OutlookWeb, :live_component
alias Outlook.Authors
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage author records in your database.</:subtitle>
</.header>
<.simple_form
:let={f}
for={@changeset}
id="author-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<.input field={{f, :name}} type="text" label="name" />
<.input field={{f, :description}} type="text" label="description" />
<.input field={{f, :homepage_name}} type="text" label="homepage_name" />
<.input field={{f, :homepage_url}} type="text" label="homepage_url" />
<:actions>
<.button phx-disable-with="Saving...">Save Author</.button>
</:actions>
</.simple_form>
</div>
"""
end
@impl true
def update(%{author: author} = assigns, socket) do
changeset = Authors.change_author(author)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"author" => author_params}, socket) do
changeset =
socket.assigns.author
|> Authors.change_author(author_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"author" => author_params}, socket) do
save_author(socket, socket.assigns.action, author_params)
end
defp save_author(socket, :edit, author_params) do
case Authors.update_author(socket.assigns.author, author_params) do
{:ok, _author} ->
{:noreply,
socket
|> put_flash(:info, "Author updated successfully")
|> push_navigate(to: socket.assigns.navigate)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_author(socket, :new, author_params) do
case Authors.create_author(author_params) do
{:ok, _author} ->
{:noreply,
socket
|> put_flash(:info, "Author 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.AuthorLive.Index do
use OutlookWeb, :live_view
alias Outlook.Authors
alias Outlook.Authors.Author
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :authors, list_authors())}
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 Author")
|> assign(:author, Authors.get_author!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Author")
|> assign(:author, %Author{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Authors")
|> assign(:author, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
author = Authors.get_author!(id)
{:ok, _} = Authors.delete_author(author)
{:noreply, assign(socket, :authors, list_authors())}
end
defp list_authors do
Authors.list_authors()
end
end

View File

@ -0,0 +1,42 @@
<.header>
Listing Authors
<:actions>
<.link patch={~p"/authors/new"}>
<.button>New Author</.button>
</.link>
</:actions>
</.header>
<.table id="authors" rows={@authors} row_click={&JS.navigate(~p"/authors/#{&1}")}>
<:col :let={author} label="Name"><%= author.name %></:col>
<:col :let={author} label="Description"><%= author.description %></:col>
<:col :let={author} label="Homepage name"><%= author.homepage_name %></:col>
<:col :let={author} label="Homepage url"><%= author.homepage_url %></:col>
<:action :let={author}>
<div class="sr-only">
<.link navigate={~p"/authors/#{author}"}>Show</.link>
</div>
<.link patch={~p"/authors/#{author}/edit"}>Edit</.link>
</:action>
<:action :let={author}>
<.link phx-click={JS.push("delete", value: %{id: author.id})} data-confirm="Are you sure?">
Delete
</.link>
</:action>
</.table>
<.modal
:if={@live_action in [:new, :edit]}
id="author-modal"
show
on_cancel={JS.navigate(~p"/authors")}
>
<.live_component
module={OutlookWeb.AuthorLive.FormComponent}
id={@author.id || :new}
title={@page_title}
action={@live_action}
author={@author}
navigate={~p"/authors"}
/>
</.modal>

View File

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

View File

@ -0,0 +1,29 @@
<.header>
Author <%= @author.id %>
<:subtitle>This is a author record from your database.</:subtitle>
<:actions>
<.link patch={~p"/authors/#{@author}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit author</.button>
</.link>
</:actions>
</.header>
<.list>
<:item title="Name"><%= @author.name %></:item>
<:item title="Description"><%= @author.description %></:item>
<:item title="Homepage name"><%= @author.homepage_name %></:item>
<:item title="Homepage url"><%= @author.homepage_url %></:item>
</.list>
<.back navigate={~p"/authors"}>Back to authors</.back>
<.modal :if={@live_action == :edit} id="author-modal" show on_cancel={JS.patch(~p"/authors/#{@author}")}>
<.live_component
module={OutlookWeb.AuthorLive.FormComponent}
id={@author.id}
title={@page_title}
action={@live_action}
author={@author}
navigate={~p"/authors/#{@author}"}
/>
</.modal>