@@ -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