Skip to content

Commit 16c9f93

Browse files
committed
refactor: simplify bounties live view pagination and status handling
1 parent 2cf462c commit 16c9f93

File tree

1 file changed

+30
-76
lines changed

1 file changed

+30
-76
lines changed

lib/algora_web/live/org/bounties_live.ex

Lines changed: 30 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,31 @@ defmodule AlgoraWeb.Org.BountiesLive do
3939
<button
4040
type="button"
4141
role="tab"
42-
aria-selected={@current_tab == :open}
43-
class={"inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium #{if @current_tab == :open, do: "bg-emerald-700 text-white", else: "hover:bg-emerald-700/50"}"}
44-
data-state={if @current_tab == :open, do: "active", else: "inactive"}
42+
aria-selected={@current_status == :open}
43+
class={"inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium #{if @current_status == :open, do: "bg-emerald-700 text-white", else: "hover:bg-emerald-700/50"}"}
44+
data-state={if @current_status == :open, do: "active", else: "inactive"}
4545
phx-click="change-tab"
4646
phx-value-tab="open"
4747
>
4848
<div class="relative flex items-center gap-2.5 text-sm md:text-base">
4949
<div class="truncate">Open</div>
50-
<span class={"min-w-[1ch] font-mono #{if @current_tab == :open, do: "text-emerald-200", else: "text-gray-400 group-hover:text-emerald-200"}"}>
50+
<span class={"min-w-[1ch] font-mono #{if @current_status == :open, do: "text-emerald-200", else: "text-gray-400 group-hover:text-emerald-200"}"}>
5151
{@stats.open_bounties_count}
5252
</span>
5353
</div>
5454
</button>
5555
<button
5656
type="button"
5757
role="tab"
58-
aria-selected={@current_tab == :completed}
59-
class={"inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium #{if @current_tab == :completed, do: "bg-emerald-700 text-white", else: "hover:bg-emerald-700/50"}"}
60-
data-state={if @current_tab == :completed, do: "active", else: "inactive"}
58+
aria-selected={@current_status == :paid}
59+
class={"inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium #{if @current_status == :paid, do: "bg-emerald-700 text-white", else: "hover:bg-emerald-700/50"}"}
60+
data-state={if @current_status == :paid, do: "active", else: "inactive"}
6161
phx-click="change-tab"
6262
phx-value-tab="completed"
6363
>
6464
<div class="relative flex items-center gap-2.5 text-sm md:text-base">
6565
<div class="truncate">Completed</div>
66-
<span class={"min-w-[1ch] font-mono #{if @current_tab == :completed, do: "text-emerald-200", else: "text-gray-400 group-hover:text-emerald-200"}"}>
66+
<span class={"min-w-[1ch] font-mono #{if @current_status == :paid, do: "text-emerald-200", else: "text-gray-400 group-hover:text-emerald-200"}"}>
6767
{@stats.rewarded_bounties_count}
6868
</span>
6969
</div>
@@ -77,7 +77,7 @@ defmodule AlgoraWeb.Org.BountiesLive do
7777
<div class="scrollbar-thin w-full overflow-auto">
7878
<table class="w-full caption-bottom text-sm">
7979
<tbody class="[&_tr:last-child]:border-0">
80-
<%= for %{bounty: bounty, claim_groups: claim_groups} <- @bounties do %>
80+
<%= for %{bounty: bounty, claim_groups: claim_groups} <- @rows do %>
8181
<tr
8282
class="bg-white/[2%] from-white/[2%] via-white/[2%] to-white/[2%] border-b border-white/15 bg-gradient-to-br transition-colors data-[state=selected]:bg-gray-100 hover:bg-gray-100/50 dark:data-[state=selected]:bg-gray-800 dark:hover:bg-white/[2%]"
8383
data-state="false"
@@ -218,13 +218,8 @@ defmodule AlgoraWeb.Org.BountiesLive do
218218
</table>
219219
</div>
220220
</div>
221-
<div :if={@has_more_open and @current_tab == :open} class="flex justify-center mt-4">
222-
<.button variant="ghost" phx-click="load_more_open">
223-
<.icon name="tabler-arrow-down" class="mr-2 h-4 w-4" /> Load More
224-
</.button>
225-
</div>
226-
<div :if={@has_more_paid and @current_tab == :completed} class="flex justify-center mt-4">
227-
<.button variant="ghost" phx-click="load_more_paid">
221+
<div :if={@has_more} class="flex justify-center mt-4">
222+
<.button variant="ghost" phx-click="load_more">
228223
<.icon name="tabler-arrow-down" class="mr-2 h-4 w-4" /> Load More
229224
</.button>
230225
</div>
@@ -240,35 +235,10 @@ defmodule AlgoraWeb.Org.BountiesLive do
240235
{:noreply, push_patch(socket, to: ~p"/org/#{socket.assigns.current_org.handle}/bounties?status=open")}
241236
end
242237

