2019-11-17 19:18:21 +01:00
|
|
|
defmodule PhilomenaWeb.PaginationPlug do
|
2019-10-09 01:19:57 +02:00
|
|
|
import Plug.Conn
|
2019-11-30 23:40:53 +01:00
|
|
|
alias Pow.Plug
|
2019-10-09 01:19:57 +02:00
|
|
|
|
|
|
|
# No options
|
2019-11-30 23:40:53 +01:00
|
|
|
def init([]), do: []
|
2019-10-09 01:19:57 +02:00
|
|
|
|
|
|
|
# Assign pagination info
|
|
|
|
def call(conn, _opts) do
|
|
|
|
conn = conn |> fetch_query_params()
|
2019-11-30 23:40:53 +01:00
|
|
|
user = conn |> Plug.current_user()
|
2019-10-09 01:19:57 +02:00
|
|
|
params = conn.params
|
|
|
|
|
|
|
|
page_number =
|
|
|
|
case Integer.parse(params["page"] |> to_string()) do
|
|
|
|
{int, _rest} ->
|
|
|
|
int
|
|
|
|
_ ->
|
|
|
|
1
|
|
|
|
end
|
|
|
|
|
|
|
|
page_number = page_number |> max(1)
|
|
|
|
|
|
|
|
page_size =
|
|
|
|
case Integer.parse(params["per_page"] |> to_string()) do
|
|
|
|
{int, _rest} ->
|
|
|
|
int
|
|
|
|
_ ->
|
|
|
|
25
|
|
|
|
end
|
|
|
|
|
|
|
|
page_size = page_size |> max(1) |> min(50)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:pagination, %{page_number: page_number, page_size: page_size})
|
2019-11-30 23:40:53 +01:00
|
|
|
|> assign(:image_pagination, %{page_number: page_number, page_size: image_page_size(user)})
|
2019-10-09 01:30:51 +02:00
|
|
|
|> assign(:scrivener, [page: page_number, page_size: page_size])
|
2019-11-30 23:40:53 +01:00
|
|
|
|> assign(:comment_scrivener, [page: page_number, page_size: comment_page_size(user)])
|
2019-10-09 01:19:57 +02:00
|
|
|
end
|
2019-11-30 23:40:53 +01:00
|
|
|
|
|
|
|
defp image_page_size(%{images_per_page: x}), do: x
|
|
|
|
defp image_page_size(_user), do: 15
|
|
|
|
|
|
|
|
defp comment_page_size(%{comments_per_page: x}), do: x
|
|
|
|
defp comment_page_size(_user), do: 25
|
2019-10-09 01:19:57 +02:00
|
|
|
end
|