Fix pagination not working correctly with multiple feeds

This commit is contained in:
Shadowfacts 2020-06-01 22:46:15 -04:00
parent 5087ef0395
commit 4e79a1fe19
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
6 changed files with 56 additions and 42 deletions

View File

@ -38,9 +38,30 @@ defmodule FrenzyWeb.FeedController do
DateTime.compare(a, b) == :gt
end)
first_item = List.first(items)
last_item = List.last(items)
prev_page_path =
if !is_nil(first_item) &&
Repo.exists?(from i in Item, where: i.date > ^first_item.date and i.feed_id == ^id) do
current_path(conn, %{after: first_item.id})
else
nil
end
next_page_path =
if !is_nil(last_item) &&
Repo.exists?(from i in Item, where: i.date < ^last_item.date and i.feed_id == ^id) do
current_path(conn, %{before: last_item.id})
else
nil
end
render(conn, "show.html", %{
feed: feed,
items: items
items: items,
next_page_path: next_page_path,
prev_page_path: prev_page_path
})
end

View File

@ -112,9 +112,34 @@ defmodule FrenzyWeb.GroupController do
DateTime.compare(a, b) == :gt
end)
first_item = List.first(items)
last_item = List.last(items)
prev_page_path =
if !is_nil(first_item) &&
Repo.exists?(
from i in Item, where: i.date > ^first_item.date and i.feed_id in ^feed_ids
) do
current_path(conn, %{after: first_item.id})
else
nil
end
next_page_path =
if !is_nil(last_item) &&
Repo.exists?(
from i in Item, where: i.date < ^last_item.date and i.feed_id in ^feed_ids
) do
current_path(conn, %{before: last_item.id})
else
nil
end
render(conn, "read.html", %{
group: group,
items: items
items: items,
prev_page_path: prev_page_path,
next_page_path: next_page_path
})
end
end

View File

@ -17,9 +17,9 @@
<h3 class="mt-4">Items</h3>
<%= unless is_nil(prev_page_path(@conn, @items)) do %>
<%= unless is_nil(@prev_page_path) do %>
<p class="text-center">
<a href="<%= prev_page_path(@conn, @items) %>" class="pagination-link">Newer</a>
<a href="<%= @prev_page_path %>" class="pagination-link">Newer</a>
</p>
<% end %>
@ -41,8 +41,8 @@
</tbody>
</table>
<%= unless is_nil(next_page_path(@conn, @items)) do %>
<%= unless is_nil(@next_page_path) do %>
<p class="text-center">
<a href="<%= next_page_path(@conn, @items) %>" class="pagination-link">Older</a>
<a href="<%= @next_page_path %>" class="pagination-link">Older</a>
</p>
<% end %>

View File

@ -1,8 +1,8 @@
<h1><%= @group.title %></h1>
<%= unless is_nil(prev_page_path(@conn, @items)) do %>
<%= unless is_nil(@prev_page_path) do %>
<p class="text-center">
<a href="<%= prev_page_path(@conn, @items) %>" class="pagination-link">Newer</a>
<a href="<%= @prev_page_path %>" class="pagination-link">Newer</a>
</p>
<% end %>
@ -34,8 +34,8 @@
</tbody>
</table>
<%= unless is_nil(next_page_path(@conn, @items)) do %>
<%= unless is_nil(@next_page_path) do %>
<p class="text-center">
<a href="<%= next_page_path(@conn, @items) %>" class="pagination-link">Older</a>
<a href="<%= @next_page_path %>" class="pagination-link">Older</a>
</p>
<% end %>

View File

@ -1,32 +1,3 @@
defmodule FrenzyWeb.FeedView do
use FrenzyWeb, :view
alias Frenzy.{Repo, Item}
import Ecto.Query
@spec next_page_path(Plug.Conn.t(), [Frenzy.Item.t()]) :: String.t() | nil
def next_page_path(_conn, []), do: nil
def next_page_path(conn, items) do
%Item{id: id, date: date} = List.last(items)
has_older = Repo.exists?(from i in Item, where: i.date < ^date)
if has_older do
Phoenix.Controller.current_path(conn, %{before: id})
else
nil
end
end
@spec prev_page_path(Plug.Conn.t(), [Frenzy.Item.t()]) :: String.t() | nil
def prev_page_path(_conn, []), do: nil
def prev_page_path(conn, [%Item{id: id, date: date} | _]) do
has_newer = Repo.exists?(from i in Item, where: i.date > ^date)
if has_newer do
Phoenix.Controller.current_path(conn, %{after: id})
else
nil
end
end
end

View File

@ -1,6 +1,3 @@
defmodule FrenzyWeb.GroupView do
use FrenzyWeb, :view
defdelegate next_page_path(conn, items), to: FrenzyWeb.FeedView
defdelegate prev_page_path(conn, items), to: FrenzyWeb.FeedView
end