243-
def handle_event("load_more_open", _params, socket) do
244-
%{open_bounties: open_bounties, current_org: current_org} = socket.assigns
245-
246-
last_bounty = List.last(open_bounties)
247-
248-
cursor = %{
249-
inserted_at: last_bounty.inserted_at,
250-
id: last_bounty.id
251-
}
252-
253-
more_bounties =
254-
Bounties.list_bounties(
255-
owner_id: current_org.id,
256-
limit: page_size(),
257-
status: :open,
258-
before: cursor
259-
)
260-
261-
{:noreply,
262-
socket
263-
|> assign(:open_bounties, open_bounties ++ more_bounties)
264-
|> assign(:has_more_open, length(more_bounties) >= page_size())
265-
|> assign_bounties()}
266-
end
267-
268-
def handle_event("load_more_paid", _params, socket) do
269-
%{paid_bounties: paid_bounties, current_org: current_org} = socket.assigns
238+
def handle_event("load_more", _params, socket) do
239+
%{rows: rows, current_org: current_org} = socket.assigns
270240

271-
last_bounty = List.last(paid_bounties)
241+
last_bounty = List.last(rows).bounty
272242

273243
cursor = %{
274244
inserted_at: last_bounty.inserted_at,
@@ -279,46 +249,33 @@ defmodule AlgoraWeb.Org.BountiesLive do
279249
Bounties.list_bounties(
280250
owner_id: current_org.id,
281251
limit: page_size(),
282-
status: :paid,
252+
status: socket.assigns.current_status,
283253
before: cursor
284254
)
285255

286256
{:noreply,
287257
socket
288-
|> assign(:paid_bounties, paid_bounties ++ more_bounties)
289-
|> assign(:has_more_paid, length(more_bounties) >= page_size())
290-
|> assign_bounties()}
258+
|> assign(:rows, rows ++ to_rows(more_bounties))
259+
|> assign(:has_more, length(more_bounties) >= page_size())}
291260
end
292261

293262
def handle_params(params, _uri, socket) do
294-
limit = page_size()
295263
current_org = socket.assigns.current_org
296-
current_tab = get_current_tab(params)
297-
298-
# TODO: fetch only bounties for the current tab
299-
open_bounties = Bounties.list_bounties(owner_id: current_org.id, limit: limit, status: :open)
300-
paid_bounties = Bounties.list_bounties(owner_id: current_org.id, limit: limit, status: :paid)
264+
current_status = get_current_status(params)
301265

302266
stats = Bounties.fetch_stats(current_org.id)
303267

268+
bounties = Bounties.list_bounties(owner_id: current_org.id, limit: page_size(), status: current_status)
269+
304270
{:noreply,
305271
socket
306-
|> assign(:current_tab, current_tab)
307-
|> assign(:open_bounties, open_bounties)
308-
|> assign(:paid_bounties, paid_bounties)
309-
|> assign(:has_more_open, length(open_bounties) >= page_size())
310-
|> assign(:has_more_paid, length(paid_bounties) >= page_size())
311-
|> assign(:stats, stats)
312-
|> assign_bounties()}
272+
|> assign(:current_status, current_status)
273+
|> assign(:rows, to_rows(bounties))
274+
|> assign(:has_more, length(bounties) >= page_size())
275+
|> assign(:stats, stats)}
313276
end
314277

315-
defp assign_bounties(socket) do
316-
bounties =
317-
case socket.assigns.current_tab do
318-
:open -> socket.assigns.open_bounties
319-
:completed -> socket.assigns.paid_bounties
320-
end
321-
278+
defp to_rows(bounties) do
322279
claims_by_ticket =
323280
bounties
324281
|> Enum.map(& &1.ticket.id)
@@ -328,18 +285,15 @@ defmodule AlgoraWeb.Org.BountiesLive do
328285
{ticket_id, Enum.group_by(claims, & &1.group_id)}
329286
end)
330287

331-
bounties =
332-
Enum.map(bounties, fn bounty ->
333-
%{bounty: bounty, claim_groups: Map.get(claims_by_ticket, bounty.ticket.id, %{})}
334-
end)
335-
336-
assign(socket, :bounties, bounties)
288+
Enum.map(bounties, fn bounty ->
289+
%{bounty: bounty, claim_groups: Map.get(claims_by_ticket, bounty.ticket.id, %{})}
290+
end)
337291
end
338292

339-
defp get_current_tab(params) do
293+
defp get_current_status(params) do
340294
case params["status"] do
341295
"open" -> :open
342-
"completed" -> :completed
296+
"completed" -> :paid
343297
_ -> :open
344298
end
345299
end

0 commit comments

Comments
 (0)