From 51092490b8754c9d7950e6dea86b5a2269f9b40b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20N=C3=BDvlt?= Date: Fri, 9 Aug 2024 17:07:50 +0200 Subject: [PATCH] Fix --- lib/chore_tracker/chores.ex | 10 +++++++--- .../live/chore_live/form_component.ex | 6 ++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/chore_tracker/chores.ex b/lib/chore_tracker/chores.ex index 9fed592..625bfea 100644 --- a/lib/chore_tracker/chores.ex +++ b/lib/chore_tracker/chores.ex @@ -61,11 +61,15 @@ defmodule ChoreTracker.Chores do end def change_chore(%Chore{} = chore, attrs \\ %{}) do - assignees = Accounts.list_users_by_id(attrs["assignee_ids"]) - chore |> 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 def change_chore_assignee(%Chore{} = chore, user) do diff --git a/lib/chore_tracker_web/live/chore_live/form_component.ex b/lib/chore_tracker_web/live/chore_live/form_component.ex index 981a968..72dcefe 100644 --- a/lib/chore_tracker_web/live/chore_live/form_component.ex +++ b/lib/chore_tracker_web/live/chore_live/form_component.ex @@ -75,6 +75,8 @@ defmodule ChoreTrackerWeb.ChoreLive.FormComponent do end 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 {:ok, chore} -> notify_parent({:saved, chore}) @@ -109,8 +111,8 @@ defmodule ChoreTrackerWeb.ChoreLive.FormComponent do defp user_options(form) do existing_ids = form.source - |> Ecto.Changeset.get_change(:assignees, []) - |> Enum.map(& &1.data.id) + |> Ecto.Changeset.get_field(:assignees) + |> Enum.map(& &1.id) Accounts.list_users() |> Enum.map(&[key: Accounts.display_user(&1), value: &1.id, selected: &1.id in existing_ids])