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,7 +59,8 @@ end
59 59
 def search_threads(query, username, from_date, to_date, sort, offset)
60 60
   DB[<<-SQL, query, username, username, from_date, from_date, to_date, to_date, offset]
61 61
     SELECT
62
-      threads.*
62
+      threads.*,
63
+      count(*) OVER() AS full_count
63 64
     FROM threads
64 65
     WHERE
65 66
       to_tsvector(title) @@ plainto_tsquery(?)
@@ -77,7 +78,8 @@ def search_posts(query, username, from_date, to_date, offset)
77 78
     SELECT
78 79
       posts.*,
79 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 83
     FROM posts
82 84
     INNER JOIN threads on posts.thread_id = threads.id
83 85
     WHERE

+ 16
- 5
web/server.rb View File

@@ -42,6 +42,8 @@ class VLVSearch < Sinatra::Base
42 42
     unless results[:errors].empty?
43 43
       erb :results, { locals: {errors: results[:errors]}, layout: :layout }
44 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 47
       previous_url, next_url = build_urls(params)
46 48
 
47 49
       locals =
@@ -85,19 +87,28 @@ class VLVSearch < Sinatra::Base
85 87
     def build_urls(params)
86 88
       current_page = params[:page].to_i
87 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 97
       url_params = { q: params[:q], type: params[:type] }
91 98
       url_params[:username] = params[:username] if params[:username]
92 99
       url_params[:from_date] = params[:from_date] if params[:from_date]
93 100
       url_params[:to_date] = params[:to_date] if params[:to_date]
94 101
 
95
-      [previous_page, next_page].map do |page|
102
+      [
96 103
         URI::Generic.build(
97 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 112
     end
102 113
 
103 114
     def current_user

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

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

Loading…
Cancel
Save