Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic search #40

Merged
merged 9 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions lib/haj/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,35 @@ 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, options \\ []) do
include_rank = Keyword.get(options, :rank, false)

base_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)}

query =
if include_rank do
from u in base_query,
select: {u, fragment("word_similarity(?, ?)", ^search_phrase, u.full_name)}
else
base_query
end

Repo.all(query)
end
Expand Down
1 change: 1 addition & 0 deletions lib/haj/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Haj.Accounts.User do
field :first_name, :string
field :last_name, :string
field :username, :string
field :full_name, :string

field :google_account, :string
field :phone, :string
Expand Down
42 changes: 32 additions & 10 deletions lib/haj/spex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,9 @@ defmodule Haj.Spex do
join: gm in assoc(sg, :group_memberships),
join: u in assoc(gm, :user),
where:
sg.show_id == ^show_id and
(fragment("SIMILARITY(?, ?) > 0.3", u.first_name, ^search_phrase) or
fragment("SIMILARITY(?, ?) > 0.3", u.last_name, ^search_phrase) or
fragment("SIMILARITY(?, ?) > 0.3", u.username, ^search_phrase)),
order_by: fragment("LEVENSHTEIN(? || ?,?)", u.first_name, u.last_name, ^search_phrase),
fragment("? <% ?", ^search_phrase, u.full_name) and
sg.show_id == ^show_id,
order_by: {:desc, fragment("? <% ?", ^search_phrase, u.full_name)},
select: u,
distinct: u

Expand All @@ -506,14 +504,38 @@ defmodule Haj.Spex do
join: gm in assoc(sg, :group_memberships),
join: u in assoc(gm, :user),
where:
sg.id == ^show_group_id and
(fragment("SIMILARITY(?, ?) > 0.3", u.first_name, ^search_phrase) or
fragment("SIMILARITY(?, ?) > 0.3", u.last_name, ^search_phrase) or
fragment("SIMILARITY(?, ?) > 0.3", u.username, ^search_phrase)),
order_by: fragment("LEVENSHTEIN(? || ?,?)", u.first_name, u.last_name, ^search_phrase),
fragment("? <% ?", ^search_phrase, u.full_name) and
sg.id == ^show_group_id,
order_by: {:desc, fragment("? <% ?", ^search_phrase, u.full_name)},
select: u,
distinct: u

Repo.all(query)
end

@doc """
Searches for a showgroup based on a search phrase for a given show
"""
def search_show_groups(show_id, search_phrase, options \\ []) do
include_rank = Keyword.get(options, :rank, false)

base_query =
from sg in ShowGroup,
join: g in assoc(sg, :group),
where:
sg.show_id == ^show_id and
fragment("? % ?", g.name, ^search_phrase),
order_by: fragment("? % ?", g.name, ^search_phrase),
preload: [:group]

query =
if include_rank do
from [sg, g] in base_query,
select: {sg, fragment("similarity(?, ?)", g.name, ^search_phrase)}
else
base_query
end

Repo.all(query)
end
end
2 changes: 1 addition & 1 deletion lib/haj_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ defmodule HajWeb.CoreComponents do

## Examples

