From 6ce840b8d9155170b54277c83b1363d4eb42ab34 Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Tue, 11 Feb 2020 18:21:12 +0100 Subject: Pagination --- lib/waev/export.ex | 7 ++++--- lib/waev_web/controllers/exports_controller.ex | 9 ++++++--- lib/waev_web/templates/exports/show.html.eex | 2 ++ lib/waev_web/views/exports_view.ex | 13 +++++++++++++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/waev/export.ex b/lib/waev/export.ex index eabb96b..d30b3f1 100644 --- a/lib/waev/export.ex +++ b/lib/waev/export.ex @@ -48,7 +48,6 @@ defmodule Waev.Export do {text, nil} end - IO.puts("Attachment: #{inspect(attachment)}") %Message{side: side, date: datetime, text: text, attachment: attachment} end end @@ -65,12 +64,14 @@ defmodule Waev.Export do end end - def get(e_id) do + def get(e_id, page, size) do case exists?(e_id) do true -> e = File.stream!(chat_path(e_id)) - |> Enum.take(600) + |> Enum.drop((page - 1) * size) + |> Enum.take(size) + |> Enum.reverse() |> Enum.reduce(%Waev.Export{id: e_id}, fn line, e -> line = String.trim(line) diff --git a/lib/waev_web/controllers/exports_controller.ex b/lib/waev_web/controllers/exports_controller.ex index f2b6fb4..8d16cb5 100644 --- a/lib/waev_web/controllers/exports_controller.ex +++ b/lib/waev_web/controllers/exports_controller.ex @@ -1,10 +1,13 @@ defmodule WaevWeb.ExportsController do use WaevWeb, :controller - def show(conn, %{"id" => id}) do - case Waev.Export.get(id) do + def show(conn, %{"id" => id} = params) do + page = Map.get(params, "page", "1") |> String.to_integer() + size = Map.get(params, "size", "100") |> String.to_integer() + + case Waev.Export.get(id, page, size) do {:ok, export} -> - render(conn, "show.html", id: id, export: export) + render(conn, "show.html", id: id, export: export, page: page, size: size) :error -> conn diff --git a/lib/waev_web/templates/exports/show.html.eex b/lib/waev_web/templates/exports/show.html.eex index 6356804..15a1360 100644 --- a/lib/waev_web/templates/exports/show.html.eex +++ b/lib/waev_web/templates/exports/show.html.eex @@ -35,3 +35,5 @@ <% end %> + +<%= pagination_bar(assigns, @page, @size) %> diff --git a/lib/waev_web/views/exports_view.ex b/lib/waev_web/views/exports_view.ex index 680454c..f0cfa0d 100644 --- a/lib/waev_web/views/exports_view.ex +++ b/lib/waev_web/views/exports_view.ex @@ -38,4 +38,17 @@ defmodule WaevWeb.ExportsView do |> raw() end) end + + def pagination_bar(assigns, page, size) do + prev = if page == 0, do: 0, else: page - 1 + # TODO max + next = page + 1 + + ~E""" +
+ """ + end end -- cgit v1.2.3