From 5206166cce1560a18b8c35ad1ca8f2af10d0a10d Mon Sep 17 00:00:00 2001 From: Adrian Salamon Date: Sun, 19 Feb 2023 22:20:17 +0100 Subject: [PATCH 1/9] feat: search for users in search bar --- lib/haj/accounts.ex | 20 +++-- lib/haj_web/components/layouts.ex | 2 +- lib/haj_web/components/layouts/live.html.heex | 45 +++++----- lib/haj_web/live/nav_live.ex | 8 +- lib/haj_web/live/search_component.ex | 83 +++++++++++++++++++ ...230219172426_add_search_index_to_users.exs | 25 ++++++ 6 files changed, 153 insertions(+), 30 deletions(-) create mode 100644 lib/haj_web/live/search_component.ex create mode 100644 priv/repo/migrations/20230219172426_add_search_index_to_users.exs diff --git a/lib/haj/accounts.ex b/lib/haj/accounts.ex index a5979ef..2fd439b 100644 --- a/lib/haj/accounts.ex +++ b/lib/haj/accounts.ex @@ -153,15 +153,25 @@ defmodule Haj.Accounts do end @doc """ - Searches for users based on a search phrase + Searches for users based on a search phrase. """ def search_users(search_phrase) do query = from u in User, - where: - fragment("SIMILARITY(?,?) > 0.3", u.first_name, ^search_phrase) or - fragment("SIMILARITY(?,?) > 0.3", u.last_name, ^search_phrase), - order_by: fragment("LEVENSHTEIN(? || ?,?)", u.first_name, u.last_name, ^search_phrase) + where: fragment("? <% ?", ^search_phrase, u.full_name), + order_by: {:desc, fragment("? <% ?", ^search_phrase, u.full_name)} + + Repo.all(query) + end + + @doc """ + Searches for spex users based on a search phrase, only returns users that have been part of a spex. + """ + def search_spex_users(search_phrase) do + query = + from u in User, + where: fragment("? <% ?", ^search_phrase, u.full_name) and u.role != :none, + order_by: {:desc, fragment("? <% ?", ^search_phrase, u.full_name)} Repo.all(query) end diff --git a/lib/haj_web/components/layouts.ex b/lib/haj_web/components/layouts.ex index ce1c7ce..8f537d0 100644 --- a/lib/haj_web/components/layouts.ex +++ b/lib/haj_web/components/layouts.ex @@ -84,7 +84,7 @@ defmodule HajWeb.Layouts do > Mina uppgifter - <.link class="px-4 py-2 border-b hover:bg-burgandy-500 hover:text-white rounded-b-md"> + <.link class="px-4 py-2 border-b hover:bg-burgandy-500 hover:text-white"> Inställningar <.link diff --git a/lib/haj_web/components/layouts/live.html.heex b/lib/haj_web/components/layouts/live.html.heex index 812c12d..836d706 100644 --- a/lib/haj_web/components/layouts/live.html.heex +++ b/lib/haj_web/components/layouts/live.html.heex @@ -21,7 +21,7 @@ @@ -45,7 +45,7 @@ @@ -60,23 +60,30 @@ -
- <.icon name={:magnifying_glass} outline class="h-8 w-8 text-gray-700" /> -
- - - <.icon name={:bell} outline class="h-8 w-8 text-gray-700" /> -
-
- -