diff --git a/lib/chiya_web/controllers/note_controller.ex b/lib/chiya_web/controllers/note_controller.ex index f489f06..48cdb4c 100644 --- a/lib/chiya_web/controllers/note_controller.ex +++ b/lib/chiya_web/controllers/note_controller.ex @@ -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) diff --git a/lib/chiya_web/controllers/note_html.ex b/lib/chiya_web/controllers/note_html.ex index fa20b65..517fdd9 100644 --- a/lib/chiya_web/controllers/note_html.ex +++ b/lib/chiya_web/controllers/note_html.ex @@ -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 diff --git a/lib/chiya_web/controllers/note_html/edit.html.heex b/lib/chiya_web/controllers/note_html/edit.html.heex index d0b38f6..bb40517 100644 --- a/lib/chiya_web/controllers/note_html/edit.html.heex +++ b/lib/chiya_web/controllers/note_html/edit.html.heex @@ -7,6 +7,7 @@ changeset={@changeset} action={~p"/admin/notes/#{@note}"} channels={@channels} + selected_channels={@selected_channels} tags={@tags} /> diff --git a/lib/chiya_web/controllers/note_html/new.html.heex b/lib/chiya_web/controllers/note_html/new.html.heex index f916670..9058b0b 100644 --- a/lib/chiya_web/controllers/note_html/new.html.heex +++ b/lib/chiya_web/controllers/note_html/new.html.heex @@ -3,6 +3,11 @@ <:subtitle>Use this form to manage note records in your database. -<.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 diff --git a/lib/chiya_web/controllers/note_html/note_form.html.heex b/lib/chiya_web/controllers/note_html/note_form.html.heex index 947e0cc..d853c62 100644 --- a/lib/chiya_web/controllers/note_html/note_form.html.heex +++ b/lib/chiya_web/controllers/note_html/note_form.html.heex @@ -21,7 +21,7 @@ label="Channel" multiple={true} options={@channels} - value={selected_channels(@changeset)} + value={@selected_channels} /> <:actions>