Files
something-erlang/lib/something_erlang_web/live/thread_live/show.ex

99 lines
3.1 KiB
Elixir
Raw Normal View History

2022-05-23 15:57:15 +02:00
defmodule SomethingErlangWeb.ThreadLive.Show do
use SomethingErlangWeb, :live_view
on_mount SomethingErlangWeb.UserLiveAuth
2022-05-23 15:57:15 +02:00
2022-07-21 18:42:42 +02:00
alias SomethingErlang.Grover
require Logger
2022-05-23 15:57:15 +02:00
@impl true
def mount(_params, _session, socket) do
Grover.mount(socket.assigns.current_user)
2022-05-23 15:57:15 +02:00
{:ok, socket}
end
@impl true
2022-07-21 18:42:42 +02:00
def handle_params(%{"id" => id, "page" => page}, _, socket) do
thread = Grover.get_thread!(id, page |> String.to_integer())
2022-05-23 15:57:15 +02:00
{:noreply,
socket
2022-07-27 21:52:18 +02:00
|> assign(:page_title, thread.title)
2022-07-21 18:42:42 +02:00
|> assign(:thread, thread)}
2022-05-23 15:57:15 +02:00
end
2022-07-21 18:42:42 +02:00
@impl true
def handle_params(%{"id" => id}, _, socket) do
{:noreply, push_redirect(socket,
to: Routes.thread_show_path(socket, :show, id, page: 1))}
end
def post(assigns) do
~H"""
<div class="post">
<.user info={@author} />
<article class="postbody">
2022-07-21 18:42:42 +02:00
<%= raw @article %>
</article>
<.toolbar date={@date} />
2022-07-21 18:42:42 +02:00
</div>
"""
end
def user(assigns) do
~H"""
<aside class="userinfo bg-base-100">
<h3 class="mb-4"><%= @info.name %></h3>
2022-07-27 21:52:18 +02:00
<div class="title hidden sm:flex flex-col text-sm pr-4">
2022-08-01 15:58:55 +02:00
<%= raw @info.title %>
2022-07-21 18:42:42 +02:00
</div>
</aside>
"""
end
def toolbar(assigns) do
~H"""
<div class="sm:col-span-2 text-sm p-2">
<%= @date |> Calendar.strftime("%A, %b %d %Y @ %H:%M") %></div>
"""
end
def pagination(assigns) do
%{page: page_number, page_count: page_count} = assigns.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
buttons = [
2022-08-02 14:11:49 +02:00
%{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},
2022-08-02 14:11:49 +02:00
%{label: "", page: next_button_target, special: "" <> last_page_disabled_button},
%{label: "»", page: page_count, special: "" <> last_page_disabled_button}
]
~H"""
<div class="navbar my-4 bg-base-200">
<div class="flex-1"></div>
2022-08-02 14:11:49 +02:00
<div class="pagination flex-none btn-group">
<%= for btn <- buttons do %>
2022-07-25 13:13:46 +02:00
<%= live_redirect class: "btn btn-sm" <> btn.special,
to: Routes.thread_show_path(@socket, :show, @thread.id, page: btn.page) do %>
<%= case btn.label do %>
2022-08-02 14:11:49 +02:00
<% "«" -> %>1 <Icons.chevron_left_double />
2022-07-25 13:13:46 +02:00
<% "" -> %><Icons.chevron_left />
<% "" -> %><Icons.chevron_right />
2022-08-02 14:11:49 +02:00
<% "»" -> %><Icons.chevron_right_double /> <%= page_count %>
2022-07-25 13:13:46 +02:00
<% _ -> %><%= btn.label %>
<% end %>
<% end %>
<% end %>
</div>
</div>
"""
end
2022-05-23 15:57:15 +02:00
end