Skip to content

Commit c17705a

Browse files
committed
finalize reward bounty form
1 parent d0fbaab commit c17705a

File tree

5 files changed

+262
-247
lines changed

5 files changed

+262
-247
lines changed

lib/algora/bounties/bounties.ex

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ defmodule Algora.Bounties do
88
alias Algora.Bounties.Bounty
99
alias Algora.Bounties.Claim
1010
alias Algora.Bounties.Jobs
11+
alias Algora.Bounties.LineItem
1112
alias Algora.Bounties.Tip
1213
alias Algora.FeeTier
1314
alias Algora.Github
14-
alias Algora.MoneyUtils
1515
alias Algora.Organizations.Member
1616
alias Algora.Payments
1717
alias Algora.Payments.Transaction
@@ -284,45 +284,6 @@ defmodule Algora.Bounties do
284284
)
285285
end
286286

287-
# TODO: move to separate module
288-
defmodule LineItem do
289-
@moduledoc false
290-
defstruct [:amount, :title, :description, :image, :type]
291-
292-
@type t :: %__MODULE__{
293-
amount: Money.t(),
294-
title: String.t(),
295-
description: String.t() | nil,
296-
image: String.t() | nil,
297-
type: :payment | :fee
298-
}
299-
300-
def to_stripe(line_item) do
301-
%{
302-
price_data: %{
303-
unit_amount: MoneyUtils.to_minor_units(line_item.amount),
304-
currency: to_string(line_item.amount.currency),
305-
product_data: %{
306-
name: line_item.title,
307-
description: line_item.description,
308-
images: if(line_item.image, do: [line_item.image])
309-
}
310-
},
311-
quantity: 1
312-
}
313-
end
314-
315-
def gross_amount(line_items) do
316-
Enum.reduce(line_items, Money.zero(:USD), fn item, acc -> Money.add!(acc, item.amount) end)
317-
end
318-
319-
def total_fee(line_items) do
320-
Enum.reduce(line_items, Money.zero(:USD), fn item, acc ->
321-
if item.type == :fee, do: Money.add!(acc, item.amount), else: acc
322-
end)
323-
end
324-
end
325-
326287
@spec generate_line_items(
327288
%{amount: Money.t()},
328289
opts :: [
@@ -349,7 +310,7 @@ defmodule Algora.Bounties do
349310
title: "Payment to @#{recipient.provider_login}",
350311
description: description,
351312
image: recipient.avatar_url,
352-
type: :payment
313+
type: :payout
353314
}
354315
]
355316
else
@@ -362,7 +323,7 @@ defmodule Algora.Bounties do
362323
title: "Payment to @#{claim.user.provider_login}",
363324
description: description,
364325
image: claim.user.avatar_url,
365-
type: :payment
326+
type: :payout
366327
}
367328
end) ++
368329
[
@@ -426,7 +387,9 @@ defmodule Algora.Bounties do
426387
group_id: tx_group_id
427388
}),
428389
{:ok, session} <-
429-
Payments.create_stripe_session(LineItem.to_stripe(line_items), %{
390+
line_items
391+
|> Enum.map(&LineItem.to_stripe/1)
392+
|> Payments.create_stripe_session(%{
430393
description: description,
431394
metadata: %{"version" => "2", "group_id" => tx_group_id}
432395
}) do
@@ -458,7 +421,7 @@ defmodule Algora.Bounties do
458421
gross_amount: gross_amount,
459422
net_amount: net_amount,
460423
total_fee: total_fee,
461-
line_items: line_items,
424+
line_items: Util.normalize_struct(line_items),
462425
group_id: group_id
463426
})
464427
|> Algora.Validations.validate_positive(:gross_amount)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
defmodule Algora.Bounties.LineItem do
2+
@moduledoc false
3+
use Algora.Schema
4+
5+
alias Algora.MoneyUtils
6+
7+
@primary_key false
8+
typed_embedded_schema do
9+
field :amount, Algora.Types.Money
10+
field :title, :string
11+
field :description, :string
12+
field :image, :string
13+
field :type, Ecto.Enum, values: [:payout, :fee]
14+
end
15+
16+
def to_stripe(line_item) do
17+
%{
18+
price_data: %{
19+
unit_amount: MoneyUtils.to_minor_units(line_item.amount),
20+
currency: to_string(line_item.amount.currency),
21+
product_data:
22+
Map.reject(
23+
%{
24+
name: line_item.title,
25+
description: line_item.description,
26+
images: if(line_item.image, do: [line_item.image])
27+
},
28+
fn {_, v} -> is_nil(v) end
29+
)
30+
},
31+
quantity: 1
32+
}
33+
end
34+
35+
def gross_amount(line_items) do
36+
Enum.reduce(line_items, Money.zero(:USD), fn item, acc -> Money.add!(acc, item.amount) end)
37+
end
38+
39+
def total_fee(line_items) do
40+
Enum.reduce(line_items, Money.zero(:USD), fn item, acc ->
41+
if item.type == :fee, do: Money.add!(acc, item.amount), else: acc
42+
end)
43+
end
44+
end

lib/algora_web/components/core_components.ex

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ defmodule AlgoraWeb.CoreComponents do
574574
phx-click={JS.push("lv:clear-flash", value: %{key: @kind}) |> hide("##{@id}")}
575575
role="alert"
576576
class={[
577-
"fixed right-4 bottom-4 z-50 hidden w-80 rounded-lg p-3 pr-8 shadow-md ring-1 sm:w-96",
577+
"fixed right-4 bottom-4 z-[1000] hidden w-80 rounded-lg p-3 pr-8 shadow-md ring-1 sm:w-96",
578578
@kind == :info &&
579579
"bg-emerald-950 fill-success-foreground text-success-foreground ring ring-success/70",
580580
@kind == :warning &&
@@ -733,10 +733,7 @@ defmodule AlgoraWeb.CoreComponents do
733733
slot :inner_block
734734

735735
def input(%{field: %FormField{} = field} = assigns) do
736-
errors =
737-
if Phoenix.Component.used_input?(field) and not assigns.hide_errors,
738-
do: field.errors,
739-
else: []
736+
errors = if assigns.hide_errors, do: [], else: field.errors
740737

741738
value =
742739
with %Money{} <- field.value,

lib/algora_web/components/ui/radio_group.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ defmodule AlgoraWeb.Components.UI.RadioGroup do
1717
1818
"""
1919
attr :name, :string, default: nil
20-
attr :value, :any, default: nil
2120
attr :options, :list, default: [], doc: "List of {label, value} tuples"
2221
attr :field, Phoenix.HTML.FormField, doc: "a form field struct retrieved from the form"
2322
attr :class, :string, default: nil
@@ -32,7 +31,12 @@ defmodule AlgoraWeb.Components.UI.RadioGroup do
3231
"border-border has-[:checked]:border-primary has-[:checked]:bg-primary/10"
3332
]}>
3433
<div class="sr-only">
35-
<.input field={@field} type="radio" value={value} checked={to_string(@value) == value} />
34+
<.input
35+
field={@field}
36+
type="radio"
37+
value={value}
38+
checked={to_string(@field.value) == to_string(value)}
39+
/>
3640
</div>
3741
<span class="flex flex-1 items-center justify-between">
3842
<span class="text-sm font-medium">{label}</span>
@@ -44,6 +48,7 @@ defmodule AlgoraWeb.Components.UI.RadioGroup do
4448
</label>
4549
<% end %>
4650
</div>
51+
<.error :for={msg <- @field.errors}>{translate_error(msg)}</.error>
4752
"""
4853
end
4954
end

0 commit comments

Comments
 (0)