assign(page: 1, per_page: 20)
+ |> paginate_contacts(1)}
end
@impl true
@@ -38,4 +41,44 @@ defmodule SendItWeb.ContactLive.Index do
{:noreply, stream_delete(socket, :contacts, contact)}
end
+
+ def handle_event("next-page", _, socket) do
+ IO.puts("NEXT Pageee")
+ {:noreply, paginate_contacts(socket, socket.assigns.page + 1)}
+ end
+
+ def handle_event("prev-page", %{"_overran" => true}, socket) do
+ {:noreply, paginate_contacts(socket, 1)}
+ end
+
+ def handle_event("prev-page", _, socket) do
+ if socket.assigns.page > 1 do
+ {:noreply, paginate_contacts(socket, socket.assigns.page - 1)}
+ else
+ {:noreply, socket}
+ end
+ end
+
+ defp paginate_contacts(socket, new_page) when new_page >= 1 do
+ %{per_page: per_page, page: cur_page} = socket.assigns
+ contacts = Marketing.list_contacts(offset: (new_page - 1) * per_page, limit: per_page)
+
+ {contacts, at, limit} =
+ if new_page >= cur_page do
+ {contacts, -1, per_page * 3 * -1}
+ else
+ {Enum.reverse(contacts), 0, per_page * 3}
+ end
+
+ case contacts do
+ [] ->
+ assign(socket, end_of_timeline?: at == -1)
+
+ [_ | _] = contacts ->
+ socket
+ |> assign(end_of_timeline?: false)
+ |> assign(:page, new_page)
+ |> stream(:contacts, contacts, at: at, limit: limit)
+ end
+ end
end
diff --git a/lib/send_it_web/live/contact_live/index.html.heex b/lib/send_it_web/live/contact_live/index.html.heex
index 9d5bdff..71c4869 100644
--- a/lib/send_it_web/live/contact_live/index.html.heex
+++ b/lib/send_it_web/live/contact_live/index.html.heex
@@ -11,6 +11,9 @@
id="contacts"
rows={@streams.contacts}
row_click={fn {_id, contact} -> JS.navigate(~p"/contacts/#{contact}") end}
+ phx-viewport-top={@page > 1 && "prev-page"}
+ phx-viewport-bottom={!@end_of_timeline? && "next-page"}
+ phx-page-loading
>
<:col :let={{_id, contact}} label="Name"><%= contact.name %>
<:col :let={{_id, contact}} label="Email"><%= contact.email %>
@@ -25,6 +28,8 @@
+No more contacts!
+
<.modal :if={@live_action in [:new]} id="contact-modal" show on_cancel={JS.patch(~p"/contacts")}>
<.live_component
module={SendItWeb.ContactLive.FormComponent}
|