add notes

This commit is contained in:
2022-07-25 20:08:40 -04:00
parent 1a423f703b
commit 63fca7ff6a
14 changed files with 607 additions and 12 deletions

View File

@ -0,0 +1,55 @@
defmodule MemexWeb.NoteLive.FormComponent do
use MemexWeb, :live_component
alias Memex.Notes
@impl true
def update(%{note: note} = assigns, socket) do
changeset = Notes.change_note(note)
{:ok,
socket
|> assign(assigns)
|> assign(:changeset, changeset)}
end
@impl true
def handle_event("validate", %{"note" => note_params}, socket) do
changeset =
socket.assigns.note
|> Notes.change_note(note_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
def handle_event("save", %{"note" => note_params}, socket) do
save_note(socket, socket.assigns.action, note_params)
end
defp save_note(socket, :edit, note_params) do
case Notes.update_note(socket.assigns.note, note_params) do
{:ok, _note} ->
{:noreply,
socket
|> put_flash(:info, "Note updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
defp save_note(socket, :new, note_params) do
case Notes.create_note(note_params) do
{:ok, _note} ->
{:noreply,
socket
|> put_flash(:info, "Note created successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}
end
end
end

View File

@ -0,0 +1,32 @@
<div>
<h2><%= @title %></h2>
<.form
let={f}
for={@changeset}
id="note-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save">
<%= label f, :title %>
<%= text_input f, :title %>
<%= error_tag f, :title %>
<%= label f, :content %>
<%= textarea f, :content %>
<%= error_tag f, :content %>
<%= label f, :tag %>
<%= multiple_select f, :tag, ["Option 1": "option1", "Option 2": "option2"] %>
<%= error_tag f, :tag %>
<%= label f, :visibility %>
<%= select f, :visibility, Ecto.Enum.values(Memex.Notes.Note, :visibility), prompt: "Choose a value" %>
<%= error_tag f, :visibility %>
<div>
<%= submit "Save", phx_disable_with: "Saving..." %>
</div>
</.form>
</div>

View File

@ -0,0 +1,46 @@
defmodule MemexWeb.NoteLive.Index do
use MemexWeb, :live_view
alias Memex.Notes
alias Memex.Notes.Note
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :notes, list_notes())}
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 Note")
|> assign(:note, Notes.get_note!(id))
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Note")
|> assign(:note, %Note{})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Notes")
|> assign(:note, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
note = Notes.get_note!(id)
{:ok, _} = Notes.delete_note(note)
{:noreply, assign(socket, :notes, list_notes())}
end
defp list_notes do
Notes.list_notes()
end
end

View File

@ -0,0 +1,45 @@
<h1>Listing Notes</h1>
<%= if @live_action in [:new, :edit] do %>
<.modal return_to={Routes.note_index_path(@socket, :index)}>
<.live_component
module={MemexWeb.NoteLive.FormComponent}
id={@note.id || :new}
title={@page_title}
action={@live_action}
note={@note}
return_to={Routes.note_index_path(@socket, :index)}
/>
</.modal>
<% end %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th>Tag</th>
<th>Visibility</th>
<th></th>
</tr>
</thead>
<tbody id="notes">
<%= for note <- @notes do %>
<tr id={"note-#{note.id}"}>
<td><%= note.title %></td>
<td><%= note.content %></td>
<td><%= note.tag %></td>
<td><%= note.visibility %></td>
<td>
<span><%= live_redirect "Show", to: Routes.note_show_path(@socket, :show, note) %></span>
<span><%= live_patch "Edit", to: Routes.note_index_path(@socket, :edit, note) %></span>
<span><%= link "Delete", to: "#", phx_click: "delete", phx_value_id: note.id, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= live_patch "New Note", to: Routes.note_index_path(@socket, :new) %></span>

View File

@ -0,0 +1,21 @@
defmodule MemexWeb.NoteLive.Show do
use MemexWeb, :live_view
alias Memex.Notes
@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(:note, Notes.get_note!(id))}
end
defp page_title(:show), do: "Show Note"
defp page_title(:edit), do: "Edit Note"
end

View File

@ -0,0 +1,41 @@
<h1>Show Note</h1>
<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.note_show_path(@socket, :show, @note)}>
<.live_component
module={MemexWeb.NoteLive.FormComponent}
id={@note.id}
title={@page_title}
action={@live_action}
note={@note}
return_to={Routes.note_show_path(@socket, :show, @note)}
/>
</.modal>
<% end %>
<ul>
<li>
<strong>Title:</strong>
<%= @note.title %>
</li>
<li>
<strong>Content:</strong>
<%= @note.content %>
</li>
<li>
<strong>Tag:</strong>
<%= @note.tag %>
</li>
<li>
<strong>Visibility:</strong>
<%= @note.visibility %>
</li>
</ul>
<span><%= live_patch "Edit", to: Routes.note_show_path(@socket, :edit, @note), class: "button" %></span> |
<span><%= live_redirect "Back", to: Routes.note_index_path(@socket, :index) %></span>