chiya/lib/chiya_web/live/note_show_live.ex

205 lines
6.3 KiB
Elixir
Raw Normal View History

2023-03-09 07:28:06 +01:00
defmodule ChiyaWeb.NoteShowLive do
use ChiyaWeb, :live_view
alias Chiya.Notes
alias Chiya.Notes.NoteImage
import Phoenix.HTML.Tag
2023-03-09 07:28:06 +01:00
2023-06-20 23:10:48 +02:00
@accepted_extensions ~w(.jpg .jpeg .gif .png .webp)
2023-03-09 07:28:06 +01:00
@impl true
def render(assigns) do
~H"""
<.header>
2023-04-08 09:54:57 +02:00
<%= @note.name %>
2023-03-09 07:28:06 +01:00
<:actions>
<.link href={~p"/admin/notes/#{@note}/edit"}>
2023-07-04 07:13:57 +02:00
<.button><.icon name="hero-pencil" /> Edit</.button>
2023-03-09 07:28:06 +01:00
</.link>
2023-06-02 07:07:22 +02:00
<.link href={~p"/note/#{@note.slug}"}>
2023-07-04 07:13:57 +02:00
<.button><.icon name="hero-eye" /> Preview</.button>
2023-06-03 11:47:55 +02:00
</.link>
2023-05-21 09:53:17 +02:00
<%= if is_nil(@note.published_at) do %>
<.link href={~p"/admin/notes/#{@note}/publish"}>
2023-07-04 07:13:57 +02:00
<.button><.icon name="hero-newspaper" /> Publish</.button>
</.link>
2023-05-21 09:53:17 +02:00
<% else %>
2023-06-02 07:07:22 +02:00
<.link href={~p"/admin/notes/#{@note}/unpublish"}>
2023-07-04 07:13:57 +02:00
<.button><.icon name="hero-newspaper" /> Un-Publish</.button>
</.link>
2023-05-21 09:53:17 +02:00
<% end %>
2023-07-04 07:13:57 +02:00
<.link href={~p"/admin/notes/#{@note}/raw"}>
<.button><.icon name="hero-code-bracket" /> Raw</.button>
</.link>
<.link href={~p"/admin/notes/#{@note}"} method="delete" data-confirm="Are you sure?">
<.button><.icon name="hero-trash" /> Delete</.button>
</.link>
2023-03-09 07:28:06 +01:00
</:actions>
</.header>
<.list>
2023-04-10 12:08:19 +02:00
<:item title="Published at">
<%= pretty_date(@note.published_at) %> <span>(<%= from_now(@note.published_at) %>)</span>
</:item>
<:item title="Channels"><%= note_channels(@note.channels) %></:item>
2023-03-09 07:28:06 +01:00
<:item title="Kind"><%= @note.kind %></:item>
<:item title="Url"><%= @note.url %></:item>
2023-04-10 19:18:27 +02:00
<:item title="Tags"><%= note_tags(@note.tags) %></:item>
2023-04-11 22:52:45 +02:00
<:item title="Links outgoing"><%= note_links(@note.links_from) %></:item>
<:item title="Links incoming"><%= note_links(@note.links_to) %></:item>
<:item title="Embed">
<pre class="p-1 bg-gray-100 text-black rounded select-all">[[<%= @note.slug %>]]</pre>
</:item>
</.list>
2023-03-09 07:28:06 +01:00
<.line />
2023-03-09 21:04:16 +01:00
<%= if !Enum.empty?(@note.images) do %>
<div class="flex flex-wrap gap-3" id="images">
2023-03-09 21:44:29 +01:00
<%= for image <- @note.images do %>
<article>
<a href={"/admin/notes/#{@note.id}/image/#{image.id}"}>
2023-03-09 21:44:29 +01:00
<img
2023-06-19 21:49:41 +02:00
class={[
"rounded-lg border border-theme-dim w-28 mb-3",
image.featured && "border-theme-primary"
]}
2023-03-23 08:17:47 +01:00
src={ChiyaWeb.Uploaders.NoteImage.url({image.path, image}, :thumb_dithered)}
2023-06-20 20:28:45 +02:00
/>
2023-06-19 21:49:41 +02:00
</a>
<div class="flex justify-between">
2023-04-10 12:08:19 +02:00
<.button phx-click="delete_image" phx-value-id={image.id} data-confirm="Are you sure?">
2023-06-19 21:49:41 +02:00
<.icon name="hero-trash" />
</.button>
2023-06-19 21:49:41 +02:00
<.button phx-click="toggle_favorite" phx-value-id={image.id}>
<.icon name="hero-star-solid" />
</.button>
</div>
2023-03-09 21:44:29 +01:00
</article>
<% end %>
</div>
<.line />
2023-03-09 21:04:16 +01:00
<% end %>
2023-03-09 07:28:06 +01:00
<.header>
Note Images
<:subtitle>Add images here</:subtitle>
</.header>
<.simple_form
for={@image_form}
id="image_form"
phx-submit="update_image"
phx-change="validate_image"
multipart={true}
>
<.live_upload upload={@uploads.note_images} />
<:actions>
<.button phx-disable-with="Changing...">Add Images</.button>
</:actions>
</.simple_form>
"""
end
@impl true
def mount(%{"id" => note_id}, _session, socket) do
image_changeset = Notes.change_note_image(%NoteImage{})
2023-07-04 07:13:49 +02:00
note = Notes.get_note_preloaded!(note_id)
2023-03-09 07:28:06 +01:00
{:ok,
socket
2023-07-04 07:13:49 +02:00
|> assign(:note, note)
2023-03-09 07:28:06 +01:00
|> assign(:uploaded_files, [])
|> assign(:image_edit_form, to_form(image_changeset))
2023-03-09 07:28:06 +01:00
|> assign(:image_form, to_form(image_changeset))
2023-07-04 07:13:49 +02:00
|> assign(:page_title, note.name)
2023-06-20 23:10:48 +02:00
|> allow_upload(:note_images,
accept: @accepted_extensions,
max_entries: 100
)}
2023-03-09 07:28:06 +01:00
end
def handle_event("validate_image", _params, socket) do
{:noreply, socket}
end
@impl Phoenix.LiveView
def handle_event("cancel-upload", %{"ref" => ref}, socket) do
{:noreply, cancel_upload(socket, :note_images, ref)}
end
@impl Phoenix.LiveView
def handle_event("update_image", _params, socket) do
uploaded_files =
consume_uploaded_entries(socket, :note_images, fn %{path: path}, _entry ->
{:ok, _note_image} =
Notes.create_note_image(%{
path: path,
note_id: socket.assigns.note.id
})
{:ok, path}
end)
{:noreply,
socket
|> update(:uploaded_files, &(&1 ++ uploaded_files))
|> assign(:note, Notes.get_note_preloaded!(socket.assigns.note.id))}
end
2023-03-09 21:04:16 +01:00
2023-06-19 21:49:41 +02:00
@impl Phoenix.LiveView
def handle_event("validate_edit_image", assigns, socket) do
{:noreply,
socket
|> assign(
:image_edit_form,
to_form(Notes.change_note_image(%NoteImage{}, assigns))
)}
end
2023-06-19 21:49:41 +02:00
@impl Phoenix.LiveView
def handle_event("update_edit_image", %{"id" => id} = assigns, socket) do
id
|> Notes.get_note_image!()
|> Notes.update_note_image(assigns)
2023-04-10 12:08:19 +02:00
{:noreply, socket}
end
2023-03-09 21:04:16 +01:00
@impl Phoenix.LiveView
def handle_event("delete_image", %{"id" => id}, socket) do
2023-03-09 21:44:29 +01:00
:ok =
Notes.get_note_image!(id)
|> Notes.delete_note_image()
2023-03-09 21:04:16 +01:00
{:noreply, assign(socket, :note, Notes.get_note_preloaded!(socket.assigns.note.id))}
end
2023-04-09 16:14:55 +02:00
2023-06-19 21:49:41 +02:00
@impl Phoenix.LiveView
def handle_event("toggle_favorite", %{"id" => id}, socket) do
image = Notes.get_note_image!(id)
Notes.update_note_image(image, %{featured: !image.featured})
{:noreply, assign(socket, :note, Notes.get_note_preloaded!(socket.assigns.note.id))}
end
defp note_links(notes), do: content_tag(:ul, do: Enum.map(notes, &note_link/1))
defp note_link(note) do
content_tag(:li, do: content_tag(:a, note.name, href: Chiya.Notes.Note.note_path_admin(note)))
end
defp note_tags(tags), do: content_tag(:ul, do: Enum.map(tags, &note_tag/1))
defp note_tag(tag),
do: content_tag(:li, do: content_tag(:a, tag.name, href: ~p"/tagged-with/#{tag.slug}"))
defp note_channels(channels), do: content_tag(:ul, do: Enum.map(channels, &note_channel/1))
defp note_channel(channel),
do:
content_tag(:li, do: content_tag(:a, channel.name, href: ~p"/admin/channels/#{channel.id}"))
2023-03-09 07:28:06 +01:00
end