<.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) %>
-
- <%= submit("Save", phx_disable_with: "Saving...") %>
+
+ <%= 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"
+ ) %>
+ <%= error_tag(f, :visibility) %>
diff --git a/lib/memex_web/live/note_live/index.ex b/lib/memex_web/live/note_live/index.ex
index 548905e..706f804 100644
--- a/lib/memex_web/live/note_live/index.ex
+++ b/lib/memex_web/live/note_live/index.ex
@@ -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
diff --git a/lib/memex_web/live/note_live/index.html.heex b/lib/memex_web/live/note_live/index.html.heex
index 19a6eab..ceb95e2 100644
--- a/lib/memex_web/live/note_live/index.html.heex
+++ b/lib/memex_web/live/note_live/index.html.heex
@@ -1,10 +1,54 @@
-
Listing Notes
+
+
+ <%= gettext("notes") %>
+
+
+ <%= if @notes |> Enum.empty?() do %>
+
+ <%= gettext("no notes found") %>
+
+ <% 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
+ 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") %>
+
+ <% end %>
+
+
+ <% end %>
+
+ <%= if @current_user do %>
+ <.link patch={Routes.note_index_path(@socket, :new)} class="self-end btn btn-primary">
+ <%= dgettext("actions", "new note") %>
+
+ <% end %>
+
<%= 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 @@
/>
<% end %>
-
-
-
-
- Title |
- Content |
- Tag |
- Visibility |
-
- |
-
-
-
- <%= for note <- @notes do %>
-
- <%= note.title %> |
- <%= note.content %> |
- <%= note.tag %> |
- <%= note.visibility %> |
-
-
-
- <.link navigate={Routes.note_show_path(@socket, :show, note)}>
- <%= dgettext("actions", "Show") %>
-
-
-
- <.link patch={Routes.note_index_path(@socket, :edit, note)}>
- <%= dgettext("actions", "Edit") %>
-
-
-
- <.link
- href="#"
- phx-click="delete"
- phx-value-id={note.id}
- data-confirm={dgettext("prompts", "Are you sure?")}
- >
- <%= dgettext("actions", "Delete") %>
-
-
- |
-
- <% end %>
-
-
-
-
- <.link patch={Routes.note_index_path(@socket, :new)}>
- <%= dgettext("actions", "New Note") %>
-
-
diff --git a/lib/memex_web/live/note_live/show.ex b/lib/memex_web/live/note_live/show.ex
index 0854b1a..44d6f4b 100644
--- a/lib/memex_web/live/note_live/show.ex
+++ b/lib/memex_web/live/note_live/show.ex
@@ -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"
diff --git a/lib/memex_web/live/note_live/show.html.heex b/lib/memex_web/live/note_live/show.html.heex
index fc5239b..8de70cf 100644
--- a/lib/memex_web/live/note_live/show.html.heex
+++ b/lib/memex_web/live/note_live/show.html.heex
@@ -1,10 +1,41 @@
-
Show Note
+
+
+ <%= @note.title %>
+
+
+
<%= if @note.tags, do: @note.tags |> Enum.join(", ") %>
+
+
+
+
+ <%= gettext("Visibility: %{visibility}", visibility: @note.visibility) %>
+
+
+
+ <.link class="btn btn-primary" patch={Routes.note_index_path(@socket, :index)}>
+ <%= dgettext("actions", "Back") %>
+
+ <%= if @current_user do %>
+ <.link class="btn btn-primary" patch={Routes.note_show_path(@socket, :edit, @note)}>
+ <%= dgettext("actions", "edit") %>
+
+ <% end %>
+
+
<%= 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 @@
/>
<% end %>
-
-
- -
- Title:
- <%= @note.title %>
-
-
- -
- Content:
- <%= @note.content %>
-
-
- -
- Tag:
- <%= @note.tag %>
-
-
- -
- Visibility:
- <%= @note.visibility %>
-
-
-
-
- <.link patch={Routes.note_show_path(@socket, :edit, @note)} class="button">
- <%= dgettext("actions", "Edit") %>
-
-
-|
-
- <.link patch={Routes.note_index_path(@socket, :index)}>
- <%= dgettext("actions", "Back") %>
-
-
diff --git a/priv/repo/migrations/20220726000552_create_notes.exs b/priv/repo/migrations/20220726000552_create_notes.exs
index 8c6495a..1e4e0c6 100644
--- a/priv/repo/migrations/20220726000552_create_notes.exs
+++ b/priv/repo/migrations/20220726000552_create_notes.exs
@@ -6,9 +6,11 @@ defmodule Memex.Repo.Migrations.CreateNotes do
add :id, :binary_id, primary_key: true
add :title, :string
add :content, :text
- add :tag, {:array, :string}
+ add :tags, {:array, :string}
add :visibility, :string
+ add :user_id, references(:users, on_delete: :delete_all, type: :binary_id)
+
timestamps()
end
end
diff --git a/test/memex/notes_test.exs b/test/memex/notes_test.exs
index 4b9ea29..4462278 100644
--- a/test/memex/notes_test.exs
+++ b/test/memex/notes_test.exs
@@ -1,71 +1,85 @@
defmodule Memex.NotesTest do
use Memex.DataCase
+ import Memex.NotesFixtures
+ alias Memex.{Notes, Notes.Note}
- alias Memex.Notes
+ @invalid_attrs %{content: nil, tag: nil, title: nil, visibility: nil}
describe "notes" do
- alias Memex.Notes.Note
-
- import Memex.NotesFixtures
-
- @invalid_attrs %{content: nil, tag: nil, title: nil, visibility: nil}
-
- test "list_notes/0 returns all notes" do
- note = note_fixture()
- assert Notes.list_notes() == [note]
+ setup do
+ [user: user_fixture()]
end
- test "get_note!/1 returns the note with given id" do
- note = note_fixture()
- assert Notes.get_note!(note.id) == note
+ test "list_notes/1 returns all notes for a user", %{user: user} do
+ note_a = note_fixture(%{title: "a", visibility: :public}, user)
+ note_b = note_fixture(%{title: "b", visibility: :unlisted}, user)
+ note_c = note_fixture(%{title: "c", visibility: :private}, user)
+ assert Notes.list_notes(user) == [note_a, note_b, note_c]
end
- test "create_note/1 with valid data creates a note" do
- valid_attrs = %{content: "some content", tag: [], title: "some title", visibility: :public}
+ test "list_public_notes/0 returns public notes", %{user: user} do
+ public_note = note_fixture(%{visibility: :public}, user)
+ note_fixture(%{visibility: :unlisted}, user)
+ note_fixture(%{visibility: :private}, user)
+ assert Notes.list_public_notes() == [public_note]
+ end
- assert {:ok, %Note{} = note} = Notes.create_note(valid_attrs)
+ test "get_note!/1 returns the note with given id", %{user: user} do
+ note = note_fixture(user)
+ assert Notes.get_note!(note.id, user) == note
+ end
+
+ test "create_note/1 with valid data creates a note", %{user: user} do
+ valid_attrs = %{
+ "content" => "some content",
+ "tags_string" => "tag1,tag2",
+ "title" => "some title",
+ "visibility" => :public
+ }
+
+ assert {:ok, %Note{} = note} = Notes.create_note(valid_attrs, user)
assert note.content == "some content"
- assert note.tag == []
+ assert note.tags == ["tag1", "tag2"]
assert note.title == "some title"
assert note.visibility == :public
end
- test "create_note/1 with invalid data returns error changeset" do
- assert {:error, %Ecto.Changeset{}} = Notes.create_note(@invalid_attrs)
+ test "create_note/1 with invalid data returns error changeset", %{user: user} do
+ assert {:error, %Ecto.Changeset{}} = Notes.create_note(@invalid_attrs, user)
end
- test "update_note/2 with valid data updates the note" do
- note = note_fixture()
+ test "update_note/2 with valid data updates the note", %{user: user} do
+ note = note_fixture(user)
update_attrs = %{
- content: "some updated content",
- tag: [],
- title: "some updated title",
- visibility: :private
+ "content" => "some updated content",
+ "tags_string" => "tag1,tag2",
+ "title" => "some updated title",
+ "visibility" => :private
}
- assert {:ok, %Note{} = note} = Notes.update_note(note, update_attrs)
+ assert {:ok, %Note{} = note} = Notes.update_note(note, update_attrs, user)
assert note.content == "some updated content"
- assert note.tag == []
+ assert note.tags == ["tag1", "tag2"]
assert note.title == "some updated title"
assert note.visibility == :private
end
- test "update_note/2 with invalid data returns error changeset" do
- note = note_fixture()
- assert {:error, %Ecto.Changeset{}} = Notes.update_note(note, @invalid_attrs)
- assert note == Notes.get_note!(note.id)
+ test "update_note/2 with invalid data returns error changeset", %{user: user} do
+ note = note_fixture(user)
+ assert {:error, %Ecto.Changeset{}} = Notes.update_note(note, @invalid_attrs, user)
+ assert note == Notes.get_note!(note.id, user)
end
- test "delete_note/1 deletes the note" do
- note = note_fixture()
- assert {:ok, %Note{}} = Notes.delete_note(note)
- assert_raise Ecto.NoResultsError, fn -> Notes.get_note!(note.id) end
+ test "delete_note/1 deletes the note", %{user: user} do
+ note = note_fixture(user)
+ assert {:ok, %Note{}} = Notes.delete_note(note, user)
+ assert_raise Ecto.NoResultsError, fn -> Notes.get_note!(note.id, user) end
end
- test "change_note/1 returns a note changeset" do
- note = note_fixture()
- assert %Ecto.Changeset{} = Notes.change_note(note)
+ test "change_note/1 returns a note changeset", %{user: user} do
+ note = note_fixture(user)
+ assert %Ecto.Changeset{} = Notes.change_note(note, user)
end
end
end
diff --git a/test/memex_web/live/context_live_test.exs b/test/memex_web/live/context_live_test.exs
index 3529d75..2522147 100644
--- a/test/memex_web/live/context_live_test.exs
+++ b/test/memex_web/live/context_live_test.exs
@@ -4,14 +4,24 @@ defmodule MemexWeb.ContextLiveTest do
import Phoenix.LiveViewTest
import Memex.ContextsFixtures
- @create_attrs %{content: "some content", tag: [], title: "some title", visibility: :public}
- @update_attrs %{
- content: "some updated content",
- tag: [],
- title: "some updated title",
- visibility: :private
+ @create_attrs %{
+ "content" => "some content",
+ "tags_string" => "tag1",
+ "title" => "some title",
+ "visibility" => :public
+ }
+ @update_attrs %{
+ "content" => "some updated content",
+ "tags_string" => "tag1,tag2",
+ "title" => "some updated title",
+ "visibility" => :private
+ }
+ @invalid_attrs %{
+ "content" => nil,
+ "tags_string" => "",
+ "title" => nil,
+ "visibility" => nil
}
- @invalid_attrs %{content: nil, tag: [], title: nil, visibility: nil}
defp create_context(_) do
context = context_fixture()
diff --git a/test/memex_web/live/note_live_test.exs b/test/memex_web/live/note_live_test.exs
index 63bf0ee..7b70a57 100644
--- a/test/memex_web/live/note_live_test.exs
+++ b/test/memex_web/live/note_live_test.exs
@@ -4,22 +4,31 @@ defmodule MemexWeb.NoteLiveTest do
import Phoenix.LiveViewTest
import Memex.NotesFixtures
- @create_attrs %{content: "some content", tag: [], title: "some title", visibility: :public}
- @update_attrs %{
- content: "some updated content",
- tag: [],
- title: "some updated title",
- visibility: :private
+ @create_attrs %{
+ "content" => "some content",
+ "tags_string" => "tag1",
+ "title" => "some title",
+ "visibility" => :public
+ }
+ @update_attrs %{
+ "content" => "some updated content",
+ "tags_string" => "tag1,tag2",
+ "title" => "some updated title",
+ "visibility" => :private
+ }
+ @invalid_attrs %{
+ "content" => nil,
+ "tags_string" => "",
+ "title" => nil,
+ "visibility" => nil
}
- @invalid_attrs %{content: nil, tag: [], title: nil, visibility: nil}
- defp create_note(_) do
- note = note_fixture()
- %{note: note}
+ defp create_note(%{user: user}) do
+ [note: note_fixture(user)]
end
describe "Index" do
- setup [:create_note]
+ setup [:register_and_log_in_user, :create_note]
test "lists all notes", %{conn: conn, note: note} do
{:ok, _index_live, html} = live(conn, Routes.note_index_path(conn, :index))
@@ -53,7 +62,7 @@ defmodule MemexWeb.NoteLiveTest do
test "updates note in listing", %{conn: conn, note: note} do
{:ok, index_live, _html} = live(conn, Routes.note_index_path(conn, :index))
- assert index_live |> element("#note-#{note.id} a", "Edit") |> render_click() =~
+ assert index_live |> element("[data-qa=\"note-edit-#{note.id}\"]") |> render_click() =~
"edit"
assert_patch(index_live, Routes.note_index_path(conn, :edit, note))
@@ -75,7 +84,7 @@ defmodule MemexWeb.NoteLiveTest do
test "deletes note in listing", %{conn: conn, note: note} do
{:ok, index_live, _html} = live(conn, Routes.note_index_path(conn, :index))
- assert index_live |> element("#note-#{note.id} a", "Delete") |> render_click()
+ assert index_live |> element("[data-qa=\"delete-note-#{note.id}\"]") |> render_click()
refute has_element?(index_live, "#note-#{note.id}")
end
end
diff --git a/test/support/fixtures/notes_fixtures.ex b/test/support/fixtures/notes_fixtures.ex
index 6548cc3..ee3266b 100644
--- a/test/support/fixtures/notes_fixtures.ex
+++ b/test/support/fixtures/notes_fixtures.ex
@@ -3,20 +3,23 @@ defmodule Memex.NotesFixtures do
This module defines test helpers for creating
entities via the `Memex.Notes` context.
"""
+ alias Memex.{Accounts.User, Notes, Notes.Note}
@doc """
Generate a note.
"""
- def note_fixture(attrs \\ %{}) do
+ @spec note_fixture(User.t()) :: Note.t()
+ @spec note_fixture(attrs :: map(), User.t()) :: Note.t()
+ def note_fixture(attrs \\ %{}, user) do
{:ok, note} =
attrs
|> Enum.into(%{
content: "some content",
tag: [],
title: "some title",
- visibility: :public
+ visibility: :private
})
- |> Memex.Notes.create_note()
+ |> Notes.create_note(user)
note
end