From 741483b474749c5ed5f7271c8934965ac1da4b33 Mon Sep 17 00:00:00 2001 From: Inhji Date: Tue, 20 Jun 2023 20:28:45 +0200 Subject: [PATCH] apply default layout on note creation --- lib/chiya/notes/note.ex | 6 ++++-- lib/chiya/site.ex | 2 +- lib/chiya_web/components/core_components.ex | 1 - lib/chiya_web/controllers/note_controller.ex | 9 +++++++-- lib/chiya_web/controllers/note_html.ex | 13 +++++++------ .../controllers/setting_html/show.html.heex | 3 +++ lib/chiya_web/live/note_show_live.ex | 2 +- 7 files changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/chiya/notes/note.ex b/lib/chiya/notes/note.ex index 83f7049..8392f95 100644 --- a/lib/chiya/notes/note.ex +++ b/lib/chiya/notes/note.ex @@ -68,10 +68,12 @@ defmodule Chiya.Notes.Note do @doc false def changeset(note, attrs) do + # if you need to have a preloaded note here, + # do it yourself before you come here. now GET OUT. + note - |> Chiya.Notes.preload_note() |> cast(attrs, [:name, :content, :slug, :published_at, :kind, :url]) - |> put_assoc(:channels, attrs["channels"] || []) + |> put_assoc(:channels, attrs["channels"] || attrs[:channels] || []) |> NoteSlug.maybe_generate_slug() |> NoteSlug.unique_constraint() |> validate_required([:name, :content, :slug, :kind]) diff --git a/lib/chiya/site.ex b/lib/chiya/site.ex index 3ea511a..9fa5174 100644 --- a/lib/chiya/site.ex +++ b/lib/chiya/site.ex @@ -7,7 +7,7 @@ defmodule Chiya.Site do alias Chiya.Repo alias Chiya.Site.Setting - @preloads [:home_channel, :default_channel] + @preloads [:home_channel, :default_channel, :micropub_channel] @default_settings %{ title: "Site Title", subtitle: "Subtitle", diff --git a/lib/chiya_web/components/core_components.ex b/lib/chiya_web/components/core_components.ex index 4924063..8941021 100644 --- a/lib/chiya_web/components/core_components.ex +++ b/lib/chiya_web/components/core_components.ex @@ -850,5 +850,4 @@ defmodule ChiyaWeb.CoreComponents do defp upload_error_to_string(:too_large), do: "Too large" defp upload_error_to_string(:too_many_files), do: "You have selected too many files" defp upload_error_to_string(:not_accepted), do: "You have selected an unacceptable file type" - end diff --git a/lib/chiya_web/controllers/note_controller.ex b/lib/chiya_web/controllers/note_controller.ex index 8be730b..f489f06 100644 --- a/lib/chiya_web/controllers/note_controller.ex +++ b/lib/chiya_web/controllers/note_controller.ex @@ -23,7 +23,11 @@ defmodule ChiyaWeb.NoteController do end def new(conn, _params) do - changeset = Notes.change_note(%Note{}) + settings = Chiya.Site.get_settings() + + changeset = + %Note{channels: [settings.default_channel]} + |> Notes.change_note() render(conn, :new, changeset: changeset, @@ -33,6 +37,7 @@ defmodule ChiyaWeb.NoteController do end def create(conn, %{"note" => note_params}) do + IO.inspect(note_params) note_params = from_channel_ids(note_params) case Notes.create_note(note_params) do @@ -42,7 +47,7 @@ defmodule ChiyaWeb.NoteController do |> redirect(to: ~p"/admin/notes/#{note}") {:error, %Ecto.Changeset{} = changeset} -> - render(conn, :new, changeset: changeset, channels: to_channel_options()) + render(conn, :new, changeset: changeset, channels: to_channel_options(), tags: []) end end diff --git a/lib/chiya_web/controllers/note_html.ex b/lib/chiya_web/controllers/note_html.ex index 41a00a4..fa20b65 100644 --- a/lib/chiya_web/controllers/note_html.ex +++ b/lib/chiya_web/controllers/note_html.ex @@ -1,19 +1,20 @@ defmodule ChiyaWeb.NoteHTML do use ChiyaWeb, :html - embed_templates "note_html/*" + embed_templates("note_html/*") @doc """ Renders a note form. """ - attr :changeset, Ecto.Changeset, required: true - attr :action, :string, required: true - attr :channels, :list, required: true - attr :tags, :list, required: true + attr(:changeset, Ecto.Changeset, required: true) + attr(:action, :string, required: true) + attr(:channels, :list, required: true) + attr(:tags, :list, required: true) def note_form(assigns) - def selected_channels(changeset), do: Enum.map(changeset.data.channels, fn c -> c.id end) + def selected_channels(changeset), + do: Enum.map(changeset.data.channels, fn c -> c.id end) def tags_to_string(tags), do: Enum.map_join(tags, ", ", fn t -> t.name end) end diff --git a/lib/chiya_web/controllers/setting_html/show.html.heex b/lib/chiya_web/controllers/setting_html/show.html.heex index 9dcea5a..93f3545 100644 --- a/lib/chiya_web/controllers/setting_html/show.html.heex +++ b/lib/chiya_web/controllers/setting_html/show.html.heex @@ -37,5 +37,8 @@ <:item title="Default Channel"> <%= if @setting.default_channel, do: @setting.default_channel.name %> + <:item title="Micropub Default Channel"> + <%= if @setting.micropub_channel, do: @setting.micropub_channel.name %> + <% end %> diff --git a/lib/chiya_web/live/note_show_live.ex b/lib/chiya_web/live/note_show_live.ex index 45dd324..61ddf63 100644 --- a/lib/chiya_web/live/note_show_live.ex +++ b/lib/chiya_web/live/note_show_live.ex @@ -65,7 +65,7 @@ defmodule ChiyaWeb.NoteShowLive do image.featured && "border-theme-primary" ]} src={ChiyaWeb.Uploaders.NoteImage.url({image.path, image}, :thumb_dithered)} - /> + />
<.button phx-click="delete_image" phx-value-id={image.id} data-confirm="Are you sure?">