Browse Source

Don't show next page button if there is no next page

master
Dylan Baker 3 years ago
parent
commit
8a1b934bac
3 changed files with 21 additions and 8 deletions
  1. 4
    2
      lib/search.rb
  2. 16
    5
      web/server.rb
  3. 1
    1
      web/views/results.erb

+ 4
- 2
lib/search.rb View File

59
 def search_threads(query, username, from_date, to_date, sort, offset)
59
 def search_threads(query, username, from_date, to_date, sort, offset)
60
   DB[<<-SQL, query, username, username, from_date, from_date, to_date, to_date, offset]
60
   DB[<<-SQL, query, username, username, from_date, from_date, to_date, to_date, offset]
61
     SELECT
61
     SELECT
62
-      threads.*
62
+      threads.*,
63
+      count(*) OVER() AS full_count
63
     FROM threads
64
     FROM threads
64
     WHERE
65
     WHERE
65
       to_tsvector(title) @@ plainto_tsquery(?)
66
       to_tsvector(title) @@ plainto_tsquery(?)
77
     SELECT
78
     SELECT
78
       posts.*,
79
       posts.*,
79
       threads.title as thread_title,
80
       threads.title as thread_title,
80
-      threads.remote_id as remote_thread_id
81
+      threads.remote_id as remote_thread_id,
82
+      count(*) OVER() AS full_count
81
     FROM posts
83
     FROM posts
82
     INNER JOIN threads on posts.thread_id = threads.id
84
     INNER JOIN threads on posts.thread_id = threads.id
83
     WHERE
85
     WHERE

+ 16
- 5
web/server.rb View File

42
     unless results[:errors].empty?
42
     unless results[:errors].empty?
43
       erb :results, { locals: {errors: results[:errors]}, layout: :layout }
43
       erb :results, { locals: {errors: results[:errors]}, layout: :layout }
44
     else
44
     else
45
+      params[:current_count] = results[:results].to_a.size
46
+      params[:full_count] = results[:results].empty? ? 0 : results[:results].first[:full_count]
45
       previous_url, next_url = build_urls(params)
47
       previous_url, next_url = build_urls(params)
46
 
48
 
47
       locals =
49
       locals =
85
     def build_urls(params)
87
     def build_urls(params)
86
       current_page = params[:page].to_i
88
       current_page = params[:page].to_i
87
       previous_page = current_page > 1 ? current_page - 1 : nil
89
       previous_page = current_page > 1 ? current_page - 1 : nil
88
-      next_page = current_page + 1
90
+
91
+      if params[:current_count] == params[:full_count]
92
+        next_page = nil
93
+      else
94
+        next_page = current_page + 1
95
+      end
89
 
96
 
90
       url_params = { q: params[:q], type: params[:type] }
97
       url_params = { q: params[:q], type: params[:type] }
91
       url_params[:username] = params[:username] if params[:username]
98
       url_params[:username] = params[:username] if params[:username]
92
       url_params[:from_date] = params[:from_date] if params[:from_date]
99
       url_params[:from_date] = params[:from_date] if params[:from_date]
93
       url_params[:to_date] = params[:to_date] if params[:to_date]
100
       url_params[:to_date] = params[:to_date] if params[:to_date]
94
 
101
 
95
-      [previous_page, next_page].map do |page|
102
+      [
96
         URI::Generic.build(
103
         URI::Generic.build(
97
           path: '/search',
104
           path: '/search',
98
-          query: URI.encode_www_form(url_params.merge(page: page))
99
-        )
100
-      end
105
+          query: URI.encode_www_form(url_params.merge(page: previous_page))
106
+        ),
107
+        next_page.nil? ? nil : URI::Generic.build(
108
+          path: '/search',
109
+          query: URI.encode_www_form(url_params.merge(page: next_page))
110
+        ),
111
+      ]
101
     end
112
     end
102
 
113
 
103
     def current_user
114
     def current_user

+ 1
- 1
web/views/results.erb View File

54
           </a>
54
           </a>
55
         <% end %>
55
         <% end %>
56
 
56
 
57
-        <% unless results.empty? %>
57
+        <% unless results.empty? || next_url.nil? %>
58
           <a class="btn btn--small btn--next" href="<%= next_url %>">
58
           <a class="btn btn--small btn--next" href="<%= next_url %>">
59
             next page
59
             next page
60
           </a>
60
           </a>

Loading…
Cancel
Save