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-08-03 13:42:44 +02:00
|
|
|
|
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
|
2022-07-25 11:04:46 +02:00
|
|
|
|
thread = Grover.get_thread!(id, page |> String.to_integer())
|
2022-11-07 13:49:31 +01:00
|
|
|
|
|
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
|
2022-11-07 13:49:31 +01:00
|
|
|
|
{:noreply,
|
|
|
|
|
push_redirect(socket,
|
|
|
|
|
to: Routes.thread_show_path(socket, :show, id, page: 1)
|
|
|
|
|
)}
|
2022-07-21 18:42:42 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def post(assigns) do
|
|
|
|
|
~H"""
|
|
|
|
|
<div class="post">
|
|
|
|
|
<.user info={@author} />
|
2022-08-02 13:24:07 +02:00
|
|
|
|
<article class="postbody">
|
2022-07-21 18:42:42 +02:00
|
|
|
|
<%= raw @article %>
|
|
|
|
|
</article>
|
2022-08-02 13:24:07 +02:00
|
|
|
|
<.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">
|
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
|
|
|
|
|
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">
|
2022-08-02 13:24:07 +02:00
|
|
|
|
<%= @date |> Calendar.strftime("%A, %b %d %Y @ %H:%M") %></div>
|
|
|
|
|
"""
|
|
|
|
|
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: ""
|
2022-11-07 13:49:31 +01:00
|
|
|
|
last_page_disabled_button = if page_number == page_count, do: " btn-disabled", else: ""
|
2022-07-25 11:04:46 +02:00
|
|
|
|
active_page_button = " btn-active"
|
|
|
|
|
|
2022-11-07 13:49:31 +01:00
|
|
|
|
prev_button_target = if page_number > 1, do: page_number - 1, else: 1
|
2022-07-25 11:04:46 +02:00
|
|
|
|
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},
|
2022-07-25 11:04:46 +02:00
|
|
|
|
%{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}
|
2022-07-25 11:04:46 +02:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
~H"""
|
|
|
|
|
<div class="navbar my-4 bg-base-200">
|
|
|
|
|
<div class="flex-1"></div>
|
2022-11-07 13:49:31 +01:00
|
|
|
|
<div class="pagination flex-none btn-group grid grid-cols-5">
|
2022-07-25 11:04:46 +02:00
|
|
|
|
<%= for btn <- buttons do %>
|
2022-11-07 13:49:31 +01:00
|
|
|
|
<%= live_redirect class: "btn btn-sm btn-ghost" <> btn.special,
|
2022-07-25 13:13:46 +02:00
|
|
|
|
to: Routes.thread_show_path(@socket, :show, @thread.id, page: btn.page) do %>
|
|
|
|
|
<%= case btn.label do %>
|
2022-11-07 13:49:31 +01:00
|
|
|
|
<% "«" -> %><Icons.chevron_left_double /><%= btn.page %>
|
|
|
|
|
<% "‹" -> %><Icons.chevron_left /><%= btn.page %>
|
|
|
|
|
<% "›" -> %><%= btn.page %><Icons.chevron_right />
|
|
|
|
|
<% "»" -> %><%= btn.page %><Icons.chevron_right_double />
|
|
|
|
|
<% _ -> %><%= btn.page %>
|
2022-07-25 13:13:46 +02:00
|
|
|
|
<% end %>
|
|
|
|
|
<% end %>
|
2022-07-25 11:04:46 +02:00
|
|
|
|
<% end %>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
"""
|
|
|
|
|
end
|
2022-05-23 15:57:15 +02:00
|
|
|
|
end
|