<.simple_form :let={f} for={:user} phx-change="validate" phx-submit="save">
<.simple_form :let={f} as={:user} phx-change="validate" phx-submit="save">
<.input field={{f, :email}} label="Email"/>
<.input field={{f, :username}} label="Username" />
<:actions>
Expand Down
8 changes: 4 additions & 4 deletions lib/haj_web/components/layouts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ defmodule HajWeb.Layouts do
~H"""
<.link
navigate={@navigate}
class={"text-white group flex items-center px-2 py-2 rounded-md #{if @active, do: "bg-burgandy-600"}"}
class={"text-white hover:text-burgandy-700 hover:bg-burgandy-50 flex items-center px-2 py-2 rounded-md #{if @active, do: "bg-burgandy-600"}"}
>
<.icon name={@icon_name} solid class=" mr-3 flex-shrink-0 h-6 w-6" />
<.icon name={@icon_name} solid class="mr-3 flex-shrink-0 h-6 w-6" />
<%= @title %>
</.link>
"""
Expand All @@ -59,7 +59,7 @@ defmodule HajWeb.Layouts do
~H"""
<.link
navigate={@navigate}
class={"text-burgandy-100 flex items-center pl-11 px-2 py-2 rounded-md #{if @active, do: "bg-burgandy-600"}"}
class={"text-burgandy-100 hover:text-burgandy-700 hover:bg-burgandy-50 flex items-center pl-11 px-2 py-2 rounded-md #{if @active, do: "bg-burgandy-600"}"}
>
<%= @title %>
</.link>
Expand All @@ -84,7 +84,7 @@ defmodule HajWeb.Layouts do
>
Mina uppgifter
</.link>
<.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>
<.link
Expand Down
44 changes: 25 additions & 19 deletions lib/haj_web/components/layouts/live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</div>
<nav class="px-4 pt-6">
<%= if @current_user do %>
<.sidebar_nav_links current_user={@current_user} active_tab={@active_tab}/>
<.sidebar_nav_links current_user={@current_user} active_tab={@active_tab} />
<% end %>
</nav>
</div>
Expand All @@ -45,7 +45,7 @@
<div class="h-0 flex-1 flex flex-col overflow-y-auto">
<nav class="px-4">
<%= if @current_user do %>
<.sidebar_nav_links current_user={@current_user} active_tab={@active_tab}/>
<.sidebar_nav_links current_user={@current_user} active_tab={@active_tab} />
<% end %>
</nav>
</div>
Expand All @@ -60,23 +60,29 @@
</button>
</div>

<div class="ml-auto flex flex-row items-center space-x-4">
<.icon name={:magnifying_glass} outline class="h-8 w-8 text-gray-700" />
<div class="relative h-8 w-8">
<span class="absolute top-0.5 right-0.5 w-2.5 h-2.5 rounded-full bg-burgandy-500">
</span>
<.icon name={:bell} outline class="h-8 w-8 text-gray-700" />
</div>
<div class="relative flex flex-row items-center">
<button role="button" phx-click={show("#user-dropdown")}>
<img
src={"https://zfinger.datasektionen.se/user/#{@current_user.username}/image/100"}
class="h-10 w-10 rounded-full object-cover object-top border-2 border-gray-300"
/>
</button>
<div id="user-dropdown" class="absolute top-12 right-0 w-48 z-10" style="display: none;">
<div class="fixed inset-0" phx-click={JS.hide(to: "#user-dropdown")} />
<.user_dropdown user={@current_user} />
<div class="w-full flex flex-row items-center justify-end space-x-4">
<.live_component module={HajWeb.SearchComponent} id="search" />
<div id="tobar-right" class="flex flex-row items-center space-x-4">
<div class="relative h-8 w-8">
<span class="absolute top-0.5 right-0.5 w-2.5 h-2.5 rounded-full bg-burgandy-500">
</span>
<.icon name={:bell} outline class="h-8 w-8 text-gray-700" />
</div>
<div class="relative flex flex-row items-center">
<button role="button" phx-click={show("#user-dropdown")} class="group">
<img
src={"https://zfinger.datasektionen.se/user/#{@current_user.username}/image/100"}
class="h-10 w-10 rounded-full object-cover object-top border-2 border-gray-300 group-hover:brightness-95"
/>
</button>
<div
id="user-dropdown"
class="absolute top-12 right-0 w-48 z-10"
style="display: none;"
>
<div class="fixed inset-0" phx-click={JS.hide(to: "#user-dropdown")} />
<.user_dropdown user={@current_user} />
</div>
</div>
</div>
</div>
Expand Down
6 changes: 5 additions & 1 deletion lib/haj_web/components/live_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ defmodule HajWeb.LiveHelpers do

def show_mobile_sidebar(js \\ %JS{}) do
js
|> JS.show(to: "#mobile-sidebar-container", transition: "fade-in", time: 100)
|> JS.show(
to: "#mobile-sidebar-container",
transition: {"transition fade-in duration-300", "opacity-0", "opacity-100"},
time: 300
)
|> JS.show(
to: "#mobile-sidebar",
display: "flex",
Expand Down
2 changes: 1 addition & 1 deletion lib/haj_web/live/applications_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule HajWeb.ApplicationsLive do
<div class="flex flex-col py-2 mb-2 gap-2 md:flex-row md:items-center border-burgandy-500 border-b-2">
<div class="uppercase font-bold">Filtrera</div>

<.form :let={f} for={:filter} phx-change="filter" class="w-full md:w-auto">
<.form :let={f} as={:filter} phx-change="filter" class="w-full md:w-auto">
<%= select(f, :show_group, group_options(@show.show_groups),
class: "h-full w-full",
prompt: "Alla grupper"
Expand Down
2 changes: 1 addition & 1 deletion lib/haj_web/live/apply_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ defmodule HajWeb.ApplyLive do

<.form
:let={f}
for={:application}
as={:application}
phx-submit="apply"
class="flex flex-col gap-1 mt-4 md:flex-[1] md:mt-0"
>
Expand Down
6 changes: 4 additions & 2 deletions lib/haj_web/live/group_admin_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule HajWeb.GroupAdminLive do
roles: [:gruppis, :chef],
role: :gruppis
)
|> assign(:active_tab, nil)

{:ok, socket}
end
Expand Down Expand Up @@ -122,13 +123,14 @@ defmodule HajWeb.GroupAdminLive do
Välj vilken typ av medlem (chef/gruppis), sök på användare och lägg sedan till!
</p>
<div class="flex flex-row items-stretch gap-2">
<.form :let={f} for={:role_form} phx-change="update_role">
<.form :let={f} as={:role_form} phx-change="update_role">
<%= select(f, :role, @roles, class: "h-full", value: @role) %>
</.form>

<.form
:let={f}
for={:search_form}
for={%{}}
as={:search_form}
phx-change="suggest"
phx-submit="add"
autocomplete={:off}
Expand Down
2 changes: 1 addition & 1 deletion lib/haj_web/live/members_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule HajWeb.MembersLive do
~H"""
<.form
:let={f}
for={:search_form}
as={:search_form}
phx-change="filter"
phx-no-submit
autocomplete={:off}
Expand Down
2 changes: 1 addition & 1 deletion lib/haj_web/live/merch_admin_live/index.html.heex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="pb-4 border-b">
<h3 class="font-bold text-2xl py-2">Administrera merch</h3>
<.form :let={f} id="show-form" for={:show} phx-change="select_show" class="">
<.form :let={f} id="show-form" as={:show} phx-change="select_show" class="">
<div class="">
<%= label(f, :show, "Välj spex",
class: "block text-sm font-semibold leading-6 text-zinc-800"
Expand Down
8 changes: 3 additions & 5 deletions lib/haj_web/live/nav_live.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
defmodule HajWeb.Nav do
use HajWeb, :component

alias HajWeb.UserLive
alias HajWeb.GroupLive
alias HajWeb.GroupsLive
alias HajWeb.MembersLive
alias HajWeb.DashboardLive
import Phoenix.LiveView
use Phoenix.Component

@spec on_mount(:default, any, any, Phoenix.LiveView.Socket.t()) ::
{:cont, Phoenix.LiveView.Socket.t()}
def on_mount(:default, _params, _session, socket) do
{:cont,
socket
|> attach_hook(:active_tab, :handle_params, &set_active_tab/3)}
|> Phoenix.LiveView.attach_hook(:active_tab, :handle_params, &set_active_tab/3)}
end

defp set_active_tab(params, _url, socket) do
Expand Down
Loading