more changes to notes

This commit is contained in:
shibao 2022-11-24 12:43:34 -05:00
parent 2ce4fe3cc8
commit a0a0697f2d
9 changed files with 102 additions and 21 deletions

View File

@ -5,15 +5,15 @@ defmodule Memex.Notes.Note do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
alias Ecto.{Changeset, UUID} alias Ecto.{Changeset, UUID}
alias Memex.{Accounts.User, Notes.Note} alias Memex.Accounts.User
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
schema "notes" do schema "notes" do
field :title, :string
field :content, :string field :content, :string
field :tags, {:array, :string} field :tags, {:array, :string}
field :tags_string, :string, virtual: true field :tags_string, :string, virtual: true
field :title, :string
field :visibility, Ecto.Enum, values: [:public, :private, :unlisted] field :visibility, Ecto.Enum, values: [:public, :private, :unlisted]
belongs_to :user, User belongs_to :user, User
@ -21,21 +21,31 @@ defmodule Memex.Notes.Note do
timestamps() timestamps()
end end
@type t :: %Note{} @type t :: %__MODULE__{
title: String.t(),
content: String.t(),
tags: [String.t()] | nil,
tags_string: String.t(),
visibility: :public | :private | :unlisted,
user: User.t() | Ecto.Association.NotLoaded.t(),
user_id: User.id(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
@type id :: UUID.t() @type id :: UUID.t()
@type changeset :: Changeset.t(t()) @type changeset :: Changeset.t(t())
@doc false @doc false
@spec create_changeset(attrs :: map(), User.t()) :: changeset() @spec create_changeset(attrs :: map(), User.t()) :: changeset()
def create_changeset(attrs, %User{id: user_id}) do def create_changeset(attrs, %User{id: user_id}) do
%Note{} %__MODULE__{}
|> cast(attrs, [:title, :content, :tags, :visibility]) |> cast(attrs, [:title, :content, :tags, :visibility])
|> change(user_id: user_id) |> change(user_id: user_id)
|> cast_tags_string(attrs) |> cast_tags_string(attrs)
|> validate_required([:title, :content, :user_id, :visibility]) |> validate_required([:title, :content, :user_id, :visibility])
end end
@spec update_changeset(Note.t(), attrs :: map(), User.t()) :: changeset() @spec update_changeset(t(), attrs :: map(), User.t()) :: changeset()
def update_changeset(%{user_id: user_id} = note, attrs, %User{id: user_id}) do def update_changeset(%{user_id: user_id} = note, attrs, %User{id: user_id}) do
note note
|> cast(attrs, [:title, :content, :tags, :visibility]) |> cast(attrs, [:title, :content, :tags, :visibility])

View File

@ -26,13 +26,13 @@ defmodule MemexWeb.NoteLive.Index do
defp apply_action(%{assigns: %{current_user: %{id: current_user_id}}} = socket, :new, _params) do defp apply_action(%{assigns: %{current_user: %{id: current_user_id}}} = socket, :new, _params) do
socket socket
|> assign(page_title: "new note") |> assign(page_title: gettext("new note"))
|> assign(note: %Note{user_id: current_user_id}) |> assign(note: %Note{user_id: current_user_id})
end end
defp apply_action(socket, :index, _params) do defp apply_action(socket, :index, _params) do
socket socket
|> assign(page_title: "notes") |> assign(page_title: gettext("notes"))
|> assign(search: nil) |> assign(search: nil)
|> assign(note: nil) |> assign(note: nil)
|> display_notes() |> display_notes()
@ -40,7 +40,7 @@ defmodule MemexWeb.NoteLive.Index do
defp apply_action(socket, :search, %{"search" => search}) do defp apply_action(socket, :search, %{"search" => search}) do
socket socket
|> assign(page_title: "notes") |> assign(page_title: gettext("notes"))
|> assign(search: search) |> assign(search: search)
|> assign(note: nil) |> assign(note: nil)
|> display_notes() |> display_notes()
@ -48,8 +48,8 @@ defmodule MemexWeb.NoteLive.Index do
@impl true @impl true
def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
%{title: title} = note = Notes.get_note!(id, current_user) note = Notes.get_note!(id, current_user)
{:ok, _} = Notes.delete_note(note, current_user) {:ok, %{title: title}} = Notes.delete_note(note, current_user)
socket = socket =
socket socket

View File

@ -8,10 +8,14 @@
for={:search} for={:search}
phx-change="search" phx-change="search"
phx-submit="search" phx-submit="search"
phx-debounce="500"
class="self-stretch flex flex-col items-stretch" class="self-stretch flex flex-col items-stretch"
> >
<%= text_input(f, :search_term, class: "input input-primary", value: @search) %> <%= text_input(f, :search_term,
class: "input input-primary",
value: @search,
phx_debounce: 300,
placeholder: gettext("search")
) %>
</.form> </.form>
<%= if @notes |> Enum.empty?() do %> <%= if @notes |> Enum.empty?() do %>

View File

@ -20,6 +20,22 @@ defmodule MemexWeb.NoteLive.Show do
|> assign(:note, Notes.get_note!(id, current_user))} |> assign(:note, Notes.get_note!(id, current_user))}
end end
defp page_title(:show), do: "show note" @impl true
defp page_title(:edit), do: "edit note" def handle_event(
"delete",
_params,
%{assigns: %{note: note, current_user: current_user}} = socket
) do
{:ok, %{title: title}} = Notes.delete_note(note, current_user)
socket =
socket
|> put_flash(:info, gettext("%{title} deleted", title: title))
|> push_navigate(to: Routes.note_index_path(Endpoint, :index))
{:noreply, socket}
end
defp page_title(:show), do: gettext("show note")
defp page_title(:edit), do: gettext("edit note")
end end

