Compare commits
3 Commits
6751ade1d9
...
feature/be
Author | SHA1 | Date | |
---|---|---|---|
24b0942a13 | |||
256c883488 | |||
6d64449c1b |
@ -4,48 +4,30 @@ defmodule SomethingErlang.Grover do
|
||||
alias SomethingErlang.AwfulApi
|
||||
require Logger
|
||||
|
||||
def mount(user) do
|
||||
grover =
|
||||
DynamicSupervisor.start_child(
|
||||
SomethingErlang.Supervisor.Grovers,
|
||||
{__MODULE__, [self(), user]}
|
||||
)
|
||||
|
||||
case grover do
|
||||
{:ok, pid} -> pid
|
||||
{:error, {:already_started, pid}} -> pid
|
||||
{:error, error} -> {:error, error}
|
||||
end
|
||||
end
|
||||
|
||||
def get_thread!(thread_id, page_number) do
|
||||
GenServer.call(via(self()), {:show_thread, thread_id, page_number})
|
||||
GenServer.call(__MODULE__, {:show_thread, thread_id, page_number})
|
||||
end
|
||||
|
||||
def get_bookmarks!(page_number) do
|
||||
GenServer.call(via(self()), {:show_bookmarks, page_number})
|
||||
def get_bookmarks!(pid, page_number) do
|
||||
GenServer.call(pid, {:show_bookmarks, page_number})
|
||||
end
|
||||
|
||||
def start_link([lv_pid, user]) do
|
||||
def start_link(user) do
|
||||
GenServer.start_link(
|
||||
__MODULE__,
|
||||
[lv_pid, user],
|
||||
name: via(lv_pid)
|
||||
[user]
|
||||
)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init([pid, user]) do
|
||||
def init([user]) do
|
||||
%{bbuserid: userid, bbpassword: userhash} = user
|
||||
|
||||
initial_state = %{
|
||||
lv_pid: pid,
|
||||
user: %{id: userid, hash: userhash}
|
||||
}
|
||||
|
||||
Logger.debug("init #{userid} #{inspect(pid)}")
|
||||
Process.monitor(pid)
|
||||
{:ok, initial_state}
|
||||
{:ok, initial_state} |> dbg()
|
||||
end
|
||||
|
||||
@impl true
|
||||
@ -57,20 +39,6 @@ defmodule SomethingErlang.Grover do
|
||||
@impl true
|
||||
def handle_call({:show_bookmarks, page_number}, _from, state) do
|
||||
bookmarks = AwfulApi.bookmarks(page_number, state.user)
|
||||
{:reply, bookmarks, state}
|
||||
{:reply, bookmarks, state} |> dbg()
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({:DOWN, _ref, :process, _object, reason}, state) do
|
||||
Logger.debug("received :DOWN from: #{inspect(state.lv_pid)} reason: #{inspect(reason)}")
|
||||
|
||||
case reason do
|
||||
{:shutdown, _} -> {:stop, :normal, state}
|
||||
:killed -> {:stop, :normal, state}
|
||||
_ -> {:noreply, state}
|
||||
end
|
||||
end
|
||||
|
||||
defp via(lv_pid),
|
||||
do: {:via, Registry, {SomethingErlang.Registry.Grovers, lv_pid}}
|
||||
end
|
||||
|
@ -4,10 +4,12 @@ defmodule SomethingErlangWeb.BookmarksLive do
|
||||
alias SomethingErlang.Grover
|
||||
|
||||
def render(assigns) do
|
||||
assigns.bookmarks |> dbg()
|
||||
|
||||
~H"""
|
||||
<div :if={@bookmarks}>
|
||||
<div :if={bookmarks = @bookmarks.ok? && @bookmarks.result}>
|
||||
<.bookmark
|
||||
:for={thread <- @bookmarks}
|
||||
:for={thread <- bookmarks}
|
||||
id={thread.id}
|
||||
title={thread.title.thread_title}
|
||||
post_count={thread.replies}
|
||||
@ -17,7 +19,7 @@ defmodule SomethingErlangWeb.BookmarksLive do
|
||||
end
|
||||
|
||||
defp bookmark(assigns) do
|
||||
assigns = assign(assigns, :pages, trunc(assigns.post_count / 40))
|
||||
assigns = assign(assigns, :pages, ceil(assigns.post_count / 40))
|
||||
|
||||
~H"""
|
||||
<div>
|
||||
@ -32,11 +34,21 @@ defmodule SomethingErlangWeb.BookmarksLive do
|
||||
socket.assigns.current_user
|
||||
|> Map.put(:bbpassword, session["bbpassword"])
|
||||
|
||||
Grover.mount(user)
|
||||
{:ok, pid} = Grover.start_link(user) |> dbg()
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(:page_title, "Bookmarks")
|
||||
|> assign(:bookmarks, Grover.get_bookmarks!(1) |> dbg())}
|
||||
|> 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
|
||||
|
@ -69,10 +69,10 @@ defmodule SomethingErlangWeb.ThreadLive do
|
||||
~H"""
|
||||
<div class="navbar my-4 bg-base-200">
|
||||
<div class="flex-1"></div>
|
||||
<div class="pagination flex-none btn-group grid grid-cols-5">
|
||||
<div class="pagination join">
|
||||
<%= for btn <- buttons(@thread) do %>
|
||||
<.link
|
||||
class={["btn btn-sm btn-ghost", btn.special]}
|
||||
class={["btn btn-sm btn-ghost join-item", btn.special]}
|
||||
navigate={~p"/thread/#{@thread.id}?page=#{btn.page}"}
|
||||
>
|
||||
<.label_button label={btn.label} page={btn.page} />
|
||||
@ -86,8 +86,8 @@ defmodule SomethingErlangWeb.ThreadLive do
|
||||
defp label_button(%{label: label} = assigns) do
|
||||
case label do
|
||||
"«" -> ~H{<.icon name="hero-chevron-double-left-mini" /><%= @page %>}
|
||||
"‹" -> ~H{<.icon name="hero-chevron-left-mini" /><%= @page %>}
|
||||
"›" -> ~H{<%= @page %><.icon name="hero-chevron-right-mini" />}
|
||||
"‹" -> ~H{<.icon name="hero-chevron-left-mini" />}
|
||||
"›" -> ~H{<.icon name="hero-chevron-right-mini" />}
|
||||
"»" -> ~H{<%= @page %><.icon name="hero-chevron-double-right-mini" />}
|
||||
_ -> ~H{<%= @page %>}
|
||||
end
|
||||
|
Reference in New Issue
Block a user