diff --git a/lib/memex/notes/note.ex b/lib/memex/notes/note.ex index 84bc68e..641c8a2 100644 --- a/lib/memex/notes/note.ex +++ b/lib/memex/notes/note.ex @@ -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]) diff --git a/lib/memex_web/live/note_live/index.ex b/lib/memex_web/live/note_live/index.ex index d2ff82f..f0457d8 100644 --- a/lib/memex_web/live/note_live/index.ex +++ b/lib/memex_web/live/note_live/index.ex @@ -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 diff --git a/lib/memex_web/live/note_live/index.html.heex b/lib/memex_web/live/note_live/index.html.heex index 233414d..04c08d8 100644 --- a/lib/memex_web/live/note_live/index.html.heex +++ b/lib/memex_web/live/note_live/index.html.heex @@ -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") + ) %> <%= if @notes |> Enum.empty?() do %> diff --git a/lib/memex_web/live/note_live/show.ex b/lib/memex_web/live/note_live/show.ex index 44d6f4b..7142726 100644 --- a/lib/memex_web/live/note_live/show.ex +++ b/lib/memex_web/live/note_live/show.ex @@ -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 diff --git a/lib/memex_web/live/note_live/show.html.heex b/lib/memex_web/live/note_live/show.html.heex index 8de70cf..9d8372f 100644 --- a/lib/memex_web/live/note_live/show.html.heex +++ b/lib/memex_web/live/note_live/show.html.heex @@ -20,12 +20,22 @@