From a05f289b5ef003c1208a3b348b8c21e56114babc Mon Sep 17 00:00:00 2001 From: Harshad Sharma Date: Thu, 16 May 2024 05:52:56 +0530 Subject: [PATCH] Add filters --- lib/freedive_web/live/liliform_live.ex | 52 ++++++++++++++++++++++++-- lib/freedive_web/live/service_live.ex | 4 +- lib/liliform/page.ex | 24 ++++++++++-- 3 files changed, 71 insertions(+), 9 deletions(-) diff --git a/lib/freedive_web/live/liliform_live.ex b/lib/freedive_web/live/liliform_live.ex index 202a458..5379d0c 100644 --- a/lib/freedive_web/live/liliform_live.ex +++ b/lib/freedive_web/live/liliform_live.ex @@ -7,18 +7,29 @@ defmodule FreediveWeb.LiliformLive do %{ title: "All", icon: "all", - active: false + active: false, + key: :all } ] def(mount(_params, _session, socket)) do items = items() + filters = filters() socket = assign(socket, :opts, Keyword.get(unquote(opts), :opts, [])) socket = assign(socket, :items_all, items) - socket = assign(socket, :items, items) - socket = assign(socket, :filters, @filters_all ++ filters()) + + socket = + assign( + socket, + :items, + FreediveWeb.LiliformLive.filter( + items, + FreediveWeb.LiliformLive.get_active_filter(filters) + ) + ) + + socket = assign(socket, :filters, @filters_all ++ filters) socket = assign(socket, :query, "") - IO.inspect(socket, label: "socket") {:ok, socket} end @@ -27,10 +38,43 @@ defmodule FreediveWeb.LiliformLive do socket = assign(socket, :items, items) {:noreply, socket} end + + def(handle_event("filter", %{"key" => key}, socket)) do + key = String.to_existing_atom(key) + items = FreediveWeb.LiliformLive.filter(socket.assigns.items_all, key) + socket = assign(socket, :items, items) + + socket = + assign( + socket, + :filters, + Enum.map(socket.assigns.filters, fn filter -> + Map.put(filter, :active, filter.key == key) + end) + ) + + {:noreply, socket} + end end end @callback items() :: [map] @callback filters() :: [map] @callback search(query :: String.t()) :: [map] + + def filter(items, key) do + case key do + :all -> + items + + _ -> + Enum.filter(items, fn item -> + Map.get(item, key) == true + end) + end + end + + def get_active_filter(filters) do + filters |> Enum.find(& &1.active) |> Map.get(:key) + end end diff --git a/lib/freedive_web/live/service_live.ex b/lib/freedive_web/live/service_live.ex index 4a2e7b6..72c306d 100644 --- a/lib/freedive_web/live/service_live.ex +++ b/lib/freedive_web/live/service_live.ex @@ -52,13 +52,13 @@ defmodule FreediveWeb.ServiceLive do [ %{ title: "Running", - key: "running", + key: :running, icon: "circle-play", active: true }, %{ title: "Enabled", - key: "enabled", + key: :enabled, icon: "circle-check", active: false } diff --git a/lib/liliform/page.ex b/lib/liliform/page.ex index aed8cfd..c9083e8 100644 --- a/lib/liliform/page.ex +++ b/lib/liliform/page.ex @@ -28,13 +28,24 @@ defmodule Liliform.Page do <.panel_tabs is-hidden-mobile> <%= for filter <- @filters do %> - <%= filter.title %> + + <%= filter.title %> + <% end %> <.panel_tabs is-hidden-tablet> <%= for filter <- @filters do %> - + <.icon for={filter.icon} color="auto" /> <% end %> @@ -42,7 +53,14 @@ defmodule Liliform.Page do <.panel_block> <.control has-icons-left> - + <.icon for="search" size="1.5rem" aria-hidden="true" is-left />