@@ -7,43 +7,75 @@ defmodule PlexusWeb.AppLive.Index do
7
7
def mount ( _params , _session , socket ) do
8
8
{ :ok ,
9
9
socket
10
+ |> assign ( :page_title , "Crowdsourced de-Googled Android apps status ratings" )
10
11
|> assign ( :page , 1 )
11
- |> stream_configure ( :apps , dom_id: & & 1 . package )
12
- |> paginate_apps ( 1 ) }
12
+ |> assign ( :form , to_form ( changeset ( ) , as: :form ) )
13
+ |> assign ( :no_results? , false )
14
+ |> assign ( :end_of_timeline? , false )
15
+ |> stream_configure ( :apps , dom_id: & & 1 . package ) }
16
+ end
17
+
18
+ defp changeset ( params \\ % { } ) do
19
+ types = % { search: :string }
20
+ data = % { }
21
+ Ecto.Changeset . cast ( { data , types } , params , Map . keys ( types ) )
13
22
end
14
23
15
24
defp paginate_apps ( socket , new_page ) when new_page >= 1 do
16
25
% Scrivener.Page {
17
26
total_entries: total_entries ,
18
27
total_pages: total_pages ,
19
28
entries: apps
20
- } = Apps . list_apps ( page: new_page , scores: true , order_by: :name )
29
+ } =
30
+ Apps . list_apps (
31
+ search_term: socket . assigns . search_term ,
32
+ page: new_page ,
33
+ scores: true ,
34
+ order_by: :name ,
35
+ page_size: 50
36
+ )
21
37
22
- case apps do
23
- [ ] ->
38
+ case { apps , new_page } do
39
+ { [ ] , page } when page != 1 ->
24
40
assign ( socket , end_of_timeline?: total_pages == new_page )
25
41
26
- [ _ | _ ] = apps ->
42
+ { apps , _ } ->
43
+ opts = if new_page == 1 , do: [ reset: true ] , else: [ ]
44
+ end_of_timeline? = new_page >= total_pages
45
+
27
46
socket
28
- |> assign ( end_of_timeline?: false )
47
+ |> assign ( end_of_timeline?: end_of_timeline? )
48
+ |> assign ( no_results?: apps == [ ] )
29
49
|> assign ( :page , new_page )
30
50
|> assign ( :total_entries , total_entries )
31
- |> stream ( :apps , apps )
51
+ |> stream ( :apps , apps , opts )
32
52
end
33
53
end
34
54
35
55
@ impl Phoenix.LiveView
36
56
def handle_params ( params , _url , socket ) do
37
- { :noreply , apply_action ( socket , socket . assigns . live_action , params ) }
57
+ { :noreply ,
58
+ socket
59
+ |> assign ( :search_term , params [ "q" ] )
60
+ |> assign ( :form , to_form ( changeset ( % { search: params [ "q" ] } ) , as: :form ) )
61
+ |> paginate_apps ( 1 ) }
38
62
end
39
63
40
- defp apply_action ( socket , :index , _params ) do
41
- socket
42
- |> assign ( :page_title , "Crowdsourced de-Googled Android apps status ratings" )
43
- |> assign ( :app , nil )
64
+ @ impl Phoenix.LiveView
65
+ def handle_event ( "search" , % { "form" => form } , socket ) do
66
+ params =
67
+ form
68
+ |> Map . get ( "search" , "" )
69
+ |> String . trim ( )
70
+ |> case do
71
+ "" -> % { }
72
+ "*" -> % { }
73
+ term -> % { q: term }
74
+ end
75
+
76
+ { :noreply , push_patch ( socket , to: ~p" /?#{ params } " ) }
44
77
end
45
78
46
- @ impl Phoenix.LiveView
47
79
def handle_event ( "next-page" , _ , socket ) do
48
80
{ :noreply , paginate_apps ( socket , socket . assigns . page + 1 ) }
49
81
end
0 commit comments