Skip to content

Commit 89c4ee1

Browse files
committed
feat: enhance claim processing with pending payment handling
- Updated the claim live module to calculate and display remaining amounts for sponsors. - Introduced logic to conditionally render the reward bounty button based on pending payments. - Refactored context assignment to include current pending payment information for improved user experience.
1 parent 24f8327 commit 89c4ee1

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

lib/algora_web/live/claim_live.ex

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ defmodule AlgoraWeb.ClaimLive do
100100
pledges
101101
|> Enum.map(fn {sponsor_id, {sponsor, pledged}} ->
102102
paid = Map.get(payments, sponsor_id, Money.zero(:USD, no_fraction_if_integer: true))
103-
tipped = Money.sub!(paid, pledged)
103+
tipped = Money.max!(Money.sub!(paid, pledged), Money.zero(:USD, no_fraction_if_integer: true))
104+
remaining = Money.max!(Money.sub!(pledged, paid), Money.zero(:USD, no_fraction_if_integer: true))
104105

105106
status =
106107
cond do
@@ -116,7 +117,8 @@ defmodule AlgoraWeb.ClaimLive do
116117
status: status,
117118
pledged: pledged,
118119
paid: paid,
119-
tipped: tipped
120+
tipped: tipped,
121+
remaining: remaining
120122
}
121123
end)
122124
|> Enum.sort_by(&{&1.pledged, &1.paid, &1.sponsor.name}, :desc)
@@ -133,13 +135,7 @@ defmodule AlgoraWeb.ClaimLive do
133135
context_ids = MapSet.new(contexts, & &1.id)
134136
available_bounties = Enum.filter(primary_claim.target.bounties, &MapSet.member?(context_ids, &1.owner_id))
135137

136-
amount =
137-
case available_bounties do
138-
[] -> nil
139-
[bounty | _] -> Money.to_decimal(bounty.amount)
140-
end
141-
142-
changeset = RewardBountyForm.changeset(%RewardBountyForm{}, %{tip_percentage: 0, amount: amount})
138+
changeset = RewardBountyForm.changeset(%RewardBountyForm{}, %{tip_percentage: 0})
143139

144140
{:ok,
145141
socket
@@ -163,15 +159,15 @@ defmodule AlgoraWeb.ClaimLive do
163159

164160
@impl true
165161
def handle_params(_params, _url, %{assigns: %{current_user: nil}} = socket) do
166-
{:noreply, socket}
162+
{:noreply, assign(socket, :current_context_pending_payment, nil)}
167163
end
168164

169165
def handle_params(%{"context" => context_id}, _url, socket) do
170166
{:noreply, socket |> assign_selected_context(context_id) |> assign_line_items()}
171167
end
172168

173169
def handle_params(_params, _url, socket) do
174-
{:noreply, socket |> assign_selected_context(default_context_id(socket)) |> assign_line_items()}
170+
{:noreply, socket |> assign_selected_context() |> assign_line_items()}
175171
end
176172

177173
@impl true
@@ -221,13 +217,39 @@ defmodule AlgoraWeb.ClaimLive do
221217
end
222218
end
223219

220+
defp assign_reward_bounty_form(socket) do
221+
remaining = socket.assigns.current_context_pending_payment
222+
223+
amount =
224+
if(remaining && Money.positive?(remaining)) do
225+
Money.to_decimal(remaining)
226+
end
227+
228+
form =
229+
socket.assigns.reward_bounty_form.source
230+
|> change(amount: amount)
231+
|> to_form()
232+
233+
assign(socket, :reward_bounty_form, form)
234+
end
235+
236+
defp assign_selected_context(socket), do: assign_selected_context(socket, default_context_id(socket))
237+
224238
defp assign_selected_context(socket, context_id) do
239+
current_context_pending_payment =
240+
if sponsor = Enum.find(socket.assigns.sponsors, &(&1.sponsor.id == context_id)) do
241+
sponsor.remaining
242+
end
243+
225244
case Enum.find(socket.assigns.contexts, &(&1.id == context_id)) do
226245
nil ->
227246
push_patch(socket, to: "/claims/#{socket.assigns.primary_claim.group_id}?context=#{default_context_id(socket)}")
228247

229248
context ->
230-
assign(socket, :selected_context, context)
249+
socket
250+
|> assign(:selected_context, context)
251+
|> assign(:current_context_pending_payment, current_context_pending_payment)
252+
|> assign_reward_bounty_form()
231253
end
232254
end
233255

@@ -340,9 +362,15 @@ defmodule AlgoraWeb.ClaimLive do
340362
<.card_title>
341363
Claim
342364
</.card_title>
343-
<.button phx-click="reward_bounty">
344-
Reward bounty
345-
</.button>
365+
<%= if is_nil(@current_context_pending_payment) or Money.positive?(@current_context_pending_payment) do %>
366+
<.button phx-click="reward_bounty">
367+
Reward bounty
368+
</.button>
369+
<% else %>
370+
<.badge variant="success">
371+
<.icon name="tabler-check" class="size-4 mr-2 -ml-1" /> Rewarded
372+
</.badge>
373+
<% end %>
346374
</div>
347375
</.card_header>
348376
<.card_content>

0 commit comments

Comments
 (0)