Skip to content

Commit 1c43b5b

Browse files
committed
feat: add search input field
1 parent 2c9e66d commit 1c43b5b

File tree

6 files changed

+57
-0
lines changed

6 files changed

+57
-0
lines changed

lib/media_server_web/components/nav_component.ex

+9
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,13 @@ defmodule MediaServerWeb.Components.NavComponent do
22
use MediaServerWeb, :live_component
33

44
alias Phoenix.LiveView.JS
5+
6+
@impl true
7+
def handle_event("search", %{"search" => %{"query" => query}}, socket) do
8+
{
9+
:noreply,
10+
socket
11+
|> push_redirect(to: Routes.search_index_path(socket, :index, query: query))
12+
}
13+
end
514
end

lib/media_server_web/components/nav_component.html.heex

+4
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,8 @@
6666
<%= live_redirect "Continues", to: Routes.continues_index_path(@socket, :index), class: "text-base block px-3 py-2 font-medium #{ @class }" %>
6767
</div>
6868
</div>
69+
70+
<.form let={f} for={:search} id="search" phx-target={@myself} phx-submit="search">
71+
<%= text_input f, :query, placeholder: "Search", class: "mt-1 block w-full shadow-sm focus:ring-slate-500 focus:border-slate-500 sm:text-sm border-gray-300 rounded-md" %>
72+
</.form>
6973
</nav>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
defmodule MediaServerWeb.SearchLive.Index do
2+
use MediaServerWeb, :live_view
3+
4+
@impl true
5+
def mount(_params, _session, socket) do
6+
{
7+
:ok,
8+
socket
9+
|> assign(page_title: "Search")
10+
}
11+
end
12+
13+
@impl true
14+
def handle_params(_params, _url, socket) do
15+
{
16+
:noreply,
17+
socket
18+
}
19+
end
20+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
<h2>Results</h2>

lib/media_server_web/router.ex

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ defmodule MediaServerWeb.Router do
3636
live "/episodes/:episode/watch", WatchEpisodeLive.Show, :show
3737
end
3838

39+
live "/search", SearchLive.Index, :index
40+
3941
live "/continues", ContinuesLive.Index, :index
4042
live "/favourites", FavouritesLive.Index, :index
4143

test/media_server_web/components/nav_component_test.exs

+20
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,25 @@ defmodule MediaServerWeb.Components.NavComponentTest do
3232
assert html =~ "Favourites"
3333
assert html =~ "Continues"
3434
end
35+
36+
test "it can query search", %{conn: conn, user: user} do
37+
conn =
38+
post(conn, Routes.user_session_path(conn, :create), %{
39+
"user" => %{
40+
"email" => user.email,
41+
"password" => AccountsFixtures.valid_user_password()
42+
}
43+
})
44+
45+
{:ok, index_live, _html} = live(conn, Routes.components_index_path(conn, :index))
46+
47+
{:ok, _, html} =
48+
index_live
49+
|> form("#search", search: %{query: "Some query"})
50+
|> render_submit()
51+
|> follow_redirect(conn, Routes.search_index_path(conn, :index, query: "Some query"))
52+
53+
assert html =~ "Results"
54+
end
3555
end
3656
end

0 commit comments

Comments
 (0)