2023-01-18 16:13:51 +01:00
|
|
|
|
defmodule SomethingErlangWeb.ThreadLive do
|
2022-05-23 15:57:15 +02:00
|
|
|
|
use SomethingErlangWeb, :live_view
|
|
|
|
|
|
2022-07-21 18:42:42 +02:00
|
|
|
|
alias SomethingErlang.Grover
|
|
|
|
|
|
2023-03-02 20:13:14 +01:00
|
|
|
|
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
|
|
|
|
|
|
2023-01-18 16:13:51 +01:00
|
|
|
|
def render(assigns) do
|
|
|
|
|
~H"""
|
2023-03-02 20:13:14 +01:00
|
|
|
|
<h2>
|
|
|
|
|
Threads!
|
|
|
|
|
</h2>
|
|
|
|
|
<pre class="whitespace-pre-wrap">
|
2023-01-18 16:13:51 +01:00
|
|
|
|
<%= inspect(@current_user) %>
|
|
|
|
|
</pre>
|
|
|
|
|
"""
|
2022-07-21 18:42:42 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def post(assigns) do
|
|
|
|
|
~H"""
|
|
|
|
|
<div class="post">
|
2023-01-18 16:13:51 +01:00
|
|
|
|
<.user info={@author} />
|
|
|
|
|
<article class="postbody">
|
2023-03-02 20:13:14 +01:00
|
|
|
|
<%= render_slot(@inner_block) %>
|
2023-01-18 16:13:51 +01:00
|
|
|
|
</article>
|
|
|
|
|
<.toolbar date={@date} />
|
2022-07-21 18:42:42 +02:00
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def user(assigns) do
|
|
|
|
|
~H"""
|
2022-08-02 13:24:07 +02:00
|
|
|
|
<aside class="userinfo bg-base-100">
|
2023-01-18 16:13:51 +01:00
|
|
|
|
<h3 class="mb-4"><%= @info.name %></h3>
|
2024-03-26 17:48:37 +01:00
|
|
|
|
<div class="title hidden sm:flex flex-col text-sm pr-4 sm:hidden">
|
2023-01-18 16:13:51 +01:00
|
|
|
|
<%= raw(@info.title) %>
|
|
|
|
|
</div>
|
2022-07-21 18:42:42 +02:00
|
|
|
|
</aside>
|
|
|
|
|
"""
|
|
|
|
|
end
|
2022-07-25 11:04:46 +02:00
|
|
|
|
|
2022-08-02 13:24:07 +02:00
|
|
|
|
def toolbar(assigns) do
|
|
|
|
|
~H"""
|
2022-08-11 14:14:47 +02:00
|
|
|
|
<div class="sm:col-span-2 text-sm p-2 px-4">
|
2023-01-18 16:13:51 +01:00
|
|
|
|
<%= @date |> Calendar.strftime("%A, %b %d %Y @ %H:%M") %>
|
|
|
|
|
</div>
|
2022-08-02 13:24:07 +02:00
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-25 11:04:46 +02:00
|
|
|
|
def pagination(assigns) do
|
|
|
|
|
~H"""
|
|
|
|
|
<div class="navbar my-4 bg-base-200">
|
2023-01-18 16:13:51 +01:00
|
|
|
|
<div class="flex-1"></div>
|
|
|
|
|
<div class="pagination flex-none btn-group grid grid-cols-5">
|
2023-03-02 20:13:14 +01:00
|
|
|
|
<%= for btn <- buttons(@thread) do %>
|
|
|
|
|
<.link
|
|
|
|
|
class={["btn btn-sm btn-ghost", btn.special]}
|
|
|
|
|
navigate={~p"/thread/#{@thread.id}?page=#{btn.page}"}
|
|
|
|
|
>
|
2023-03-25 14:39:12 +01:00
|
|
|
|
<.label_button label={btn.label} page={btn.page} />
|
2023-03-02 20:13:14 +01:00
|
|
|
|
</.link>
|
2023-01-18 16:13:51 +01:00
|
|
|
|
<% end %>
|
|
|
|
|
</div>
|
2022-07-25 11:04:46 +02:00
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
2023-01-18 16:13:51 +01:00
|
|
|
|
|
2023-03-25 14:39:12 +01:00
|
|
|
|
defp label_button(%{label: "«", page: page} = assigns),
|
|
|
|
|
do: ~H"""
|
2024-03-26 17:48:37 +01:00
|
|
|
|
<Heroicons.chevron_double_left mini /><%= @page %>
|
2023-03-25 14:39:12 +01:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
defp label_button(%{label: "‹", page: page} = assigns),
|
|
|
|
|
do: ~H"""
|
|
|
|
|
<Heroicons.chevron_left mini /><%= page %>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
defp label_button(%{label: "›", page: page} = assigns),
|
|
|
|
|
do: ~H"""
|
|
|
|
|
<%= page %><Heroicons.chevron_right mini />
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
defp label_button(%{label: "»", page: page} = assigns),
|
|
|
|
|
do: ~H"""
|
|
|
|
|
<%= page %><Heroicons.chevron_double_right mini />
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
defp label_button(%{page: page} = assigns),
|
|
|
|
|
do: ~H"""
|
|
|
|
|
<%= page %>
|
|
|
|
|
"""
|
|
|
|
|
|
2023-03-02 20:13:14 +01: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
|
|
|
|
|
|
2024-03-26 17:48:37 +01:00
|
|
|
|
def mount(params, session, socket) do
|
2023-03-02 20:13:14 +01:00
|
|
|
|
Grover.mount(socket.assigns.current_user)
|
2024-03-26 17:48:37 +01:00
|
|
|
|
session |> IO.inspect()
|
2023-01-18 16:13:51 +01:00
|
|
|
|
{: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}
|
2023-03-02 20:13:14 +01:00
|
|
|
|
|
|
|
|
|
{:noreply,
|
|
|
|
|
push_patch(
|
|
|
|
|
socket,
|
|
|
|
|
to: ~p"/thread/#{id}?#{params}",
|
|
|
|
|
replace: true
|
|
|
|
|
)}
|
2023-01-18 16:13:51 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def handle_params(%{}, _, socket) do
|
|
|
|
|
{:noreply, socket}
|
|
|
|
|
end
|
2022-05-23 15:57:15 +02:00
|
|
|
|
end
|