@@ -5,8 +5,31 @@ defmodule AlgoraWeb.User.TransactionsLive do
5
5
6
6
alias Algora.Accounts.User
7
7
alias Algora.Payments
8
+ alias Algora.Stripe.ConnectCountries
8
9
alias Algora.Util
9
10
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
+
10
33
def mount ( _params , _session , socket ) do
11
34
if connected? ( socket ) do
12
35
Payments . subscribe ( )
@@ -15,13 +38,44 @@ defmodule AlgoraWeb.User.TransactionsLive do
15
38
{ :ok ,
16
39
socket
17
40
|> assign ( :page_title , "Your transactions" )
41
+ |> assign ( :show_payout_drawer , true )
42
+ |> assign ( :payout_account_form , to_form ( PayoutAccountForm . changeset ( % PayoutAccountForm { } , % { } ) ) )
18
43
|> assign_transactions ( ) }
19
44
end
20
45
21
46
def handle_info ( :payments_updated , socket ) do
22
47
{ :noreply , assign_transactions ( socket ) }
23
48
end
24
49
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
+
25
79
defp assign_transactions ( socket ) do
26
80
transactions =
27
81
Payments . list_transactions (
@@ -66,9 +120,15 @@ defmodule AlgoraWeb.User.TransactionsLive do
66
120
def render ( assigns ) do
67
121
~H"""
68
122
< 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 >
72
132
</ div >
73
133
74
134
<!-- Totals Cards -->
@@ -162,6 +222,39 @@ defmodule AlgoraWeb.User.TransactionsLive do
162
222
</ . card_content >
163
223
</ . card >
164
224
</ 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 >
165
258
"""
166
259
end
167
260
0 commit comments