Fix search

This commit is contained in:
Harshad Sharma 2024-05-16 08:31:40 +05:30
parent 74615f5ac7
commit 48b34fd6fc
4 changed files with 54 additions and 19 deletions

View file

@ -7,3 +7,7 @@
height: 256px; height: 256px;
width: 256px; width: 256px;
} }
.is-fullwidth {
width: 100%;
}

View file

@ -36,7 +36,13 @@ defmodule FreediveWeb.LiliformLive do
end end
def(handle_event("search", %{"value" => query}, socket)) do def(handle_event("search", %{"value" => query}, socket)) do
items = search(query) filtered_items =
FreediveWeb.LiliformLive.filter(
socket.assigns.items_all,
FreediveWeb.LiliformLive.get_active_filter(socket.assigns.filters)
)
items = search(filtered_items, query)
socket = assign(socket, :items, items) socket = assign(socket, :items, items)
{:noreply, socket} {:noreply, socket}
end end
@ -50,9 +56,7 @@ defmodule FreediveWeb.LiliformLive do
assign( assign(
socket, socket,
:filters, :filters,
Enum.map(socket.assigns.filters, fn filter -> FreediveWeb.LiliformLive.update_active_filter(socket.assigns.filters, key)
Map.put(filter, :active, filter.key == key)
end)
) )
{:noreply, socket} {:noreply, socket}
@ -82,6 +86,7 @@ defmodule FreediveWeb.LiliformLive do
assign(socket, %{selected_item: item, details: nil}) assign(socket, %{selected_item: item, details: nil})
end end
end end
{:noreply, socket} {:noreply, socket}
end end
end end
@ -89,7 +94,7 @@ defmodule FreediveWeb.LiliformLive do
@callback items() :: [map] @callback items() :: [map]
@callback filters() :: [map] @callback filters() :: [map]
@callback search(query :: String.t()) :: [map] @callback search(items :: [map], query :: String.t()) :: [map]
def filter(items, key) do def filter(items, key) do
case key do case key do
@ -97,11 +102,20 @@ defmodule FreediveWeb.LiliformLive do
items items
_ -> _ ->
items |> Enum.filter(fn {_name, item} -> item[key] == true end) items
|> Enum.filter(fn {_name, item} -> item[key] == true end)
|> Enum.into(%{}, fn {name, item} -> {name, item} end)
end end
end end
def get_active_filter(filters) do def get_active_filter(filters) do
filters |> Enum.find(& &1.active) |> Map.get(:key) filters |> Enum.find(& &1.active) |> Map.get(:key)
end end
def update_active_filter(filters, key) do
filters
|> Enum.map(fn filter ->
Map.put(filter, :active, filter.key == key)
end)
end
end end

View file

@ -43,8 +43,7 @@ defmodule FreediveWeb.ServiceLive do
name: "httpd", name: "httpd",
path: "/services/httpd", path: "/services/httpd",
icon: "globe", icon: "globe",
description: description: "Hypertext Transfer Protocol Daemon",
"Hypertext Transfer Protocol Daemon",
enabled: false, enabled: false,
running: false running: false
} }
@ -68,11 +67,12 @@ defmodule FreediveWeb.ServiceLive do
] ]
end end
def search(query) do def search(items, query) do
items() items
|> Enum.filter(fn item -> |> Enum.filter(fn {name, _item} ->
String.contains?(String.downcase(item.name), String.downcase(query)) String.contains?(String.downcase(name), String.downcase(query))
end) end)
|> Enum.into(%{}, fn {name, item} -> {name, item} end)
end end
end end
@ -103,13 +103,24 @@ defmodule FreediveWeb.ServiceLive.Components do
</.link> </.link>
<.panel_block_div> <.panel_block_div>
<div class="columns"> <div class="column is-narrow">
<.title class="column"> <.icon for={@selected_item.name} color="auto" aria-hidden="true" />
<%= @selected_item.name %> </div>
</.title> <div class="columns is-vcentered is-fullwidth">
<.subtitle class="column"> <div class="column is-narrow">
<%= @selected_item.description %> <.title>
</.subtitle> <%= @selected_item.name %>
</.title>
</div>
<div class="column">
<.subtitle>
<%= @selected_item.description %>
</.subtitle>
</div>
<div class="column is-narrow">
<.icon for="running" color="auto" aria-hidden="true" />
<.icon for="enabled" color="auto" aria-hidden="true" />
</div>
</div> </div>
</.panel_block_div> </.panel_block_div>
""" """

View file

@ -135,6 +135,12 @@ defmodule Liliform.Icon do
"system" -> :bot "system" -> :bot
"account" -> :user "account" -> :user
"all" -> :infinity "all" -> :infinity
"pf" -> :shield
"sshd" -> :lock
"ntpdate" -> :clock
"httpd" -> :globe
"running" -> :circle_play
"enabled" -> :circle_check
lucide_name -> String.to_atom(lucide_name) lucide_name -> String.to_atom(lucide_name)
end end
end end