diff --git a/lib/chiya/notes.ex b/lib/chiya/notes.ex index 5fd7cb4..1119b45 100644 --- a/lib/chiya/notes.ex +++ b/lib/chiya/notes.ex @@ -131,7 +131,12 @@ defmodule Chiya.Notes do end @doc """ - Creates a note image and attaches it to a note + Gets a single note image. + """ + def get_note_image!(id), do: Repo.get!(NoteImage, id) + + @doc """ + Creates a note image and attaches it to a note. """ def create_note_image(attrs) do case %NoteImage{} @@ -147,6 +152,12 @@ defmodule Chiya.Notes do end end + def delete_note_image(%NoteImage{} = note_image) do + {:ok, _} = Repo.delete(note_image) + :ok = Chiya.Uploaders.NoteImage.delete({note_image.path, note_image}) + :ok + end + @doc """ Returns an `%Ecto.Changeset{}` for tracking note_image changes. """ diff --git a/lib/chiya_web/live/note_show_live.ex b/lib/chiya_web/live/note_show_live.ex index 1aa044d..73af776 100644 --- a/lib/chiya_web/live/note_show_live.ex +++ b/lib/chiya_web/live/note_show_live.ex @@ -5,6 +5,8 @@ defmodule ChiyaWeb.NoteShowLive do @impl true def render(assigns) do + channels = Enum.map_join(assigns.note.channels, ", ", fn c -> c.name end) + ~H""" <.header> Note <%= @note.id %> @@ -23,16 +25,12 @@ defmodule ChiyaWeb.NoteShowLive do <:item title="Published at"><%= @note.published_at %> <:item title="Kind"><%= @note.kind %> <:item title="Url"><%= @note.url %> + <:item title="Channels"><%= channels %> - - <.line /> + <%= if !Enum.empty?(@note.images) do %>
<%= for image <- @note.images do %>
@@ -40,7 +38,9 @@ defmodule ChiyaWeb.NoteShowLive do class="rounded-lg w-28 " src={Chiya.Uploaders.NoteImage.url({image.path, image}, :thumb_dithered)} /> -

Delete image

+

+ Delete image +

@@ -50,6 +50,7 @@ defmodule ChiyaWeb.NoteShowLive do
<.line /> + <% end %> <.header> Note Images @@ -113,4 +114,12 @@ defmodule ChiyaWeb.NoteShowLive do |> update(:uploaded_files, &(&1 ++ uploaded_files)) |> assign(:note, Notes.get_note_preloaded!(socket.assigns.note.id))} end + + @impl Phoenix.LiveView + def handle_event("delete_image", %{"id" => id}, socket) do + :ok = Notes.get_note_image!(id) + |> Notes.delete_note_image() + + {:noreply, assign(socket, :note, Notes.get_note_preloaded!(socket.assigns.note.id))} + end end