implement notes

This commit is contained in:
2022-11-17 22:38:52 -05:00
parent 0aa7df0b37
commit 1a508a42ef
21 changed files with 1096 additions and 574 deletions

View File

@ -4,8 +4,8 @@ defmodule MemexWeb.NoteLive.FormComponent do
alias Memex.Notes
@impl true
def update(%{note: note} = assigns, socket) do
changeset = Notes.change_note(note)
def update(%{note: note, current_user: current_user} = assigns, socket) do
changeset = Notes.change_note(note, current_user)
{:ok,
socket
@ -14,39 +14,51 @@ defmodule MemexWeb.NoteLive.FormComponent do
end
@impl true
def handle_event("validate", %{"note" => note_params}, socket) do
def handle_event(
"validate",
%{"note" => note_params},
%{assigns: %{note: note, current_user: current_user}} = socket
) do
changeset =
socket.assigns.note
|> Notes.change_note(note_params)
note
|> Notes.change_note(note_params, current_user)
|> 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)
def handle_event("save", %{"note" => note_params}, %{assigns: %{action: action}} = socket) do
save_note(socket, action, note_params)
end
defp save_note(socket, :edit, note_params) do
case Notes.update_note(socket.assigns.note, note_params) do
{:ok, _note} ->
defp save_note(
%{assigns: %{note: note, return_to: return_to, current_user: current_user}} = socket,
:edit,
note_params
) do
case Notes.update_note(note, note_params, current_user) do
{:ok, %{title: title}} ->
{:noreply,
socket
|> put_flash(:info, gettext("%{title} saved", title: title))
|> push_navigate(to: socket.assigns.return_to)}
|> push_navigate(to: 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} ->
defp save_note(
%{assigns: %{return_to: return_to, current_user: current_user}} = socket,
:new,
note_params
) do
case Notes.create_note(note_params, current_user) do
{:ok, %{title: title}} ->
{:noreply,
socket
|> put_flash(:info, gettext("%{title} created", title: title))
|> push_navigate(to: socket.assigns.return_to)}
|> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)}

View File

