2024-06-02 14:44:53 +02:00
|
|
|
|
defmodule SomethingErlangWeb.ThreadLive do
|
|
|
|
|
use SomethingErlangWeb, :live_view
|
|
|
|
|
|
|
|
|
|
alias SomethingErlang.Grover
|
|
|
|
|
|
|
|
|
|
def render(%{thread: _} = assigns) do
|
|
|
|
|
~H"""
|
|
|
|
|
<h2>
|
|
|
|
|
<%= raw(@thread.title) %>
|
|
|
|
|
</h2>
|
|
|
|
|
|
|
|
|
|
<div class="thread my-8">
|
|
|
|
|
<.pagination thread={@thread} />
|
|
|
|
|
|
|
|
|
|
<%= for %{userinfo: author, postdate: date, postbody: article} <- @thread.posts do %>
|
|
|
|
|
<.post author={author} date={date}>
|
|
|
|
|
<%= raw(article) %>
|
|
|
|
|
</.post>
|
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
|
|
<.pagination thread={@thread} />
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def render(assigns) do
|
|
|
|
|
~H"""
|
|
|
|
|
<h2>
|
|
|
|
|
Threads!
|
|
|
|
|
</h2>
|
|
|
|
|
<pre class="whitespace-pre-wrap">
|
|
|
|
|
<%= inspect(@current_user) %>
|
|
|
|
|
</pre>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def post(assigns) do
|
|
|
|
|
~H"""
|
|
|
|
|
<div class="post">
|
|
|
|
|
<.user info={@author} />
|
|
|
|
|
<article class="postbody">
|
|
|
|
|
<%= render_slot(@inner_block) %>
|
|
|
|
|
</article>
|
|
|
|
|
<.toolbar date={@date} />
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def user(assigns) do
|
|
|
|
|
~H"""
|
|
|
|
|
<aside class="userinfo bg-base-100">
|
|
|
|
|
<h3 class="mb-4"><%= @info.name %></h3>
|
|
|
|
|
<div class="title hidden sm:flex flex-col text-sm pr-4">
|
|
|
|
|
<%= raw(@info.title) %>
|
|
|
|
|
</div>
|
|
|
|
|
</aside>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def toolbar(assigns) do
|
|
|
|
|
~H"""
|
|
|
|
|
<div class="sm:col-span-2 text-sm p-2 px-4">
|
|
|
|
|
<%= @date |> Calendar.strftime("%A, %b %d %Y @ %H:%M") %>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def pagination(assigns) do
|
|
|
|
|
~H"""
|
|
|
|
|
<div class="navbar my-4 bg-base-200">
|
|
|
|
|
<div class="flex-1"></div>
|
2024-08-12 16:52:26 +02:00
|
|
|
|
<div class="pagination join">
|
2024-06-02 14:44:53 +02:00
|
|
|
|
<%= for btn <- buttons(@thread) do %>
|
|
|
|
|
<.link
|
2024-08-12 16:52:26 +02:00
|
|
|
|
class={["btn btn-sm btn-ghost join-item", btn.special]}
|
2024-06-02 14:44:53 +02:00
|
|
|
|
navigate={~p"/thread/#{@thread.id}?page=#{btn.page}"}
|
|
|
|
|
>
|
|
|
|
|
<.label_button label={btn.label} page={btn.page} />
|
|
|
|
|
</.link>
|
|
|
|
|
<% end %>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
2024-06-02 15:55:18 +02:00
|
|
|
|
defp label_button(%{label: label} = assigns) do
|
|
|
|
|
case label do
|
|
|
|
|
"«" -> ~H{<.icon name="hero-chevron-double-left-mini" /><%= @page %>}
|
2024-08-12 16:52:26 +02:00
|
|
|
|
"‹" -> ~H{<.icon name="hero-chevron-left-mini" />}
|
|
|
|
|
"›" -> ~H{<.icon name="hero-chevron-right-mini" />}
|
2024-06-02 15:55:18 +02:00
|
|
|
|
"»" -> ~H{<%= @page %><.icon name="hero-chevron-double-right-mini" />}
|
|
|
|
|
_ -> ~H{<%= @page %>}
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-06-02 14:44:53 +02:00
|
|
|
|
|
|
|
|
|
defp buttons(thread) do
|
|
|
|
|
%{page: page_number, page_count: page_count} = thread
|
|
|
|
|
|
|
|
|
|
first_page_disabled_button = if page_number == 1, do: " btn-disabled", else: ""
|
|
|
|
|
last_page_disabled_button = if page_number == page_count, do: " btn-disabled", else: ""
|
|
|
|
|
active_page_button = " btn-active"
|
|
|
|
|
|
|
|
|
|
prev_button_target = if page_number > 1, do: page_number - 1, else: 1
|
|
|
|
|
next_button_target = if page_number < page_count, do: page_number + 1, else: page_count
|
|
|
|
|
|
|
|
|
|
[
|
|
|
|
|
%{label: "«", page: 1, special: "" <> first_page_disabled_button},
|
|
|
|
|
%{label: "‹", page: prev_button_target, special: "" <> first_page_disabled_button},
|
|
|
|
|
%{label: "#{page_number}", page: page_number, special: active_page_button},
|
|
|
|
|
%{label: "›", page: next_button_target, special: "" <> last_page_disabled_button},
|
|
|
|
|
%{label: "»", page: page_count, special: "" <> last_page_disabled_button}
|
|
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def mount(_params, session, socket) do
|
|
|
|
|
user =
|
|
|
|
|
socket.assigns.current_user
|
|
|
|
|
|> Map.put(:bbpassword, session["bbpassword"])
|
|
|
|
|
|
|
|
|
|
Grover.mount(user)
|
|
|
|
|
|
|
|
|
|
{:ok, socket}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def handle_params(%{"id" => id, "page" => page}, _, socket) do
|
|
|
|
|
thread = Grover.get_thread!(id, page |> String.to_integer())
|
|
|
|
|
|
|
|
|
|
{:noreply,
|
|
|
|
|
socket
|
|
|
|
|
|> assign(:page_title, thread.title)
|
|
|
|
|
|> assign(:thread, thread)}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def handle_params(%{"id" => id}, _, socket) do
|
|
|
|
|
params = %{page: 1}
|
|
|
|
|
|
|
|
|
|
{:noreply,
|
|
|
|
|
push_patch(
|
|
|
|
|
socket,
|
|
|
|
|
to: ~p"/thread/#{id}?#{params}",
|
|
|
|
|
replace: true
|
|
|
|
|
)}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def handle_params(%{}, _, socket) do
|
|
|
|
|
{:noreply, socket}
|
|
|
|
|
end
|
|
|
|
|
end
|