memEx/lib/memex_web/live/note_live/index.ex

80 lines
2.4 KiB
Elixir
Raw Normal View History

2022-07-25 20:08:40 -04:00
defmodule MemexWeb.NoteLive.Index do
use MemexWeb, :live_view
2022-11-17 22:38:52 -05:00
alias Memex.{Notes, Notes.Note}
2022-07-25 20:08:40 -04:00
@impl true
2022-11-18 23:45:15 -05:00
def mount(%{"search" => search}, _session, socket) do
{:ok, socket |> assign(search: search) |> display_notes()}
2022-11-17 22:38:52 -05:00
end
2022-07-25 20:08:40 -04:00
def mount(_params, _session, socket) do
2022-11-18 23:45:15 -05:00
{:ok, socket |> assign(search: nil) |> display_notes()}
2022-07-25 20:08:40 -04:00
end
@impl true
2022-11-17 22:38:52 -05:00
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, apply_action(socket, live_action, params)}
2022-07-25 20:08:40 -04:00
end
2022-11-17 22:38:52 -05:00
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
%{title: title} = note = Notes.get_note!(id, current_user)
2022-07-25 20:08:40 -04:00
socket
2022-11-17 22:30:01 -05:00
|> assign(page_title: gettext("edit %{title}", title: title))
2022-11-17 22:38:52 -05:00
|> assign(note: note)
2022-07-25 20:08:40 -04:00
end
2022-11-17 22:38:52 -05:00
defp apply_action(%{assigns: %{current_user: %{id: current_user_id}}} = socket, :new, _params) do
2022-07-25 20:08:40 -04:00
socket
2022-11-17 22:30:01 -05:00
|> assign(page_title: "new note")
2022-11-17 22:38:52 -05:00
|> assign(note: %Note{user_id: current_user_id})
2022-07-25 20:08:40 -04:00
end
defp apply_action(socket, :index, _params) do
socket
2022-11-17 22:30:01 -05:00
|> assign(page_title: "notes")
2022-11-18 23:45:15 -05:00
|> assign(search: nil)
2022-11-17 22:38:52 -05:00
|> assign(note: nil)
2022-11-18 23:45:15 -05:00
|> display_notes()
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(page_title: "notes")
|> assign(search: search)
|> assign(note: nil)
|> display_notes()
2022-07-25 20:08:40 -04:00
end
@impl true
2022-11-17 22:38:52 -05:00
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)
2022-07-25 20:08:40 -04:00
2022-11-17 22:38:52 -05:00
socket =
socket
|> assign(notes: Notes.list_notes(current_user))
2022-11-17 22:30:01 -05:00
|> put_flash(:info, gettext("%{title} deleted", title: title))
2022-07-25 20:08:40 -04:00
2022-11-17 22:38:52 -05:00
{:noreply, socket}
2022-07-25 20:08:40 -04:00
end
2022-11-18 23:45:15 -05:00
@impl true
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
{:noreply, socket |> push_patch(to: Routes.note_index_path(Endpoint, :index))}
end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
{:noreply, socket |> push_patch(to: Routes.note_index_path(Endpoint, :search, search_term))}
end
defp display_notes(%{assigns: %{current_user: current_user, search: search}} = socket)
when not (current_user |> is_nil()) do
socket |> assign(notes: Notes.list_notes(search, current_user))
end
defp display_notes(%{assigns: %{search: search}} = socket) do
socket |> assign(notes: Notes.list_public_notes(search))
end
2022-07-25 20:08:40 -04:00
end