@ -1,6 +1,4 @@
<div>
<h2><%= @title %></h2>
<div class="h-full flex flex-col justify-start items-stretch space-y-4">
<.form
:let={f}
for={@changeset}
@ -8,27 +6,44 @@
phx-target={@myself}
phx-change="validate"
phx-submit="save"
phx-debounce="300"
class="flex flex-col justify-start items-stretch space-y-4"
>
<%= label(f, :title) %>
<%= text_input(f, :title) %>
<%= text_input(f, :title,
class: "input input-primary",
placeholder: gettext("title")
) %>
<%= error_tag(f, :title) %>
<%= label(f, :content) %>
<%= textarea(f, :content) %>
<%= textarea(f, :content,
id: "note-form-content",
class: "input input-primary h-64 min-h-64",
phx_hook: "MaintainAttrs",
phx_update: "ignore",
placeholder: gettext("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"
<%= text_input(f, :tags_string,
id: "tags-input",
class: "input input-primary",
placeholder: gettext("tag1,tag2"),
phx_update: "ignore",
value: Notes.get_tags_string(@changeset)
) %>
<%= error_tag(f, :visibility) %>
<%= error_tag(f, :tags_string) %>
<div>
<%= submit("Save", phx_disable_with: "Saving...") %>
<div class="flex justify-center items-stretch space-x-4">
<%= select(f, :visibility, Ecto.Enum.values(Memex.Notes.Note, :visibility),
class: "grow input input-primary",
prompt: gettext("select privacy")
) %>
<%= submit(dgettext("actions", "save"),
phx_disable_with: gettext("saving..."),
class: "mx-auto btn btn-primary"
) %>
</div>
<%= error_tag(f, :visibility) %>
</.form>
</div>

View File

@ -1,45 +1,52 @@
defmodule MemexWeb.NoteLive.Index do
use MemexWeb, :live_view
alias Memex.Notes
alias Memex.Notes.Note
alias Memex.{Notes, Notes.Note}
@impl true
def mount(_params, _session, %{assigns: %{current_user: current_user}} = socket)
when not (current_user |> is_nil()) do
{:ok, socket |> assign(notes: Notes.list_notes(current_user))}
end
def mount(_params, _session, socket) do
{:ok, assign(socket, :notes, list_notes())}
{:ok, socket |> assign(notes: Notes.list_public_notes())}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, apply_action(socket, live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
%{title: title} = note = Notes.get_note!(id, current_user)
socket
|> assign(page_title: gettext("edit %{title}", title: title))
|> assign(:note, Notes.get_note!(id))
|> assign(note: note)
end
defp apply_action(socket, :new, _params) do
defp apply_action(%{assigns: %{current_user: %{id: current_user_id}}} = socket, :new, _params) do
socket
|> assign(page_title: "new note")
|> assign(:note, %Note{})
|> assign(note: %Note{user_id: current_user_id})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(page_title: "notes")
|> assign(:note, nil)
|> assign(note: nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
note = Notes.get_note!(id)
{:ok, _} = Notes.delete_note(note)
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
%{title: title} = note = Notes.get_note!(id, current_user)
{:ok, _} = Notes.delete_note(note, current_user)
socket =
socket
|> assign(notes: Notes.list_notes(current_user))
|> put_flash(:info, gettext("%{title} deleted", title: title))
defp list_notes do
Notes.list_notes()
{:noreply, socket}
end
end

View File

@ -1,10 +1,54 @@
<h1>Listing Notes</h1>
<div class="mx-auto flex flex-col justify-center items-start space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= gettext("notes") %>
</h1>
<%= if @notes |> Enum.empty?() do %>
<h1 class="self-center text-primary-500">
<%= gettext("no notes found") %>
</h1>
<% else %>
<.live_component
module={MemexWeb.Components.NotesTableComponent}
id="notes-index-table"
current_user={@current_user}
notes={@notes}
>
<:actions :let={note}>
<%= if @current_user do %>
<.link
patch={Routes.note_index_path(@socket, :edit, note)}
data-qa={"note-edit-#{note.id}"}
>
<%= dgettext("actions", "edit") %>
</.link>
<.link
href="#"
phx-click="delete"
phx-value-id={note.id}
data-confirm={dgettext("prompts", "are you sure?")}
data-qa={"delete-note-#{note.id}"}
>
<%= dgettext("actions", "delete") %>
</.link>
<% end %>
</:actions>
</.live_component>
<% end %>
<%= if @current_user do %>
<.link patch={Routes.note_index_path(@socket, :new)} class="self-end btn btn-primary">
<%= dgettext("actions", "new note") %>
</.link>
<% end %>
</div>
<%= 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}
current_user={@current_user}
title={@page_title}
action={@live_action}
note={@note}
@ -12,55 +56,3 @@
/>
</.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>
<.link navigate={Routes.note_show_path(@socket, :show, note)}>
<%= dgettext("actions", "Show") %>
</.link>
</span>
<span>
<.link patch={Routes.note_index_path(@socket, :edit, note)}>
<%= dgettext("actions", "Edit") %>
</.link>
</span>
<span>
<.link
href="#"
phx-click="delete"
phx-value-id={note.id}
data-confirm={dgettext("prompts", "Are you sure?")}
>
<%= dgettext("actions", "Delete") %>
</.link>
</span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span>
<.link patch={Routes.note_index_path(@socket, :new)}>
<%= dgettext("actions", "New Note") %>
</.link>
</span>

View File

@ -9,11 +9,15 @@ defmodule MemexWeb.NoteLive.Show do
end
@impl true
def handle_params(%{"id" => id}, _, socket) do
def handle_params(
%{"id" => id},
_,
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
) do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:note, Notes.get_note!(id))}
|> assign(:page_title, page_title(live_action))
|> assign(:note, Notes.get_note!(id, current_user))}
end
defp page_title(:show), do: "show note"

View File

@ -1,10 +1,41 @@
<h1>Show Note</h1>
<div class="mx-auto flex flex-col justify-center items-stretch space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= @note.title %>
</h1>
<p><%= if @note.tags, do: @note.tags |> Enum.join(", ") %></p>
<textarea
id="show-note-content"
class="input input-primary h-128 min-h-128"
phx-hook="MaintainAttrs"
phx-update="ignore"
readonly
phx-no-format
><%= @note.content %></textarea>
<p class="self-end">
<%= gettext("Visibility: %{visibility}", visibility: @note.visibility) %>
</p>
<div class="self-end flex space-x-4">
<.link class="btn btn-primary" patch={Routes.note_index_path(@socket, :index)}>
<%= dgettext("actions", "Back") %>
</.link>
<%= if @current_user do %>
<.link class="btn btn-primary" patch={Routes.note_show_path(@socket, :edit, @note)}>
<%= dgettext("actions", "edit") %>
</.link>
<% end %>
</div>
</div>
<%= 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}
current_user={@current_user}
title={@page_title}
action={@live_action}
note={@note}
@ -12,37 +43,3 @@
/>
</.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>
<.link patch={Routes.note_show_path(@socket, :edit, @note)} class="button">
<%= dgettext("actions", "Edit") %>
</.link>
</span>
|
<span>
<.link patch={Routes.note_index_path(@socket, :index)}>
<%= dgettext("actions", "Back") %>
</.link>
</span>