View File

@ -20,12 +20,22 @@
<div class="self-end flex space-x-4"> <div class="self-end flex space-x-4">
<.link class="btn btn-primary" patch={Routes.note_index_path(@socket, :index)}> <.link class="btn btn-primary" patch={Routes.note_index_path(@socket, :index)}>
<%= dgettext("actions", "Back") %> <%= dgettext("actions", "back") %>
</.link> </.link>
<%= if @current_user do %> <%= if @current_user do %>
<.link class="btn btn-primary" patch={Routes.note_show_path(@socket, :edit, @note)}> <.link class="btn btn-primary" patch={Routes.note_show_path(@socket, :edit, @note)}>
<%= dgettext("actions", "edit") %> <%= dgettext("actions", "edit") %>
</.link> </.link>
<button
type="button"
class="btn btn-primary"
phx-click="delete"
data-confirm={dgettext("prompts", "are you sure?")}
data-qa={"delete-note-#{@note.id}"}
>
<%= dgettext("actions", "delete") %>
</button>
<% end %> <% end %>
</div> </div>
</div> </div>

View File

@ -11,7 +11,6 @@ msgid ""
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/show.html.heex:46 #: lib/memex_web/live/context_live/show.html.heex:46
#: lib/memex_web/live/note_live/show.html.heex:23
#: lib/memex_web/live/pipeline_live/show.html.heex:41 #: lib/memex_web/live/pipeline_live/show.html.heex:41
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Back" msgid "Back"
@ -75,7 +74,8 @@ msgid "create invite"
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/index.html.heex:53 #: lib/memex_web/live/context_live/index.html.heex:53
#: lib/memex_web/live/note_live/index.html.heex:43 #: lib/memex_web/live/note_live/index.html.heex:47
#: lib/memex_web/live/note_live/show.html.heex:37
#: lib/memex_web/live/pipeline_live/index.html.heex:51 #: lib/memex_web/live/pipeline_live/index.html.heex:51
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "delete" msgid "delete"
@ -88,7 +88,7 @@ msgstr ""
#: lib/memex_web/live/context_live/index.html.heex:43 #: lib/memex_web/live/context_live/index.html.heex:43
#: lib/memex_web/live/context_live/show.html.heex:40 #: lib/memex_web/live/context_live/show.html.heex:40
#: lib/memex_web/live/note_live/index.html.heex:34 #: lib/memex_web/live/note_live/index.html.heex:38
#: lib/memex_web/live/note_live/show.html.heex:27 #: lib/memex_web/live/note_live/show.html.heex:27
#: lib/memex_web/live/pipeline_live/index.html.heex:41 #: lib/memex_web/live/pipeline_live/index.html.heex:41
#: lib/memex_web/live/pipeline_live/show.html.heex:35 #: lib/memex_web/live/pipeline_live/show.html.heex:35
@ -117,7 +117,7 @@ msgstr ""
msgid "new context" msgid "new context"
msgstr "" msgstr ""
#: lib/memex_web/live/note_live/index.html.heex:52 #: lib/memex_web/live/note_live/index.html.heex:56
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "new note" msgid "new note"
msgstr "" msgstr ""
@ -148,3 +148,8 @@ msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "show" msgid "show"
msgstr "" msgstr ""
#: lib/memex_web/live/note_live/show.html.heex:23
#, elixir-autogen, elixir-format
msgid "back"
msgstr ""

