2023-03-09 07:28:06 +01:00
|
|
|
defmodule ChiyaWeb.NoteShowLive do
|
|
|
|
use ChiyaWeb, :live_view
|
|
|
|
|
|
|
|
alias Chiya.Notes
|
2023-04-01 12:09:58 +02:00
|
|
|
alias Chiya.Notes.NoteImage
|
2023-03-09 07:28:06 +01:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
2023-03-09 21:04:16 +01:00
|
|
|
channels = Enum.map_join(assigns.note.channels, ", ", fn c -> c.name end)
|
2023-03-09 21:46:07 +01:00
|
|
|
assigns = assign(assigns, :channels, channels)
|
2023-03-09 21:04:16 +01:00
|
|
|
|
2023-03-09 07:28:06 +01:00
|
|
|
~H"""
|
|
|
|
<.header>
|
2023-04-08 09:54:57 +02:00
|
|
|
<%= @note.name %>
|
2023-04-09 18:36:00 +02:00
|
|
|
<:subtitle>
|
|
|
|
<pre class="p-1 bg-gray-100 rounded select-all">[[<%= @note.slug %>]]</pre>
|
|
|
|
</:subtitle>
|
2023-03-09 07:28:06 +01:00
|
|
|
<:actions>
|
|
|
|
<.link href={~p"/admin/notes/#{@note}/edit"}>
|
2023-04-09 16:14:55 +02:00
|
|
|
<.button>Edit</.button>
|
2023-03-09 07:28:06 +01:00
|
|
|
</.link>
|
2023-04-02 23:46:49 +02:00
|
|
|
<.link href={~p"/#{@note.slug}"}>
|
2023-03-31 22:20:32 +02:00
|
|
|
<.button>Preview</.button>
|
|
|
|
</.link>
|
|
|
|
<.link href={~p"/admin/notes/#{@note}/raw"}>
|
|
|
|
<.button>Raw</.button>
|
2023-03-13 05:59:29 +01:00
|
|
|
</.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>
|
2023-04-08 09:54:57 +02:00
|
|
|
<:item title="Channels"><%= @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>
|
2023-03-09 15:03:22 +01:00
|
|
|
</.list>
|
|
|
|
|
2023-03-09 07:28:06 +01:00
|
|
|
<.line />
|
|
|
|
|
2023-03-09 21:04:16 +01:00
|
|
|
<%= if !Enum.empty?(@note.images) do %>
|
2023-03-22 08:37:52 +01:00
|
|
|
<div class="flex flex-wrap gap-3" id="images">
|
2023-03-09 21:44:29 +01:00
|
|
|
<%= for image <- @note.images do %>
|
|
|
|
<article>
|
2023-04-06 15:03:04 +02:00
|
|
|
<a href={"/admin/notes/#{@note.id}/image/#{image.id}"}>
|
2023-03-09 21:44:29 +01:00
|
|
|
<img
|
2023-04-13 23:52:01 +02:00
|
|
|
class="rounded-lg border border-theme-dim w-28 mb-3"
|
2023-03-23 08:17:47 +01:00
|
|
|
src={ChiyaWeb.Uploaders.NoteImage.url({image.path, image}, :thumb_dithered)}
|
2023-03-09 21:44:29 +01:00
|
|
|
/>
|
2023-04-10 12:08:19 +02:00
|
|
|
<.button phx-click="delete_image" phx-value-id={image.id} data-confirm="Are you sure?">
|
2023-04-06 15:03:04 +02:00
|
|
|
Delete Image
|
|
|
|
</.button>
|
|
|
|
</a>
|
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>
|
|
|
|
|
|
|
|
<.back navigate={~p"/admin/notes"}>Back to notes</.back>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def mount(%{"id" => note_id}, _session, socket) do
|
2023-04-01 12:09:58 +02:00
|
|
|
image_changeset = Notes.change_note_image(%NoteImage{})
|
2023-03-09 07:28:06 +01:00
|
|
|
|
|
|
|
{:ok,
|
|
|
|
socket
|
|
|
|
|> assign(:note, Notes.get_note_preloaded!(note_id))
|
|
|
|
|> assign(:uploaded_files, [])
|
2023-04-01 12:09:58 +02:00
|
|
|
|> assign(:image_edit_form, to_form(image_changeset))
|
2023-03-09 07:28:06 +01:00
|
|
|
|> assign(:image_form, to_form(image_changeset))
|
|
|
|
|> allow_upload(:note_images, accept: ~w(.jpg .jpeg .gif .png), max_entries: 100)}
|
|
|
|
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-04-01 12:09:58 +02:00
|
|
|
def handle_event("validate_edit_image", assigns, socket) do
|
|
|
|
{:noreply,
|
|
|
|
socket
|
|
|
|
|> assign(
|
|
|
|
:image_edit_form,
|
|
|
|
to_form(Notes.change_note_image(%NoteImage{}, assigns))
|
|
|
|
)}
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-04-01 12:09:58 +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-04-10 19:18:27 +02:00
|
|
|
defp note_links(notes), do: Enum.map_join(notes, ", ", fn n -> n.name end)
|
|
|
|
defp note_tags(tags), do: Enum.map_join(tags, ", ", fn t -> t.name end)
|
2023-03-09 07:28:06 +01:00
|
|
|
end
|