2024-05-15 20:53:00 +02:00
|
|
|
defmodule FreediveWeb.LiliformLive do
|
2024-05-16 16:03:26 +02:00
|
|
|
@callback items() :: map
|
|
|
|
@callback filters() :: [map]
|
|
|
|
@callback search(items :: map, query :: String.t()) :: map
|
|
|
|
|
2024-05-15 23:59:20 +02:00
|
|
|
defmacro __using__(opts) do
|
|
|
|
quote location: :keep, bind_quoted: [opts: opts] do
|
2024-05-15 22:18:55 +02:00
|
|
|
use FreediveWeb, :live_view
|
2024-05-18 08:58:34 +02:00
|
|
|
require Logger
|
|
|
|
|
2024-05-15 23:59:20 +02:00
|
|
|
@behaviour FreediveWeb.LiliformLive
|
2024-05-16 01:47:37 +02:00
|
|
|
@filters_all [
|
2024-05-16 01:37:36 +02:00
|
|
|
%{
|
|
|
|
title: "All",
|
|
|
|
icon: "all",
|
2024-05-16 02:22:56 +02:00
|
|
|
active: false,
|
|
|
|
key: :all
|
2024-05-16 01:37:36 +02:00
|
|
|
}
|
|
|
|
]
|
2024-05-15 23:59:20 +02:00
|
|
|
|
2024-05-16 01:37:36 +02:00
|
|
|
def(mount(_params, _session, socket)) do
|
2024-05-16 01:47:37 +02:00
|
|
|
items = items()
|
2024-05-16 02:22:56 +02:00
|
|
|
filters = filters()
|
2024-05-16 01:37:36 +02:00
|
|
|
socket = assign(socket, :opts, Keyword.get(unquote(opts), :opts, []))
|
2024-05-16 01:47:37 +02:00
|
|
|
socket = assign(socket, :items_all, items)
|
2024-05-16 03:56:54 +02:00
|
|
|
socket = assign(socket, :selected_item, nil)
|
|
|
|
socket = assign(socket, :details, nil)
|
2024-05-16 02:22:56 +02:00
|
|
|
|
|
|
|
socket =
|
|
|
|
assign(
|
|
|
|
socket,
|
|
|
|
:items,
|
|
|
|
FreediveWeb.LiliformLive.filter(
|
|
|
|
items,
|
|
|
|
FreediveWeb.LiliformLive.get_active_filter(filters)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
socket = assign(socket, :filters, @filters_all ++ filters)
|
2024-05-16 01:37:36 +02:00
|
|
|
socket = assign(socket, :query, "")
|
2024-05-18 08:58:34 +02:00
|
|
|
|
|
|
|
if connected?(socket) do
|
|
|
|
apply(__MODULE__, :on_mount_connected, [socket])
|
|
|
|
else
|
|
|
|
{:ok, socket}
|
|
|
|
end
|
2024-05-15 23:59:20 +02:00
|
|
|
end
|
2024-05-16 01:37:36 +02:00
|
|
|
|
2024-05-16 01:47:37 +02:00
|
|
|
def(handle_event("search", %{"value" => query}, socket)) do
|
2024-05-16 05:01:40 +02:00
|
|
|
filtered_items =
|
|
|
|
FreediveWeb.LiliformLive.filter(
|
|
|
|
socket.assigns.items_all,
|
|
|
|
FreediveWeb.LiliformLive.get_active_filter(socket.assigns.filters)
|
|
|
|
)
|
|
|
|
|
2024-05-16 05:15:32 +02:00
|
|
|
searched_items = search(filtered_items, query)
|
2024-05-19 03:59:33 +02:00
|
|
|
socket = assign(socket, %{items: searched_items, query: query})
|
2024-05-16 05:15:32 +02:00
|
|
|
|
|
|
|
socket =
|
|
|
|
case Kernel.length(Map.keys(searched_items)) == 1 do
|
|
|
|
true ->
|
|
|
|
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
|
|
|
|
|
2024-05-16 01:47:37 +02:00
|
|
|
{:noreply, socket}
|
|
|
|
end
|
2024-05-16 02:22:56 +02:00
|
|
|
|
|
|
|
def(handle_event("filter", %{"key" => key}, socket)) do
|
|
|
|
key = String.to_existing_atom(key)
|
2024-05-19 03:59:33 +02:00
|
|
|
filtered_items = FreediveWeb.LiliformLive.filter(socket.assigns.items_all, key)
|
|
|
|
searched_items = search(filtered_items, socket.assigns.query)
|
|
|
|
socket = assign(socket, :items, searched_items)
|
2024-05-16 02:22:56 +02:00
|
|
|
|
|
|
|
socket =
|
|
|
|
assign(
|
|
|
|
socket,
|
|
|
|
:filters,
|
2024-05-16 05:01:40 +02:00
|
|
|
FreediveWeb.LiliformLive.update_active_filter(socket.assigns.filters, key)
|
2024-05-16 02:22:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
2024-05-16 03:56:54 +02:00
|
|
|
|
|
|
|
def(handle_event("tap", %{"name" => item_name}, socket)) do
|
|
|
|
# 1 tap to select, 2 taps for details
|
|
|
|
item = socket.assigns.items[item_name]
|
2024-05-16 04:04:09 +02:00
|
|
|
|
2024-05-16 03:56:54 +02:00
|
|
|
socket =
|
|
|
|
case socket.assigns.selected_item do
|
|
|
|
nil ->
|
|
|
|
assign(socket, :selected_item, item)
|
|
|
|
|
|
|
|
selected ->
|
|
|
|
case selected.name == item_name do
|
|
|
|
true ->
|
|
|
|
case socket.assigns.details do
|
|
|
|
nil ->
|
|
|
|
assign(socket, :details, true)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
assign(socket, %{selected_item: item, details: nil})
|
|
|
|
end
|
|
|
|
|
|
|
|
false ->
|
|
|
|
assign(socket, %{selected_item: item, details: nil})
|
|
|
|
end
|
|
|
|
end
|
2024-05-16 05:01:40 +02:00
|
|
|
|
2024-05-16 03:56:54 +02:00
|
|
|
{:noreply, socket}
|
|
|
|
end
|
2024-05-16 15:59:56 +02:00
|
|
|
|
|
|
|
def(handle_params(params, _url, socket)) do
|
|
|
|
socket = assign(socket, :query, Map.get(params, "search", ""))
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
2024-05-15 22:18:55 +02:00
|
|
|
end
|
2024-05-15 20:53:00 +02:00
|
|
|
end
|
2024-05-15 23:59:20 +02:00
|
|
|
|
2024-05-16 02:22:56 +02:00
|
|
|
def filter(items, key) do
|
|
|
|
case key do
|
|
|
|
:all ->
|
|
|
|
items
|
|
|
|
|
|
|
|
_ ->
|
2024-05-16 05:01:40 +02:00
|
|
|
items
|
|
|
|
|> Enum.filter(fn {_name, item} -> item[key] == true end)
|
|
|
|
|> Enum.into(%{}, fn {name, item} -> {name, item} end)
|
2024-05-16 02:22:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_active_filter(filters) do
|
|
|
|
filters |> Enum.find(& &1.active) |> Map.get(:key)
|
|
|
|
end
|
2024-05-16 05:01:40 +02:00
|
|
|
|
|
|
|
def update_active_filter(filters, key) do
|
|
|
|
filters
|
|
|
|
|> Enum.map(fn filter ->
|
|
|
|
Map.put(filter, :active, filter.key == key)
|
|
|
|
end)
|
|
|
|
end
|
2024-05-15 20:53:00 +02:00
|
|
|
end
|