Consolidate search reset logic

This commit is contained in:
Claudio Ortolina
2025-07-06 09:55:41 +01:00
parent 3787736e39
commit 0d855167ff
@@ -9,11 +9,8 @@ defmodule MusicLibraryWeb.UniversalSearchLive.Index do
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
{:ok, {:ok,
socket socket
|> assign(:search_query, "")
|> assign(:search_results, %{collection: [], wishlist: [], artists: []})
|> assign(:search_counts, %{collection_count: 0, wishlist_count: 0, artists_count: 0})
|> assign(:show_modal, false) |> assign(:show_modal, false)
|> assign(:total_results, 0)} |> reset()}
end end
@impl true @impl true
@@ -26,10 +23,7 @@ defmodule MusicLibraryWeb.UniversalSearchLive.Index do
{:noreply, {:noreply,
socket socket
|> assign(:show_modal, false) |> assign(:show_modal, false)
|> assign(:search_query, "") |> reset()}
|> assign(:search_results, %{collection: [], wishlist: [], artists: []})
|> assign(:search_counts, %{collection_count: 0, wishlist_count: 0, artists_count: 0})
|> assign(:total_results, 0)}
end end
@impl true @impl true
@@ -40,9 +34,7 @@ defmodule MusicLibraryWeb.UniversalSearchLive.Index do
"" -> "" ->
{:noreply, {:noreply,
socket socket
|> assign(:search_results, %{collection: [], wishlist: [], artists: []}) |> reset()}
|> assign(:search_counts, %{collection_count: 0, wishlist_count: 0, artists_count: 0})
|> assign(:total_results, 0)}
_query -> _query ->
send(self(), {:perform_search, query}) send(self(), {:perform_search, query})
@@ -105,6 +97,14 @@ defmodule MusicLibraryWeb.UniversalSearchLive.Index do
|> push_navigate(to: ~p"/wishlist?query=#{query}")} |> push_navigate(to: ~p"/wishlist?query=#{query}")}
end end
defp reset(socket) do
socket
|> assign(:search_query, "")
|> assign(:search_results, %{collection: [], wishlist: [], artists: []})
|> assign(:search_counts, %{collection_count: 0, wishlist_count: 0, artists_count: 0})
|> assign(:total_results, 0)
end
@impl true @impl true
def handle_info({:perform_search, query}, socket) do def handle_info({:perform_search, query}, socket) do
# Only search if the query hasn't changed (debouncing) # Only search if the query hasn't changed (debouncing)