initial book review schema
This commit is contained in:
parent
e218290e08
commit
90414e6b93
15 changed files with 653 additions and 0 deletions
86
lib/wild_web/live/book_read_live/form_component.ex
Normal file
86
lib/wild_web/live/book_read_live/form_component.ex
Normal file
|
@ -0,0 +1,86 @@
|
|||
defmodule WildWeb.BookReadLive.FormComponent do
|
||||
use WildWeb, :live_component
|
||||
|
||||
alias Wild.BookReads
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div>
|
||||
<.header>
|
||||
{@title}
|
||||
<:subtitle>Use this form to manage book_read records in your database.</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.simple_form
|
||||
for={@form}
|
||||
id="book_read-form"
|
||||
phx-target={@myself}
|
||||
phx-change="validate"
|
||||
phx-submit="save"
|
||||
>
|
||||
<.input field={@form[:rating]} type="number" label="Rating" />
|
||||
<.input field={@form[:thoughts]} type="text" label="Thoughts" />
|
||||
<.input field={@form[:read_start]} type="datetime-local" label="Read start" />
|
||||
<.input field={@form[:read_finish]} type="datetime-local" label="Read finish" />
|
||||
<.input field={@form[:progress]} type="number" label="Progress" />
|
||||
<:actions>
|
||||
<.button phx-disable-with="Saving...">Save Book read</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def update(%{book_read: book_read} = assigns, socket) do
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign_new(:form, fn ->
|
||||
to_form(BookReads.change_book_read(book_read))
|
||||
end)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"book_read" => book_read_params}, socket) do
|
||||
changeset = BookReads.change_book_read(socket.assigns.book_read, book_read_params)
|
||||
{:noreply, assign(socket, form: to_form(changeset, action: :validate))}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"book_read" => book_read_params}, socket) do
|
||||
save_book_read(socket, socket.assigns.action, book_read_params)
|
||||
end
|
||||
|
||||
defp save_book_read(socket, :edit, book_read_params) do
|
||||
case BookReads.update_book_read(socket.assigns.book_read, book_read_params) do
|
||||
{:ok, book_read} ->
|
||||
notify_parent({:saved, book_read})
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Book read updated successfully")
|
||||
|> push_patch(to: socket.assigns.patch)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, form: to_form(changeset))}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_book_read(socket, :new, book_read_params) do
|
||||
case BookReads.create_book_read(book_read_params) do
|
||||
{:ok, book_read} ->
|
||||
notify_parent({:saved, book_read})
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Book read created successfully")
|
||||
|> push_patch(to: socket.assigns.patch)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, form: to_form(changeset))}
|
||||
end
|
||||
end
|
||||
|
||||
defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
|
||||
end
|
47
lib/wild_web/live/book_read_live/index.ex
Normal file
47
lib/wild_web/live/book_read_live/index.ex
Normal file
|
@ -0,0 +1,47 @@
|
|||
defmodule WildWeb.BookReadLive.Index do
|
||||
use WildWeb, :live_view
|
||||
|
||||
alias Wild.BookReads
|
||||
alias Wild.BookReads.BookRead
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, stream(socket, :book_reads, BookReads.list_book_reads())}
|
||||
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 Book read")
|
||||
|> assign(:book_read, BookReads.get_book_read!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Book read")
|
||||
|> assign(:book_read, %BookRead{})
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Listing Book reads")
|
||||
|> assign(:book_read, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({WildWeb.BookReadLive.FormComponent, {:saved, book_read}}, socket) do
|
||||
{:noreply, stream_insert(socket, :book_reads, book_read)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
book_read = BookReads.get_book_read!(id)
|
||||
{:ok, _} = BookReads.delete_book_read(book_read)
|
||||
|
||||
{:noreply, stream_delete(socket, :book_reads, book_read)}
|
||||
end
|
||||
end
|
45
lib/wild_web/live/book_read_live/index.html.heex
Normal file
45
lib/wild_web/live/book_read_live/index.html.heex
Normal file
|
@ -0,0 +1,45 @@
|
|||
<.header>
|
||||
Listing Book reads
|
||||
<:actions>
|
||||
<.link patch={~p"/book_reads/new"}>
|
||||
<.button>New Book read</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.table
|
||||
id="book_reads"
|
||||
rows={@streams.book_reads}
|
||||
row_click={fn {_id, book_read} -> JS.navigate(~p"/book_reads/#{book_read}") end}
|
||||
>
|
||||
<:col :let={{_id, book_read}} label="Rating">{book_read.rating}</:col>
|
||||
<:col :let={{_id, book_read}} label="Thoughts">{book_read.thoughts}</:col>
|
||||
<:col :let={{_id, book_read}} label="Read start">{book_read.read_start}</:col>
|
||||
<:col :let={{_id, book_read}} label="Read finish">{book_read.read_finish}</:col>
|
||||
<:col :let={{_id, book_read}} label="Progress">{book_read.progress}</:col>
|
||||
<:action :let={{_id, book_read}}>
|
||||
<div class="sr-only">
|
||||
<.link navigate={~p"/book_reads/#{book_read}"}>Show</.link>
|
||||
</div>
|
||||
<.link patch={~p"/book_reads/#{book_read}/edit"}>Edit</.link>
|
||||
</:action>
|
||||
<:action :let={{id, book_read}}>
|
||||
<.link
|
||||
phx-click={JS.push("delete", value: %{id: book_read.id}) |> hide("##{id}")}
|
||||
data-confirm="Are you sure?"
|
||||
>
|
||||
Delete
|
||||
</.link>
|
||||
</:action>
|
||||
</.table>
|
||||
|
||||
<.modal :if={@live_action in [:new, :edit]} id="book_read-modal" show on_cancel={JS.patch(~p"/book_reads")}>
|
||||
<.live_component
|
||||
module={WildWeb.BookReadLive.FormComponent}
|
||||
id={@book_read.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
book_read={@book_read}
|
||||
patch={~p"/book_reads"}
|
||||
/>
|
||||
</.modal>
|
21
lib/wild_web/live/book_read_live/show.ex
Normal file
21
lib/wild_web/live/book_read_live/show.ex
Normal file
|
@ -0,0 +1,21 @@
|
|||
defmodule WildWeb.BookReadLive.Show do
|
||||
use WildWeb, :live_view
|
||||
|
||||
alias Wild.BookReads
|
||||
|
||||
@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(:book_read, BookReads.get_book_read!(id))}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Book read"
|
||||
defp page_title(:edit), do: "Edit Book read"
|
||||
end
|
30
lib/wild_web/live/book_read_live/show.html.heex
Normal file
30
lib/wild_web/live/book_read_live/show.html.heex
Normal file
|
@ -0,0 +1,30 @@
|
|||
<.header>
|
||||
Book read {@book_read.id}
|
||||
<:subtitle>This is a book_read record from your database.</:subtitle>
|
||||
<:actions>
|
||||
<.link patch={~p"/book_reads/#{@book_read}/show/edit"} phx-click={JS.push_focus()}>
|
||||
<.button>Edit book_read</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.list>
|
||||
<:item title="Rating">{@book_read.rating}</:item>
|
||||
<:item title="Thoughts">{@book_read.thoughts}</:item>
|
||||
<:item title="Read start">{@book_read.read_start}</:item>
|
||||
<:item title="Read finish">{@book_read.read_finish}</:item>
|
||||
<:item title="Progress">{@book_read.progress}</:item>
|
||||
</.list>
|
||||
|
||||
<.back navigate={~p"/book_reads"}>Back to book_reads</.back>
|
||||
|
||||
<.modal :if={@live_action == :edit} id="book_read-modal" show on_cancel={JS.patch(~p"/book_reads/#{@book_read}")}>
|
||||
<.live_component
|
||||
module={WildWeb.BookReadLive.FormComponent}
|
||||
id={@book_read.id}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
book_read={@book_read}
|
||||
patch={~p"/book_reads/#{@book_read}"}
|
||||
/>
|
||||
</.modal>
|
Loading…
Add table
Add a link
Reference in a new issue