diff --git a/lib/chiya/notes.ex b/lib/chiya/notes.ex index b53c2f1..84d168d 100644 --- a/lib/chiya/notes.ex +++ b/lib/chiya/notes.ex @@ -138,7 +138,7 @@ defmodule Chiya.Notes do @doc """ Gets a single note image. """ - def get_note_image!(id), do: Repo.get!(NoteImage, id) + def get_note_image!(id), do: Repo.get!(NoteImage, id) |> Repo.preload(:note) @doc """ Creates a note image and attaches it to a note. diff --git a/lib/chiya/notes/note_image.ex b/lib/chiya/notes/note_image.ex index 0ca7f30..0c30629 100644 --- a/lib/chiya/notes/note_image.ex +++ b/lib/chiya/notes/note_image.ex @@ -6,9 +6,10 @@ defmodule Chiya.Notes.NoteImage do schema "note_images" do field :content, :string, default: "" field :path, ChiyaWeb.Uploaders.NoteImage.Type - field :note_id, :id field :featured, :boolean, default: false + belongs_to :note, Chiya.Notes.Note + timestamps() end diff --git a/lib/chiya_web/controllers/note_controller.ex b/lib/chiya_web/controllers/note_controller.ex index 8ad0f20..f4059ce 100644 --- a/lib/chiya_web/controllers/note_controller.ex +++ b/lib/chiya_web/controllers/note_controller.ex @@ -75,6 +75,26 @@ defmodule ChiyaWeb.NoteController do |> text(raw_note) end + def edit_image(conn, %{"image_id" => id}) do + image = Notes.get_note_image!(id) + changeset = Notes.change_note_image(image) + render(conn, :edit_image, image: image, changeset: changeset) + end + + def update_image(conn, %{"image_id" => id, "note_image" => image_params}) do + image = Notes.get_note_image!(id) + + case Notes.update_note_image(image, image_params) do + {:ok, image} -> + conn + |> put_flash(:info, "Image updated successfully.") + |> redirect(to: ~p"/admin/notes/#{image.note_id}") + + {:error, %Ecto.Changeset{} = changeset} -> + render(conn, :edit_image, image: image, changeset: changeset) + end + end + defp from_channel_ids(note_params) do selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1) diff --git a/lib/chiya_web/controllers/note_html/edit_image.html.heex b/lib/chiya_web/controllers/note_html/edit_image.html.heex new file mode 100644 index 0000000..c8461a5 --- /dev/null +++ b/lib/chiya_web/controllers/note_html/edit_image.html.heex @@ -0,0 +1,21 @@ +<.header> + Edit Image <%= @image.id %> + <:subtitle>Use this form to manage note image records in your database. + + +<.simple_form :let={f} for={@changeset} action={~p"/admin/notes/#{@image.note.id}/image/#{@image.id}"}> + <.error :if={@changeset.action}> + Oops, something went wrong! Please check the errors below. + + + <.input field={f[:id]} type="hidden" value={@image.id} /> + <.input field={f[:content]} type="textarea" label="Content" /> + <.input field={f[:featured]} type="checkbox" label="Featured" /> + + <:actions> + <.button type="submit">Save + + + + +<.back navigate={~p"/admin/notes/#{@image.note_id}"}>Back to notes \ No newline at end of file diff --git a/lib/chiya_web/controllers/page_html/note.html.heex b/lib/chiya_web/controllers/page_html/note.html.heex index bfd0774..2493119 100644 --- a/lib/chiya_web/controllers/page_html/note.html.heex +++ b/lib/chiya_web/controllers/page_html/note.html.heex @@ -17,7 +17,7 @@
<%= for image <- @note.images do %> - + diff --git a/lib/chiya_web/live/note_show_live.ex b/lib/chiya_web/live/note_show_live.ex index b28c633..7ec0e0e 100644 --- a/lib/chiya_web/live/note_show_live.ex +++ b/lib/chiya_web/live/note_show_live.ex @@ -42,39 +42,19 @@ defmodule ChiyaWeb.NoteShowLive do
<%= for image <- @note.images do %>
- + - - - <.modal id={"image-edit-modal-#{image.id}"}> - <.simple_form - :let={f} - for={to_form(Notes.change_note_image(image))} - id={"image-edit-form-#{image.id}"} - phx-submit="update_edit_image" - phx-change="validate_edit_image" + <.button + phx-click="delete_image" + phx-value-id={image.id} + data-confirm="Are you sure?" > - <.input field={f[:id]} type="hidden" value={image.id} /> - <.input field={f[:content]} type="textarea" label="Content" /> - <.input field={f[:featured]} type="checkbox" label="Featured" /> - - <:actions> - <.button type="submit" phx-click={hide_modal("image-edit-modal-#{image.id}")}> - Save - - <.button - phx-click="delete_image" - phx-value-id={image.id} - data-confirm="Are you sure?" - > - Delete Image - - - - + Delete Image + +
<% end %>
diff --git a/lib/chiya_web/router.ex b/lib/chiya_web/router.ex index 16eec7d..cf0d09b 100644 --- a/lib/chiya_web/router.ex +++ b/lib/chiya_web/router.ex @@ -63,6 +63,9 @@ defmodule ChiyaWeb.Router do live "/notes/:id", NoteShowLive, :show get "/notes/:id/raw", NoteController, :raw + + get "/notes/:id/image/:image_id", NoteController, :edit_image + put "/notes/:id/image/:image_id", NoteController, :update_image end ## Authentication routes