add search to tag index

This commit is contained in:
2022-12-03 20:35:54 -05:00
parent d743336868
commit 3ea4b77b67
25 changed files with 226 additions and 51 deletions

View File

@ -15,11 +15,38 @@ defmodule Cannery.Tags do
iex> list_tags(%User{id: 123})
[%Tag{}, ...]
iex> list_tags("cool", %User{id: 123})
[%Tag{name: "my cool tag"}, ...]
"""
@spec list_tags(User.t()) :: [Tag.t()]
def list_tags(%{id: user_id}),
@spec list_tags(search :: nil | String.t(), User.t()) :: [Tag.t()]
def list_tags(search \\ nil, user)
def list_tags(search, %{id: user_id}) when search |> is_nil() or search == "",
do: Repo.all(from t in Tag, where: t.user_id == ^user_id, order_by: t.name)
def list_tags(search, %{id: user_id}) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from t in Tag,
where: t.user_id == ^user_id,
where:
fragment(
"search @@ websearch_to_tsquery('english', ?)",
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(search, websearch_to_tsquery('english', ?), 4)",
^trimmed_search
)
}
)
end
@doc """
Gets a single tag.

View File

@ -9,27 +9,53 @@ defmodule CanneryWeb.TagLive.Index do
alias CanneryWeb.{Endpoint, ViewHelpers}
@impl true
def mount(_params, _session, socket), do: {:ok, socket |> display_tags()}
def mount(%{"search" => search}, _session, socket) do
{:ok, socket |> assign(:search, search) |> display_tags()}
end
def mount(_params, _session, socket) do
{:ok, socket |> assign(:search, nil) |> display_tags()}
end
@impl true
def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, apply_action(socket, live_action, params) |> display_tags}
{:noreply, apply_action(socket, live_action, params)}
end
defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, gettext("Edit Tag"))
|> assign(:tag, Tags.get_tag!(id, current_user))
|> assign(
page_title: gettext("Edit Tag"),
tag: Tags.get_tag!(id, current_user)
)
end
defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, gettext("New Tag"))
|> assign(:tag, %Tag{bg_color: ViewHelpers.random_color(), text_color: "#ffffff"})
|> assign(
page_title: gettext("New Tag"),
tag: %Tag{bg_color: ViewHelpers.random_color(), text_color: "#ffffff"}
)
end
defp apply_action(socket, :index, _params) do
socket |> assign(:page_title, gettext("Tags")) |> assign(:tag, nil)
socket
|> assign(
page_title: gettext("Tags"),
search: nil,
tag: nil
)
|> display_tags()
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(
page_title: gettext("Tags"),
search: search,
tag: nil
)
|> display_tags()
end
@impl true
@ -39,7 +65,16 @@ defmodule CanneryWeb.TagLive.Index do
{:noreply, socket |> put_flash(:info, prompt) |> display_tags()}
end
defp display_tags(%{assigns: %{current_user: current_user}} = socket) do
socket |> assign(tags: Tags.list_tags(current_user))
@impl true
def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
{:noreply, socket |> push_patch(to: Routes.tag_index_path(Endpoint, :index))}
end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
{:noreply, socket |> push_patch(to: Routes.tag_index_path(Endpoint, :search, search_term))}
end
defp display_tags(%{assigns: %{search: search, current_user: current_user}} = socket) do
socket |> assign(tags: Tags.list_tags(search, current_user))
end
end

View File

@ -5,7 +5,7 @@
<p class="title text-md text-primary-600">
<%= gettext("Tags can be added to your containers to help you organize") %>
</p>
<%= if @tags |> Enum.empty?() do %>
<%= if @tags |> Enum.empty?() and @search |> is_nil() do %>
<h2 class="title text-xl text-primary-600">
<%= gettext("No tags") %>
<%= display_emoji("😔") %>
@ -19,7 +19,33 @@
<%= dgettext("actions", "New Tag") %>
</.link>
<% end %>
<div class="w-full flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 max-w-xl">
<.form
:let={f}
for={:search}
phx-change="search"
phx-submit="search"
class="grow self-stretch flex flex-col items-stretch"
data-qa="tag_search"
>
<%= text_input(f, :search_term,
class: "input input-primary",
value: @search,
phx_debounce: 300,
placeholder: gettext("Search tags")
) %>
</.form>
</div>
<div class="flex flex-row flex-wrap justify-center items-center">
<%= if @tags |> Enum.empty?() do %>
<h2 class="title text-xl text-primary-600">
<%= gettext("No tags") %>
<%= display_emoji("😔") %>
</h2>
<% end %>
<%= for tag <- @tags do %>
<.tag_card tag={tag}>
<.link

View File

@ -64,7 +64,8 @@ defmodule CanneryWeb.Router do
live "/tags", TagLive.Index, :index
live "/tags/new", TagLive.Index, :new
live "/tags/:id/edit", TagLive.Index, :edit
live "/tags/edit/:id", TagLive.Index, :edit
live "/tags/search/:search", TagLive.Index, :search
live "/catalog", AmmoTypeLive.Index, :index
live "/catalog/new", AmmoTypeLive.Index, :new