Skip to content

Commit 3459241

Browse files
committed
add create payout account form
1 parent e6c2d17 commit 3459241

File tree

2 files changed

+224
-3
lines changed

2 files changed

+224
-3
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
defmodule Algora.Stripe.ConnectCountries do
2+
@moduledoc false
3+
4+
def list,
5+
do: [
6+
{"Albania", "AL"},
7+
{"Algeria", "DZ"},
8+
{"Angola", "AO"},
9+
{"Antigua and Barbuda", "AG"},
10+
{"Argentina", "AR"},
11+
{"Armenia", "AM"},
12+
{"Australia", "AU"},
13+
{"Austria", "AT"},
14+
{"Azerbaijan", "AZ"},
15+
{"Bahamas", "BS"},
16+
{"Bahrain", "BH"},
17+
{"Bangladesh", "BD"},
18+
{"Belgium", "BE"},
19+
{"Benin", "BJ"},
20+
{"Bhutan", "BT"},
21+
{"Bolivia", "BO"},
22+
{"Bosnia and Herzegovina", "BA"},
23+
{"Botswana", "BW"},
24+
{"Brazil", "BR"},
25+
{"Brunei", "BN"},
26+
{"Bulgaria", "BG"},
27+
{"Cambodia", "KH"},
28+
{"Canada", "CA"},
29+
{"Chile", "CL"},
30+
{"China", "CN"},
31+
{"Colombia", "CO"},
32+
{"Costa Rica", "CR"},
33+
{"Croatia", "HR"},
34+
{"Cyprus", "CY"},
35+
{"Czech Republic", "CZ"},
36+
{"Denmark", "DK"},
37+
{"Dominican Republic", "DO"},
38+
{"Ecuador", "EC"},
39+
{"Egypt", "EG"},
40+
{"El Salvador", "SV"},
41+
{"Estonia", "EE"},
42+
{"Ethiopia", "ET"},
43+
{"Finland", "FI"},
44+
{"France", "FR"},
45+
{"Gabon", "GA"},
46+
{"Gambia", "GM"},
47+
{"Germany", "DE"},
48+
{"Ghana", "GH"},
49+
{"Gibraltar", "GI"},
50+
{"Greece", "GR"},
51+
{"Guatemala", "GT"},
52+
{"Guyana", "GY"},
53+
{"Hong Kong", "HK"},
54+
{"Hungary", "HU"},
55+
{"Iceland", "IS"},
56+
{"India", "IN"},
57+
{"Indonesia", "ID"},
58+
{"Ireland", "IE"},
59+
{"Israel", "IL"},
60+
{"Italy", "IT"},
61+
{"Ivory Coast", "CI"},
62+
{"Jamaica", "JM"},
63+
{"Japan", "JP"},
64+
{"Jordan", "JO"},
65+
{"Kazakhstan", "KZ"},
66+
{"Kenya", "KE"},
67+
{"Kuwait", "KW"},
68+
{"Laos", "LA"},
69+
{"Latvia", "LV"},
70+
{"Liechtenstein", "LI"},
71+
{"Lithuania", "LT"},
72+
{"Luxembourg", "LU"},
73+
{"Macao", "MO"},
74+
{"Macedonia", "MK"},
75+
{"Madagascar", "MG"},
76+
{"Malaysia", "MY"},
77+
{"Malta", "MT"},
78+
{"Mauritius", "MU"},
79+
{"Mexico", "MX"},
80+
{"Moldova", "MD"},
81+
{"Monaco", "MC"},
82+
{"Mongolia", "MN"},
83+
{"Morocco", "MA"},
84+
{"Mozambique", "MZ"},
85+
{"Namibia", "NA"},
86+
{"Netherlands", "NL"},
87+
{"New Zealand", "NZ"},
88+
{"Nigeria", "NG"},
89+
{"Norway", "NO"},
90+
{"Oman", "OM"},
91+
{"Pakistan", "PK"},
92+
{"Panama", "PA"},
93+
{"Paraguay", "PY"},
94+
{"Peru", "PE"},
95+
{"Philippines", "PH"},
96+
{"Poland", "PL"},
97+
{"Portugal", "PT"},
98+
{"Qatar", "QA"},
99+
{"Romania", "RO"},
100+
{"Rwanda", "RW"},
101+
{"Saint Lucia", "LC"},
102+
{"San Marino", "SM"},
103+
{"Saudi Arabia", "SA"},
104+
{"Senegal", "SN"},
105+
{"Serbia", "RS"},
106+
{"Singapore", "SG"},
107+
{"Slovakia", "SK"},
108+
{"Slovenia", "SI"},
109+
{"South Africa", "ZA"},
110+
{"South Korea", "KR"},
111+
{"Spain", "ES"},
112+
{"Sri Lanka", "LK"},
113+
{"Sweden", "SE"},
114+
{"Switzerland", "CH"},
115+
{"Taiwan", "TW"},
116+
{"Tanzania", "TZ"},
117+
{"Thailand", "TH"},
118+
{"Trinidad and Tobago", "TT"},
119+
{"Tunisia", "TN"},
120+
{"Turkey", "TR"},
121+
{"United Arab Emirates", "AE"},
122+
{"United Kingdom", "GB"},
123+
{"United States", "US"},
124+
{"Uruguay", "UY"},
125+
{"Uzbekistan", "UZ"},
126+
{"Vietnam", "VN"}
127+
]
128+
end

