Auto open details when searched items have single result

This commit is contained in:
Harshad Sharma 2024-05-16 08:45:32 +05:30
parent 48b34fd6fc
commit c1c428d524
3 changed files with 61 additions and 46 deletions

View file

@ -42,8 +42,22 @@ defmodule FreediveWeb.LiliformLive do
FreediveWeb.LiliformLive.get_active_filter(socket.assigns.filters)
)
items = search(filtered_items, query)
socket = assign(socket, :items, items)
searched_items = search(filtered_items, query)
socket = assign(socket, :items, searched_items)
socket =
case Kernel.length(Map.keys(searched_items)) == 1 do
true ->
IO.inspect(Map.keys(searched_items) |> List.first())
assign(socket, %{
selected_item: Map.get(searched_items, Map.keys(searched_items) |> List.first()),
details: true
})
false ->
assign(socket, %{selected_item: nil, details: nil})
end
{:noreply, socket}
end

View file

@ -3,7 +3,7 @@ defmodule FreediveWeb.ServiceLive do
def render(assigns) do
~H"""
<.page name="Services" filters={@filters}>
<.page name="Services" filters={@filters} details={@details}>
<FreediveWeb.ServiceLive.Components.items_block
items={@items}
selected_item={@selected_item}

View file

@ -11,6 +11,7 @@ defmodule Liliform.Page do
attr :name, :string, required: true, doc: "page name"
attr :class, :string, default: "", doc: "additional classes"
attr :filters, :list, default: [], doc: "filters"
attr :details, :string, default: nil, doc: "details"
attr :query, :string, default: "", doc: "search query"
attr :rest, :global
@ -23,12 +24,12 @@ defmodule Liliform.Page do
~H"""
<.block class="px-2 py-4">
<.panel class={@class} {@rest} is-info>
<.panel_heading>
<%= @name %>
</.panel_heading>
<%= if @details == nil do %>
<.panel_tabs is-hidden-mobile>
<%= for filter <- @filters do %>
<a
@ -53,7 +54,7 @@ defmodule Liliform.Page do
</a>
<% end %>
</.panel_tabs>
<% end %>
<.panel_block>
<.control has-icons-left>
<input