Add group read view

This commit is contained in:
Shadowfacts 2020-06-01 22:38:27 -04:00
parent 2a3c085fef
commit 5da980aef4
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
6 changed files with 77 additions and 6 deletions

View File

@ -1,8 +1,9 @@
defmodule FrenzyWeb.GroupController do defmodule FrenzyWeb.GroupController do
use FrenzyWeb, :controller use FrenzyWeb, :controller
alias Frenzy.{Repo, Group, Feed} alias Frenzy.{Repo, Group, Feed, Item, Paginator}
alias FrenzyWeb.Router.Helpers, as: Routes alias FrenzyWeb.Router.Helpers, as: Routes
alias FrenzyWeb.Endpoint alias FrenzyWeb.Endpoint
import Ecto.Query
plug :user_owns_group plug :user_owns_group
@ -95,4 +96,25 @@ defmodule FrenzyWeb.GroupController do
{:ok, _} = Repo.delete(group) {:ok, _} = Repo.delete(group)
redirect(conn, to: Routes.group_path(Endpoint, :index)) redirect(conn, to: Routes.group_path(Endpoint, :index))
end end
def read(conn, params) do
group = conn.assigns[:group] |> Repo.preload(:feeds)
feed_ids = Enum.map(group.feeds, fn feed -> feed.id end)
items =
Item
|> where([i], i.feed_id in ^feed_ids)
|> Paginator.paginate(params)
|> limit(50)
|> preload(:feed)
|> Repo.all()
|> Enum.sort_by(fn item -> item.date end, fn a, b ->
DateTime.compare(a, b) == :gt
end)
render(conn, "read.html", %{
group: group,
items: items
})
end
end end

View File

@ -48,6 +48,7 @@ defmodule FrenzyWeb.Router do
get "/", GroupController, :index get "/", GroupController, :index
resources "/groups", GroupController resources "/groups", GroupController
get "/groups/:id/read", GroupController, :read
resources "/feeds", FeedController, except: [:index, :new] resources "/feeds", FeedController, except: [:index, :new]
post "/feeds/:id/refresh", FeedController, :refresh post "/feeds/:id/refresh", FeedController, :refresh

View File

@ -0,0 +1,41 @@
<h1><%= @group.title %></h1>
<%= unless is_nil(prev_page_path(@conn, @items)) do %>
<p class="text-center">
<a href="<%= prev_page_path(@conn, @items) %>" class="pagination-link">Newer</a>
</p>
<% end %>
<table class="table table-striped">
<tbody>
<%= for item <- @items do %>
<tr <%= if item.read do %>class="item-read"<% end %>>
<td>
<a href="<%= Routes.item_path(@conn, :show, item.id) %>">
<%= item.title || "(Untitled)" %>
</a>
</td>
<td>
<a href="<%= Routes.feed_path(@conn, :show, item.feed.id) %>">
<%= if item.feed.favicon do %>
<img src="<%= item.feed.favicon %>" alt="<%= item.feed.title %> favicon" class="favicon">
<% end %>
<%= item.feed.title || "(Untitled)" %>
</a>
</td>
<td>
<%= if item.date do %>
<% {:ok, date} = Timex.format(item.date, "{YYYY}-{0M}-{0D} {0h12}:{m} {AM}") %>
<%= date %>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= unless is_nil(next_page_path(@conn, @items)) do %>
<p class="text-center">
<a href="<%= next_page_path(@conn, @items) %>" class="pagination-link">Older</a>
</p>
<% end %>

View File

@ -18,7 +18,7 @@
</div> </div>
<% end %> <% end %>
<a href="#" class="btn btn-secondary">Read Articles</a> <a href="<%= Routes.group_path(@conn, :read, @group.id) %>" class="btn btn-secondary">Read Articles</a>
<h3 class="mt-4">Feeds</h3> <h3 class="mt-4">Feeds</h3>

View File

@ -3,8 +3,10 @@ defmodule FrenzyWeb.FeedView do
alias Frenzy.{Repo, Item} alias Frenzy.{Repo, Item}
import Ecto.Query import Ecto.Query
@spec next_page_path(Plug.Conn.t(), [Frenzy.Item.t()]) :: String.t() @spec next_page_path(Plug.Conn.t(), [Frenzy.Item.t()]) :: String.t() | nil
defp next_page_path(conn, items) do def next_page_path(_conn, []), do: nil
def next_page_path(conn, items) do
%Item{id: id, date: date} = List.last(items) %Item{id: id, date: date} = List.last(items)
has_older = Repo.exists?(from i in Item, where: i.date < ^date) has_older = Repo.exists?(from i in Item, where: i.date < ^date)
@ -15,8 +17,10 @@ defmodule FrenzyWeb.FeedView do
end end
end end
@spec prev_page_path(Plug.Conn.t(), [Frenzy.Item.t()]) :: String.t() @spec prev_page_path(Plug.Conn.t(), [Frenzy.Item.t()]) :: String.t() | nil
defp prev_page_path(conn, [%Item{id: id, date: date} | _]) do 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) has_newer = Repo.exists?(from i in Item, where: i.date > ^date)
if has_newer do if has_newer do

View File

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