add selected_channels to note_form to properly set default channel

This commit is contained in:
Inhji 2023-06-23 06:38:31 +02:00
parent aa36502ddb
commit 92e548e96c
5 changed files with 34 additions and 9 deletions

View file

@ -23,21 +23,22 @@ defmodule ChiyaWeb.NoteController do
end end
def new(conn, _params) do def new(conn, _params) do
settings = Chiya.Site.get_settings() default_channels = get_default_channels(conn)
changeset = changeset =
%Note{channels: [settings.default_channel]} %Note{}
|> Notes.preload_note()
|> Notes.change_note() |> Notes.change_note()
render(conn, :new, render(conn, :new,
changeset: changeset, changeset: changeset,
channels: to_channel_options(), channels: to_channel_options(),
selected_channels: default_channels,
tags: [] tags: []
) )
end end
def create(conn, %{"note" => note_params}) do def create(conn, %{"note" => note_params}) do
IO.inspect(note_params)
note_params = from_channel_ids(note_params) note_params = from_channel_ids(note_params)
case Notes.create_note(note_params) do case Notes.create_note(note_params) do
@ -46,8 +47,15 @@ defmodule ChiyaWeb.NoteController do
|> put_flash(:info, "Note created successfully.") |> put_flash(:info, "Note created successfully.")
|> redirect(to: ~p"/admin/notes/#{note}") |> redirect(to: ~p"/admin/notes/#{note}")
# TODO: set channels from changeset when error happened?
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
render(conn, :new, changeset: changeset, channels: to_channel_options(), tags: []) render(conn, :new,
changeset: changeset,
channels: to_channel_options(),
selected_channels: nil,
tags: []
)
end end
end end
@ -60,11 +68,13 @@ defmodule ChiyaWeb.NoteController do
def edit(conn, %{"id" => id}) do def edit(conn, %{"id" => id}) do
note = Notes.get_note_preloaded!(id) note = Notes.get_note_preloaded!(id)
changeset = Notes.change_note(note) changeset = Notes.change_note(note)
selected_channels = Enum.map(note.channels, fn c -> c.id end)
render(conn, :edit, render(conn, :edit,
note: note, note: note,
changeset: changeset, changeset: changeset,
channels: to_channel_options(), channels: to_channel_options(),
selected_channels: selected_channels,
tags: note.tags tags: note.tags
) )
end end
@ -79,11 +89,14 @@ defmodule ChiyaWeb.NoteController do
|> put_flash(:info, "Note updated successfully.") |> put_flash(:info, "Note updated successfully.")
|> redirect(to: ~p"/admin/notes/#{note}") |> redirect(to: ~p"/admin/notes/#{note}")
# TODO: set channels from changeset when error happened?
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit, render(conn, :edit,
note: note, note: note,
changeset: changeset, changeset: changeset,
channels: to_channel_options(), channels: to_channel_options(),
selected_channels: nil,
tags: note.tags tags: note.tags
) )
end end
@ -218,6 +231,14 @@ defmodule ChiyaWeb.NoteController do
) )
end end
defp get_default_channels(conn = %Plug.Conn{}) do
if conn.assigns.settings.default_channel do
[conn.assigns.settings.default_channel.id]
else
[]
end
end
defp from_channel_ids(note_params) do defp from_channel_ids(note_params) do
selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1) selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1)

View file

@ -10,11 +10,9 @@ defmodule ChiyaWeb.NoteHTML do
attr(:action, :string, required: true) attr(:action, :string, required: true)
attr(:channels, :list, required: true) attr(:channels, :list, required: true)
attr(:tags, :list, required: true) attr(:tags, :list, required: true)
attr(:selected_channels, :list, required: true)
def note_form(assigns) def note_form(assigns)
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) def tags_to_string(tags), do: Enum.map_join(tags, ", ", fn t -> t.name end)
end end

View file

@ -7,6 +7,7 @@
changeset={@changeset} changeset={@changeset}
action={~p"/admin/notes/#{@note}"} action={~p"/admin/notes/#{@note}"}
channels={@channels} channels={@channels}
selected_channels={@selected_channels}
tags={@tags} tags={@tags}
/> />

View file

@ -3,6 +3,11 @@
<:subtitle>Use this form to manage note records in your database.</:subtitle> <:subtitle>Use this form to manage note records in your database.</:subtitle>
</.header> </.header>
<.note_form changeset={@changeset} action={~p"/admin/notes"} channels={@channels} tags={@tags} /> <.note_form
changeset={@changeset}
action={~p"/admin/notes"}
channels={@channels}
tags={@tags}
selected_channels={@selected_channels} />
<.back navigate={~p"/admin/notes"}>Back to notes</.back> <.back navigate={~p"/admin/notes"}>Back to notes</.back>

View file

@ -21,7 +21,7 @@
label="Channel" label="Channel"
multiple={true} multiple={true}
options={@channels} options={@channels}
value={selected_channels(@changeset)} value={@selected_channels}
/> />
<:actions> <:actions>