Files
something-erlang/lib/something_erlang_web/live/bookmarks_live.ex

55 lines
1.3 KiB
Elixir

defmodule SomethingErlangWeb.BookmarksLive do
use SomethingErlangWeb, :live_view
alias SomethingErlang.Grover
def render(assigns) do
assigns.bookmarks |> dbg()
~H"""
<div :if={bookmarks = @bookmarks.ok? && @bookmarks.result}>
<.bookmark
:for={thread <- bookmarks}
id={thread.id}
title={thread.title.thread_title}
post_count={thread.replies}
/>
</div>
"""
end
defp bookmark(assigns) do
assigns = assign(assigns, :pages, ceil(assigns.post_count / 40))
~H"""
<div>
<.link href={~p"/thread/#{@id}"}><%= @title %></.link>
<.link href={~p"/thread/#{@id}?page=#{@pages}"}><%= @pages %></.link>
</div>
"""
end
def mount(_params, session, socket) do
user =
socket.assigns.current_user
|> Map.put(:bbpassword, session["bbpassword"])
{:ok, pid} = Grover.start_link(user) |> dbg()
{:ok,
socket
|> assign(:page_title, "Bookmarks")
|> assign_async(:bookmarks, fn ->
{:ok, bookmarks} = get_bookmarks(pid)
{:ok, %{bookmarks: bookmarks}}
end)}
end
defp get_bookmarks(pid) do
case Grover.get_bookmarks!(pid, 1) do
bookmarks when is_list(bookmarks) -> {:ok, bookmarks}
_ -> {:error, "Failed getting bookmarks"}
end
end
end