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

46 lines
1.0 KiB
Elixir
Raw Normal View History

2022-07-25 20:08:40 -04:00
defmodule MemexWeb.NoteLive.Index do
use MemexWeb, :live_view
alias Memex.Notes
alias Memex.Notes.Note
@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :notes, list_notes())}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :edit, %{"id" => id}) do
socket
2022-11-17 22:30:01 -05:00
|> assign(page_title: gettext("edit %{title}", title: title))
2022-07-25 20:08:40 -04:00
|> assign(:note, Notes.get_note!(id))
end
defp apply_action(socket, :new, _params) do
socket
2022-11-17 22:30:01 -05:00
|> assign(page_title: "new note")
2022-07-25 20:08:40 -04:00
|> assign(:note, %Note{})
end
defp apply_action(socket, :index, _params) do
socket
2022-11-17 22:30:01 -05:00
|> assign(page_title: "notes")
2022-07-25 20:08:40 -04:00
|> assign(:note, nil)
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
note = Notes.get_note!(id)
{:ok, _} = Notes.delete_note(note)
2022-11-17 22:30:01 -05:00
|> put_flash(:info, gettext("%{title} deleted", title: title))
2022-07-25 20:08:40 -04:00
defp list_notes do
Notes.list_notes()
end
end