add selected_channels to note_form to properly set default channel
This commit is contained in:
parent
aa36502ddb
commit
92e548e96c
5 changed files with 34 additions and 9 deletions
|
@ -23,21 +23,22 @@ defmodule ChiyaWeb.NoteController do
|
|||
end
|
||||
|
||||
def new(conn, _params) do
|
||||
settings = Chiya.Site.get_settings()
|
||||
default_channels = get_default_channels(conn)
|
||||
|
||||
changeset =
|
||||
%Note{channels: [settings.default_channel]}
|
||||
%Note{}
|
||||
|> Notes.preload_note()
|
||||
|> Notes.change_note()
|
||||
|
||||
render(conn, :new,
|
||||
changeset: changeset,
|
||||
channels: to_channel_options(),
|
||||
selected_channels: default_channels,
|
||||
tags: []
|
||||
)
|
||||
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
|
||||
|
@ -46,8 +47,15 @@ defmodule ChiyaWeb.NoteController do
|
|||
|> put_flash(:info, "Note created successfully.")
|
||||
|> redirect(to: ~p"/admin/notes/#{note}")
|
||||
|
||||
# TODO: set channels from changeset when error happened?
|
||||
|
||||
{: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
|
||||
|
||||
|
@ -60,11 +68,13 @@ defmodule ChiyaWeb.NoteController do
|
|||
def edit(conn, %{"id" => id}) do
|
||||
note = Notes.get_note_preloaded!(id)
|
||||
changeset = Notes.change_note(note)
|
||||
selected_channels = Enum.map(note.channels, fn c -> c.id end)
|
||||
|
||||
render(conn, :edit,
|
||||
note: note,
|
||||
changeset: changeset,
|
||||
channels: to_channel_options(),
|
||||
selected_channels: selected_channels,
|
||||
tags: note.tags
|
||||
)
|
||||
end
|
||||
|
@ -79,11 +89,14 @@ defmodule ChiyaWeb.NoteController do
|
|||
|> put_flash(:info, "Note updated successfully.")
|
||||
|> redirect(to: ~p"/admin/notes/#{note}")
|
||||
|
||||
# TODO: set channels from changeset when error happened?
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
render(conn, :edit,
|
||||
note: note,
|
||||
changeset: changeset,
|
||||
channels: to_channel_options(),
|
||||
selected_channels: nil,
|
||||
tags: note.tags
|
||||
)
|
||||
end
|
||||
|
@ -218,6 +231,14 @@ defmodule ChiyaWeb.NoteController do
|
|||
)
|
||||
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
|
||||
selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1)
|
||||
|
||||
|
|
|
@ -10,11 +10,9 @@ defmodule ChiyaWeb.NoteHTML do
|
|||
attr(:action, :string, required: true)
|
||||
attr(:channels, :list, required: true)
|
||||
attr(:tags, :list, required: true)
|
||||
attr(:selected_channels, :list, required: true)
|
||||
|
||||
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)
|
||||
end
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
changeset={@changeset}
|
||||
action={~p"/admin/notes/#{@note}"}
|
||||
channels={@channels}
|
||||
selected_channels={@selected_channels}
|
||||
tags={@tags}
|
||||
/>
|
||||
|
||||
|
|
|
@ -3,6 +3,11 @@
|
|||
<:subtitle>Use this form to manage note records in your database.</:subtitle>
|
||||
</.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>
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
label="Channel"
|
||||
multiple={true}
|
||||
options={@channels}
|
||||
value={selected_channels(@changeset)}
|
||||
value={@selected_channels}
|
||||
/>
|
||||
|
||||
<:actions>
|
||||
|
|
Loading…
Reference in a new issue