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
import Ecto.Changeset
alias Ecto.{Changeset, UUID}
alias Memex.{Accounts.User, Notes.Note}
alias Memex.Accounts.User
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "notes" do
field :title, :string
field :content, :string
field :tags, {:array, :string}
field :tags_string, :string, virtual: true
field :title, :string
field :visibility, Ecto.Enum, values: [:public, :private, :unlisted]
belongs_to :user, User
@ -21,21 +21,31 @@ defmodule Memex.Notes.Note do
timestamps()
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 changeset :: Changeset.t(t())
@doc false
@spec create_changeset(attrs :: map(), User.t()) :: changeset()
def create_changeset(attrs, %User{id: user_id}) do
%Note{}
%__MODULE__{}
|> cast(attrs, [:title, :content, :tags, :visibility])
|> change(user_id: user_id)
|> cast_tags_string(attrs)
|> validate_required([:title, :content, :user_id, :visibility])
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
note
|> 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
socket
|> assign(page_title: "new note")
|> assign(page_title: gettext("new note"))
|> assign(note: %Note{user_id: current_user_id})
end
defp apply_action(socket, :index, _params) do
socket
|> assign(page_title: "notes")
|> assign(page_title: gettext("notes"))
|> assign(search: nil)
|> assign(note: nil)
|> display_notes()
@ -40,7 +40,7 @@ defmodule MemexWeb.NoteLive.Index do
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(page_title: "notes")
|> assign(page_title: gettext("notes"))
|> assign(search: search)
|> assign(note: nil)
|> display_notes()
@ -48,8 +48,8 @@ defmodule MemexWeb.NoteLive.Index do
@impl true
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)
note = Notes.get_note!(id, current_user)
{:ok, %{title: title}} = Notes.delete_note(note, current_user)
socket =
socket

View File

@ -8,10 +8,14 @@
for={:search}
phx-change="search"
phx-submit="search"
phx-debounce="500"
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>
<%= if @notes |> Enum.empty?() do %>

View File

@ -20,6 +20,22 @@ defmodule MemexWeb.NoteLive.Show do
|> assign(:note, Notes.get_note!(id, current_user))}
end
defp page_title(:show), do: "show note"
defp page_title(:edit), do: "edit note"
@impl true
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

View File

@ -20,12 +20,22 @@
<div class="self-end flex space-x-4">
<.link class="btn btn-primary" patch={Routes.note_index_path(@socket, :index)}>
<%= dgettext("actions", "Back") %>
<%= 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>
<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 %>
</div>
</div>

View File

@ -11,7 +11,6 @@ msgid ""
msgstr ""
#: 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
#, elixir-autogen, elixir-format
msgid "Back"
@ -75,7 +74,8 @@ msgid "create invite"
msgstr ""
#: 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
#, elixir-autogen, elixir-format
msgid "delete"
@ -88,7 +88,7 @@ msgstr ""
#: lib/memex_web/live/context_live/index.html.heex:43
#: 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/pipeline_live/index.html.heex:41
#: lib/memex_web/live/pipeline_live/show.html.heex:35
@ -117,7 +117,7 @@ msgstr ""
msgid "new context"
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
msgid "new note"
msgstr ""
@ -148,3 +148,8 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "show"
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 ""
#: lib/memex_web/live/note_live/index.ex:57
#: lib/memex_web/live/note_live/show.ex:33
#, elixir-autogen, elixir-format
msgid "%{title} deleted"
msgstr ""
@ -267,12 +268,14 @@ msgstr ""
msgid "no invites 😔"
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
msgid "no notes found"
msgstr ""
#: 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
#, elixir-autogen, elixir-format
msgid "notes"
@ -398,3 +401,23 @@ msgstr ""
#, elixir-autogen, elixir-format
msgid "visibility: %{visibility}"
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 ""
#: 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
#, elixir-autogen, elixir-format
msgid "are you sure?"

View File

@ -119,5 +119,17 @@ defmodule MemexWeb.NoteLiveTest do
assert html =~ "#{@update_attrs |> Map.get("title")} saved"
assert html =~ "some updated content"
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