I have a bunch of components and a list of data with different “types”. I want to render the appropriate component depending on the type of the data.
This is basically the code i am trying, but i’m not sure the best way to do it with heex.
for {agg, results} <- @data_list do
case agg.type do
"price_stats" -> ~H"""
<.live_component module={ComponentTestWeb.Components.StatsFacet} id={agg.internal_name} title={agg.title} items={results} />
"""
_ -> ~H"<.live_component module={ComponentTestWeb.Components.KeywordFacet} id={agg.internal_name} title={agg.title} items={results} />"
end
end
How should I do this? I have tried it in the template, but i couldn’t find a good example of case statements in the template. So then i thought i’d do it as a functional component, but it’s not clear to me how to set the assigns correctly. For example, this says i’m doing the wrong thing for change tracking;
def show_facets(assigns) do
for {agg, results} <- assigns.agg_results do
case agg.type do
"price_stats" -> ~H"""
<.live_component module={ComponentTestWeb.Components.StatsFacet} id={agg.internal_name} title={agg.title} items={results} />
"""
_ -> ~H"<.live_component module={ComponentTestWeb.Components.KeywordFacet} id={agg.internal_name} title={agg.title} items={results} />"
end
end
end