This commit is contained in:
Ondřej 2024-08-09 17:07:50 +02:00
parent fef1f17938
commit 51092490b8
2 changed files with 11 additions and 5 deletions

View file

@ -61,11 +61,15 @@ defmodule ChoreTracker.Chores do
end end
def change_chore(%Chore{} = chore, attrs \\ %{}) do def change_chore(%Chore{} = chore, attrs \\ %{}) do
assignees = Accounts.list_users_by_id(attrs["assignee_ids"])
chore chore
|> Chore.changeset(attrs) |> Chore.changeset(attrs)
|> Changeset.put_assoc(:assignees, assignees) |> then(fn changeset ->
if ids = attrs["assignee_ids"] do
Changeset.put_assoc(changeset, :assignees, Accounts.list_users_by_id(ids))
else
changeset
end
end)
end end
def change_chore_assignee(%Chore{} = chore, user) do def change_chore_assignee(%Chore{} = chore, user) do

View file

@ -75,6 +75,8 @@ defmodule ChoreTrackerWeb.ChoreLive.FormComponent do
end end
defp save_chore(socket, :edit, chore_params) do defp save_chore(socket, :edit, chore_params) do
chore_params = Map.merge(%{"assignee_ids" => []}, chore_params)
case Chores.update_chore(socket.assigns.chore, chore_params) do case Chores.update_chore(socket.assigns.chore, chore_params) do
{:ok, chore} -> {:ok, chore} ->
notify_parent({:saved, chore}) notify_parent({:saved, chore})
@ -109,8 +111,8 @@ defmodule ChoreTrackerWeb.ChoreLive.FormComponent do
defp user_options(form) do defp user_options(form) do
existing_ids = existing_ids =
form.source form.source
|> Ecto.Changeset.get_change(:assignees, []) |> Ecto.Changeset.get_field(:assignees)
|> Enum.map(& &1.data.id) |> Enum.map(& &1.id)
Accounts.list_users() Accounts.list_users()
|> Enum.map(&[key: Accounts.display_user(&1), value: &1.id, selected: &1.id in existing_ids]) |> Enum.map(&[key: Accounts.display_user(&1), value: &1.id, selected: &1.id in existing_ids])