From d22a48a514fd5bb4c0a4f6ee41e681c5c8a50e3f Mon Sep 17 00:00:00 2001 From: Darren Black Date: Tue, 28 Mar 2023 18:21:41 +1100 Subject: [PATCH] Fix remove_component (#86) --- lib/ash_phoenix/filter_form/filter_form.ex | 8 +++---- test/filter_form_test.exs | 25 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/ash_phoenix/filter_form/filter_form.ex b/lib/ash_phoenix/filter_form/filter_form.ex index ae83c33..7ac16f3 100644 --- a/lib/ash_phoenix/filter_form/filter_form.ex +++ b/lib/ash_phoenix/filter_form/filter_form.ex @@ -903,11 +903,11 @@ defmodule AshPhoenix.FilterForm do |> set_validity() end - @doc "Removes the group *or* component with the given id" - def remove_component(form, group_or_component_id) do + @doc "Removes the group *or* predicate with the given id" + def remove_component(form, group_or_predicate_id) do form - |> remove_group(group_or_component_id) - |> remove_component(group_or_component_id) + |> remove_group(group_or_predicate_id) + |> remove_predicate(group_or_predicate_id) end defp remove_if_empty(form, false), do: [form] diff --git a/test/filter_form_test.exs b/test/filter_form_test.exs index c876c13..9dc62ab 100644 --- a/test/filter_form_test.exs +++ b/test/filter_form_test.exs @@ -61,6 +61,31 @@ defmodule AshPhoenix.FilterFormTest do } = form end + test "groups and predicates can be removed with remove_component" do + form = FilterForm.new(Post) + + {form, group_id} = FilterForm.add_group(form, operator: :or, return_id?: true) + + {form, predicate_id} = + FilterForm.add_predicate(form, :title, :eq, "new post", to: group_id, return_id?: true) + + form = FilterForm.remove_component(form, predicate_id) + + assert %FilterForm{ + components: [ + %FilterForm{ + components: [] + } + ] + } = form + + form = FilterForm.remove_component(form, group_id) + + assert %FilterForm{ + components: [] + } = form + end + test "with `remove_empty_groups?: true` empty groups are removed on component removal" do form = FilterForm.new(Post, remove_empty_groups?: true)