feat: category filter includes subcategories

This commit is contained in:
Schuwi
2025-09-19 20:28:12 +02:00
parent a0348c7df9
commit b68f8d92f7
2 changed files with 50 additions and 1 deletions

View File

@@ -143,6 +143,32 @@ defmodule ComponentsElixir.Inventory.Hierarchical do
end)
end
@doc """
Gets all descendant IDs for a given entity ID, including the entity itself.
This recursively finds all children, grandchildren, etc.
## Examples
iex> categories = [
...> %{id: 1, parent_id: nil},
...> %{id: 2, parent_id: 1},
...> %{id: 3, parent_id: 2},
...> %{id: 4, parent_id: 1}
...> ]
iex> Hierarchical.descendant_ids(categories, 1, &(&1.parent_id))
[1, 2, 3, 4]
"""
def descendant_ids(entities, entity_id, parent_id_accessor_fn) do
[entity_id | get_descendant_ids_recursive(entities, entity_id, parent_id_accessor_fn)]
end
defp get_descendant_ids_recursive(entities, parent_id, parent_id_accessor_fn) do
children = child_entities(entities, parent_id, parent_id_accessor_fn)
Enum.flat_map(children, fn child ->
[child.id | get_descendant_ids_recursive(entities, child.id, parent_id_accessor_fn)]
end)
end
@doc """
Generates display name for entity including parent context.
For dropdown displays: "Parent > Child"