defmodule ChiyaWeb.NoteShowLive do use ChiyaWeb, :live_view alias Chiya.Notes @impl true def render(assigns) do ~H""" <.header> Note <%= @note.id %> <:subtitle>This is a note record from your database. <:actions> <.link href={~p"/admin/notes/#{@note}/edit"}> <.button>Edit note <.list> <:item title="Name"><%= @note.name %> <:item title="Content"><%= @note.content %> <:item title="Slug"><%= @note.slug %> <:item title="Published at"><%= @note.published_at %> <:item title="Kind"><%= @note.kind %> <:item title="Url"><%= @note.url %> <.line />
<%= for image <- @note.images do %>

Delete image

<% end %>
<.line /> <.header> Note Images <:subtitle>Add images here <.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 <.back navigate={~p"/admin/notes"}>Back to notes """ end @impl true def mount(%{"id" => note_id}, _session, socket) do image_changeset = Notes.change_note_image(%Chiya.Notes.NoteImage{}) {:ok, socket |> assign(:note, Notes.get_note_preloaded!(note_id)) |> assign(:uploaded_files, []) |> 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 end