2022-05-23 15:57:15 +02:00
|
|
|
|
defmodule SomethingErlangWeb.ThreadLive.Show do
|
|
|
|
|
use SomethingErlangWeb, :live_view
|
2022-07-18 15:16:57 +02:00
|
|
|
|
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
|
2022-07-21 18:42:42 +02:00
|
|
|
|
def mount(%{"id" => id} = _params, _session, socket) do
|
2022-07-25 11:04:46 +02:00
|
|
|
|
Grover.mount(socket.assigns.current_user, id)
|
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
|
2022-07-25 11:04:46 +02:00
|
|
|
|
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="grow-1 w-full">
|
|
|
|
|
<%= raw @article %>
|
|
|
|
|
</article>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def user(assigns) do
|
|
|
|
|
~H"""
|
2022-07-27 21:52:18 +02:00
|
|
|
|
<aside class="userinfo bg-base-100 shrink-0 sm:w-[13em]">
|
2022-07-25 11:04:46 +02:00
|
|
|
|
<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
|
2022-07-25 11:04:46 +02:00
|
|
|
|
|
|
|
|
|
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-07-25 13:13:46 +02:00
|
|
|
|
%{label: "«", page: 1, special: " btn-square" <> first_page_disabled_button},
|
|
|
|
|
%{label: "‹", page: prev_button_target, special: " btn-square" <> first_page_disabled_button},
|
2022-07-25 11:04:46 +02:00
|
|
|
|
%{label: "#{page_number}", page: page_number, special: active_page_button},
|
2022-07-25 13:13:46 +02:00
|
|
|
|
%{label: "›", page: next_button_target, special: " btn-square" <> last_page_disabled_button},
|
|
|
|
|
%{label: "»", page: page_count, special: " btn-square" <> last_page_disabled_button}
|
2022-07-25 11:04:46 +02:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
~H"""
|
|
|
|
|
<div class="navbar my-4 bg-base-200">
|
|
|
|
|
<div class="flex-1"></div>
|
|
|
|
|
<div class="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 %>
|
|
|
|
|
<% "«" -> %><Icons.chevron_left_double />
|
|
|
|
|
<% "‹" -> %><Icons.chevron_left />
|
|
|
|
|
<% "›" -> %><Icons.chevron_right />
|
|
|
|
|
<% "»" -> %><Icons.chevron_right_double />
|
|
|
|
|
<% _ -> %><%= btn.label %>
|
|
|
|
|
<% end %>
|
|
|
|
|
<% end %>
|
2022-07-25 11:04:46 +02:00
|
|
|
|
<% end %>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
2022-05-23 15:57:15 +02:00
|
|
|
|
end
|