Skip to content

Commit 7a8832f

Browse files
committed
refactor: unify form handling in DashboardLive
- Consolidated form handling by replacing individual form assignments with a single `@form` variable for contract, tip, and bounty forms. - Updated the `create_tip` function to use `selected_developer` instead of fetching the recipient from the token. - Enhanced the share drawer logic to dynamically assign the correct form based on the selected share drawer type, improving code maintainability and readability.
1 parent 31adc5d commit 7a8832f

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

lib/algora_web/live/org/dashboard_live.ex

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,16 @@ defmodule AlgoraWeb.Org.DashboardLive do
532532
|> Map.put(:action, :validate)
533533

534534
with %{valid?: true} <- changeset,
535-
{:ok, token} <- Accounts.get_access_token(socket.assigns.current_user),
536-
{:ok, recipient} <- Workspace.ensure_user(token, get_field(changeset, :github_handle)),
537535
{:ok, checkout_url} <-
538-
Bounties.create_tip(%{
539-
creator: socket.assigns.current_user,
540-
owner: socket.assigns.current_org,
541-
recipient: recipient,
542-
amount: get_field(changeset, :amount)
543-
}) do
536+
Bounties.create_tip(
537+
%{
538+
creator: socket.assigns.current_user,
539+
owner: socket.assigns.current_org,
540+
recipient: socket.assigns.selected_developer,
541+
amount: get_field(changeset, :amount)
542+
},
543+
ticket_ref: get_field(changeset, :ticket_ref)
544+
) do
544545
{:noreply, redirect(socket, external: checkout_url)}
545546
else
546547
%{valid?: false} ->
@@ -579,16 +580,6 @@ defmodule AlgoraWeb.Org.DashboardLive do
579580
{:noreply, assign(socket, :show_share_drawer, false)}
580581
end
581582

582-
@impl true
583-
def handle_event("validate_contract", %{"contract_form" => params}, socket) do
584-
changeset =
585-
%ContractForm{}
586-
|> ContractForm.changeset(params)
587-
|> Map.put(:action, :validate)
588-
589-
{:noreply, assign(socket, :contract_form, to_form(changeset))}
590-
end
591-
592583
@impl true
593584
def handle_event("create_contract", %{"contract_form" => params}, socket) do
594585
changeset = ContractForm.changeset(%ContractForm{}, params)
@@ -1235,12 +1226,8 @@ defmodule AlgoraWeb.Org.DashboardLive do
12351226
</.card_header>
12361227
<.card_content>
12371228
<div class="space-y-4">
1238-
<.input
1239-
label="Hourly Rate"
1240-
icon="tabler-currency-dollar"
1241-
field={@contract_form[:hourly_rate]}
1242-
/>
1243-
<.input label="Hours per Week" field={@contract_form[:hours_per_week]} />
1229+
<.input label="Hourly Rate" icon="tabler-currency-dollar" field={@form[:hourly_rate]} />
1230+
<.input label="Hours per Week" field={@form[:hours_per_week]} />
12441231
</div>
12451232
</.card_content>
12461233
</.card>
@@ -1266,10 +1253,10 @@ defmodule AlgoraWeb.Org.DashboardLive do
12661253
<div class="space-y-4">
12671254
<.input
12681255
label="URL"
1269-
field={@bounty_form[:url]}
1256+
field={@form[:url]}
12701257
placeholder="https://github.com/owner/repo/issues/123"
12711258
/>
1272-
<.input label="Amount" icon="tabler-currency-dollar" field={@bounty_form[:amount]} />
1259+
<.input label="Amount" icon="tabler-currency-dollar" field={@form[:amount]} />
12731260
</div>
12741261
</.card_content>
12751262
</.card>
@@ -1293,17 +1280,17 @@ defmodule AlgoraWeb.Org.DashboardLive do
12931280
</.card_header>
12941281
<.card_content>
12951282
<div class="space-y-4">
1296-
<.input label="Amount" icon="tabler-currency-dollar" field={@tip_form[:amount]} />
1283+
<.input label="Amount" icon="tabler-currency-dollar" field={@form[:amount]} />
12971284
<.input
12981285
label="URL"
1299-
field={@tip_form[:url]}
1286+
field={@form[:url]}
13001287
placeholder="https://github.com/owner/repo/issues/123"
13011288
helptext="We'll add a comment to the issue to notify the developer."
13021289
/>
13031290
<%!-- # TODO: implement --%>
13041291
<.input
13051292
label="Review (optional)"
1306-
field={@tip_form[:message]}
1293+
field={@form[:message]}
13071294
placeholder="Thanks for your great work!"
13081295
/>
13091296
</div>
@@ -1394,6 +1381,27 @@ defmodule AlgoraWeb.Org.DashboardLive do
13941381
end
13951382

13961383
defp share_drawer(assigns) do
1384+
assigns =
1385+
case assigns.share_drawer_type do
1386+
nil ->
1387+
assigns
1388+
1389+
"contract" ->
1390+
assigns
1391+
|> assign(:phx_submit, "create_contract")
1392+
|> assign(:form, assigns.contract_form)
1393+
1394+
"tip" ->
1395+
assigns
1396+
|> assign(:phx_submit, "create_tip")
1397+
|> assign(:form, assigns.tip_form)
1398+
1399+
"bounty" ->
1400+
assigns
1401+
|> assign(:phx_submit, "create_bounty")
1402+
|> assign(:form, assigns.bounty_form)
1403+
end
1404+
13971405
~H"""
13981406
<.drawer show={@show_share_drawer} direction="right" on_cancel="close_share_drawer">
13991407
<.share_drawer_header
@@ -1402,7 +1410,7 @@ defmodule AlgoraWeb.Org.DashboardLive do
14021410
share_drawer_type={@share_drawer_type}
14031411
/>
14041412
<.drawer_content :if={@selected_developer} class="mt-4">
1405-
<.form for={@contract_form} phx-change="validate_contract" phx-submit="create_contract">
1413+
<.form for={@form} phx-submit={@phx_submit}>
14061414
<div class="flex flex-col gap-8">
14071415
<.share_drawer_developer_info selected_developer={@selected_developer} />
14081416
<%= if incomplete?(@achievements, :connect_github_status) do %>
@@ -1412,9 +1420,7 @@ defmodule AlgoraWeb.Org.DashboardLive do
14121420
<.share_drawer_content
14131421
:if={@selected_developer}
14141422
share_drawer_type={@share_drawer_type}
1415-
contract_form={@contract_form}
1416-
tip_form={@tip_form}
1417-
bounty_form={@bounty_form}
1423+
form={@form}
14181424
/>
14191425
</div>
14201426
<.alert
@@ -1434,9 +1440,7 @@ defmodule AlgoraWeb.Org.DashboardLive do
14341440
<.share_drawer_content
14351441
:if={@selected_developer}
14361442
share_drawer_type={@share_drawer_type}
1437-
contract_form={@contract_form}
1438-
tip_form={@tip_form}
1439-
bounty_form={@bounty_form}
1443+
form={@form}
14401444
/>
14411445
<% end %>
14421446
</div>

0 commit comments

Comments
 (0)