apply default layout on note creation #136

Merged
inhji merged 2 commits from devel into main 2023-06-20 20:30:04 +02:00
7 changed files with 23 additions and 13 deletions

View file

@ -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])

View file

@ -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",

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -37,5 +37,8 @@
<:item title="Default Channel">
<%= if @setting.default_channel, do: @setting.default_channel.name %>
</:item>
<:item title="Micropub Default Channel">
<%= if @setting.micropub_channel, do: @setting.micropub_channel.name %>
</:item>
</.list>
<% end %>

View file

@ -65,7 +65,7 @@ defmodule ChiyaWeb.NoteShowLive do
image.featured && "border-theme-primary"
]}
src={ChiyaWeb.Uploaders.NoteImage.url({image.path, image}, :thumb_dithered)}
/>
/>
</a>
<div class="flex justify-between">
<.button phx-click="delete_image" phx-value-id={image.id} data-confirm="Are you sure?">