Skip to content

Commit e1c0c60

Browse files
committed
miscellanea
1 parent 9b52008 commit e1c0c60

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

lib/algora_web/live/claim_live.ex

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,31 +116,41 @@ defmodule AlgoraWeb.ClaimLive do
116116
end
117117

118118
@impl true
119-
def handle_event("reward_bounty", _params, socket) do
120-
claim = socket.assigns.primary_claim
119+
def handle_event("reward_bounty", _params, %{assigns: %{current_user: nil}} = socket) do
120+
{:noreply,
121+
redirect(socket, to: ~p"/auth/login?#{%{return_to: ~p"/claims/#{socket.assigns.primary_claim.group_id}"}}")}
122+
end
123+
124+
def handle_event("reward_bounty", _params, %{assigns: %{current_user: current_user, target: target}} = socket) do
125+
user_org_ids = MapSet.new([current_user | Algora.Organizations.get_user_orgs(current_user)], & &1.id)
121126

122-
# TODO: use the correct bounty
123-
bounty = hd(claim.target.bounties)
127+
# TODO: allow user to choose if multiple bounties are available
128+
case Enum.find(target.bounties, &MapSet.member?(user_org_ids, &1.owner_id)) do
129+
nil ->
130+
# TODO: allow user to add a bounty
131+
{:noreply, put_flash(socket, :error, "You are not authorized to reward this bounty")}
124132

125-
case Algora.Bounties.reward_bounty(
126-
%{
127-
# TODO: handle unauthenticated user
128-
creator: socket.assigns.current_user,
129-
amount: bounty.amount,
130-
bounty_id: bounty.id,
131-
claims: socket.assigns.claims
132-
},
133-
ticket_ref: %{
134-
owner: claim.target.repository.user.provider_login,
135-
repo: claim.target.repository.name,
136-
number: claim.target.number
137-
}
138-
) do
139-
{:ok, session_url} ->
140-
{:noreply, redirect(socket, external: session_url)}
133+
bounty ->
134+
case Algora.Bounties.reward_bounty(
135+
%{
136+
creator: current_user,
137+
# TODO: allow user to choose amount
138+
amount: bounty.amount,
139+
bounty_id: bounty.id,
140+
claims: socket.assigns.claims
141+
},
142+
ticket_ref: %{
143+
owner: target.repository.user.provider_login,
144+
repo: target.repository.name,
145+
number: target.number
146+
}
147+
) do
148+
{:ok, session_url} ->
149+
{:noreply, redirect(socket, external: session_url)}
141150

142-
{:error, _reason} ->
143-
{:noreply, put_flash(socket, :error, "Failed to create payment session. Please try again later.")}
151+
{:error, _reason} ->
152+
{:noreply, put_flash(socket, :error, "Failed to create payment session. Please try again later.")}
153+
end
144154
end
145155
end
146156

0 commit comments

Comments
 (0)