My space on the web https://inhji.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

64 lines
1.5 KiB

defmodule MirageWeb.NoteLive.Show do
@moduledoc """
Shows notes and their edit form
"""
use MirageWeb, :live_view
alias Mirage.Notes
alias Mirage.Links
alias Mirage.Links.Link
defp page_title(:show, note), do: note.title
defp page_title(:edit, note), do: "Edit Note"
@impl true
def mount(_params, session, socket) do
{:ok, socket |> with_user(session)}
end
@impl true
def handle_params(%{"id" => id}, _, socket) do
note =
Notes.get_note!(id)
|> Notes.view_note!()
|> Notes.preload_note()
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action, note))
|> assign(:note, note)
|> assign(:link_changeset, Link.changeset(%Link{}, %{note_id: id}))}
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
link = Notes.get_note!(id)
{:ok, _} = Notes.delete_note(link)
{:noreply, push_redirect(socket, to: Routes.note_index_path(socket, :index))}
end
@impl true
def handle_event("save_link", %{"link" => link}, socket) do
note_id = socket.assigns.note.id
with {:ok, link} <- Links.create_link_or_return_existing(link),
{:ok, _note_link} <- Notes.create_note_link(%{link_id: link.id, note_id: note_id}) do
note =
note_id
|> Notes.get_note!()
|> Notes.preload_note()
{:noreply,
socket
|> assign(%{
note: note,
link_changeset: Link.changeset(%Link{}, %{note_id: note_id})
})}
else
_error ->
{:noreply, socket}
end
end
end