View File

@ -16,6 +16,7 @@ msgid "%{title} created"
msgstr "" msgstr ""
#: lib/memex_web/live/note_live/index.ex:57 #: lib/memex_web/live/note_live/index.ex:57
#: lib/memex_web/live/note_live/show.ex:33
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{title} deleted" msgid "%{title} deleted"
msgstr "" msgstr ""
@ -267,12 +268,14 @@ msgstr ""
msgid "no invites 😔" msgid "no invites 😔"
msgstr "" msgstr ""
#: lib/memex_web/live/note_live/index.html.heex:19 #: lib/memex_web/live/note_live/index.html.heex:23
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "no notes found" msgid "no notes found"
msgstr "" msgstr ""
#: lib/memex_web/components/topbar.ex:43 #: lib/memex_web/components/topbar.ex:43
#: lib/memex_web/live/note_live/index.ex:35
#: lib/memex_web/live/note_live/index.ex:43
#: lib/memex_web/live/note_live/index.html.heex:3 #: lib/memex_web/live/note_live/index.html.heex:3
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "notes" msgid "notes"
@ -398,3 +401,23 @@ msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "visibility: %{visibility}" msgid "visibility: %{visibility}"
msgstr "" msgstr ""
#: lib/memex_web/live/note_live/show.ex:40
#, elixir-autogen, elixir-format
msgid "edit note"
msgstr ""
#: lib/memex_web/live/note_live/index.ex:29
#, elixir-autogen, elixir-format
msgid "new note"
msgstr ""
#: lib/memex_web/live/note_live/index.html.heex:17
#, elixir-autogen, elixir-format
msgid "search"
msgstr ""
#: lib/memex_web/live/note_live/show.ex:39
#, elixir-autogen, elixir-format
msgid "show note"
msgstr ""

View File

@ -142,7 +142,8 @@ msgid "are you sure you want to make %{invite_name} unlimited?"
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/index.html.heex:51 #: lib/memex_web/live/context_live/index.html.heex:51
#: lib/memex_web/live/note_live/index.html.heex:40 #: lib/memex_web/live/note_live/index.html.heex:44
#: lib/memex_web/live/note_live/show.html.heex:34
#: lib/memex_web/live/pipeline_live/index.html.heex:49 #: lib/memex_web/live/pipeline_live/index.html.heex:49
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "are you sure?" msgid "are you sure?"

View File

@ -119,5 +119,17 @@ defmodule MemexWeb.NoteLiveTest do
assert html =~ "#{@update_attrs |> Map.get("title")} saved" assert html =~ "#{@update_attrs |> Map.get("title")} saved"
assert html =~ "some updated content" assert html =~ "some updated content"
end end
test "deletes note", %{conn: conn, note: note} do
{:ok, show_live, _html} = live(conn, Routes.note_show_path(conn, :show, note))
{:ok, index_live, _html} =
show_live
|> element("[data-qa=\"delete-note-#{note.id}\"]")
|> render_click()
|> follow_redirect(conn, Routes.note_index_path(conn, :index))
refute has_element?(index_live, "#note-#{note.id}")
end
end end
end end