lib/algora_web/live/user/transactions_live.ex

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,31 @@ defmodule AlgoraWeb.User.TransactionsLive do
55

66
alias Algora.Accounts.User
77
alias Algora.Payments
8+
alias Algora.Stripe.ConnectCountries
89
alias Algora.Util
910

11+
defmodule PayoutAccountForm do
12+
@moduledoc false
13+
use Ecto.Schema
14+
15+
import Ecto.Changeset
16+
17+
@countries ConnectCountries.list()
18+
19+
embedded_schema do
20+
field :country, :string
21+
end
22+
23+
def changeset(schema \\ %__MODULE__{}, attrs) do
24+
schema
25+
|> cast(attrs, [:country])
26+
|> validate_required([:country])
27+
|> validate_inclusion(:country, Enum.map(@countries, &elem(&1, 1)))
28+
end
29+
30+
def countries, do: @countries
31+
end
32+
1033
def mount(_params, _session, socket) do
1134
if connected?(socket) do
1235
Payments.subscribe()
@@ -15,13 +38,44 @@ defmodule AlgoraWeb.User.TransactionsLive do
1538
{:ok,
1639
socket
1740
|> assign(:page_title, "Your transactions")
41+
|> assign(:show_payout_drawer, true)
42+
|> assign(:payout_account_form, to_form(PayoutAccountForm.changeset(%PayoutAccountForm{}, %{})))
1843
|> assign_transactions()}
1944
end
2045

2146
def handle_info(:payments_updated, socket) do
2247
{:noreply, assign_transactions(socket)}
2348
end
2449

50+
def handle_event("show_payout_drawer", _params, socket) do
51+
{:noreply, assign(socket, :show_payout_drawer, true)}
52+
end
53+
54+
def handle_event("close_drawer", _params, socket) do
55+
{:noreply, assign(socket, :show_payout_drawer, false)}
56+
end
57+
58+
def handle_event("create_payout_account", %{"payout_account_form" => params}, socket) do
59+
changeset =
60+
%PayoutAccountForm{}
61+
|> PayoutAccountForm.changeset(params)
62+
|> Map.put(:action, :validate)
63+
64+
case changeset do
65+
%{valid?: true} = changeset ->
66+
# TODO: Actually create the payout account
67+
IO.inspect(changeset.changes, label: "Would create payout account with")
68+
69+
{:noreply,
70+
socket
71+
|> put_flash(:info, "Payout account created!")
72+
|> assign(:show_payout_drawer, false)}
73+
74+
%{valid?: false} = changeset ->
75+
{:noreply, assign(socket, :payout_account_form, to_form(changeset))}
76+
end
77+
end
78+
2579
defp assign_transactions(socket) do
2680
transactions =
2781
Payments.list_transactions(
@@ -66,9 +120,15 @@ defmodule AlgoraWeb.User.TransactionsLive do
66120
def render(assigns) do
67121
~H"""
68122
<div class="container mx-auto max-w-7xl space-y-6 p-6">
69-
<div class="space-y-1">
70-
<h1 class="text-2xl font-bold">Your Transactions</h1>
71-
<p class="text-muted-foreground">View and manage your transaction history</p>
123+
<div class="flex items-end justify-between gap-4">
124+
<div class="space-y-1">
125+
<h1 class="text-2xl font-bold">Your Transactions</h1>
126+
<p class="text-muted-foreground">View and manage your transaction history</p>
127+
</div>
128+
<.button phx-click="show_payout_drawer">
129+
<.icon name="tabler-plus" class="w-4 h-4 mr-2 -ml-1" />
130+
<span>Create payout account</span>
131+
</.button>
72132
</div>
73133
74134
<!-- Totals Cards -->
@@ -162,6 +222,39 @@ defmodule AlgoraWeb.User.TransactionsLive do
162222
</.card_content>
163223
</.card>
164224
</div>
225+
<.drawer show={@show_payout_drawer} on_cancel="close_drawer" direction="right">
226+
<.drawer_header>
227+
<.drawer_title>Create Payout Account</.drawer_title>
228+
<.drawer_description>Create a payout account to receive your earnings</.drawer_description>
229+
</.drawer_header>
230+
<.drawer_content class="mt-4">
231+
<.simple_form for={@payout_account_form} phx-submit="create_payout_account">
232+
<div class="space-y-6 max-w-md">
233+
<.input
234+
field={@payout_account_form[:country]}
235+
label="Country"
236+
type="select"
237+
prompt=""
238+
options={
239+
PayoutAccountForm.countries()
240+
|> Enum.map(fn {name, code} ->
241+
{Algora.Misc.CountryEmojis.get(code, "🌎") <> " " <> name, code}
242+
end)
243+
}
244+
helptext="Select the country where you or your business will legally operate."
245+
/>
246+
<div class="flex justify-end gap-4">
247+
<.button variant="outline" type="button" phx-click="close_drawer">
248+
Cancel
249+
</.button>
250+
<.button type="submit">
251+
Create Account
252+
</.button>
253+
</div>
254+
</div>
255+
</.simple_form>
256+
</.drawer_content>
257+
</.drawer>
165258
"""
166259
end
167260

0 commit comments

Comments
 (0)