Skip to content

Commit d028738

Browse files
committed
refactor: streamline bounty form handling and event logic
- Simplified the `input` function in `CoreComponents` by removing unnecessary debugging code. - Enhanced the `RepoNav` module by introducing a conditional `main_bounty_form` assignment based on user permissions, improving clarity and maintainability. - Refactored event handling for bounty creation, ensuring better error handling and user feedback during the process.
1 parent 33b5e7b commit d028738

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

lib/algora_web/components/core_components.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ defmodule AlgoraWeb.CoreComponents do
762762

763763
def input(%{type: "radio", value: value} = assigns) do
764764
assigns =
765-
assign_new(assigns, :checked, fn -> "radio" |> Phoenix.HTML.Form.normalize_value(value) |> dbg() end)
765+
assign_new(assigns, :checked, fn -> Phoenix.HTML.Form.normalize_value("radio", value) end)
766766

767767
~H"""
768768
<div>

lib/algora_web/live/org/nav.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ defmodule AlgoraWeb.Org.Nav do
6161

6262
case apply_action(changeset, :save) do
6363
{:ok, data} ->
64-
dbg(data)
65-
6664
bounty_res =
6765
case data.type do
6866
:github ->

lib/algora_web/live/org/repo_nav.ex

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,99 @@ defmodule AlgoraWeb.Org.RepoNav do
1313
current_org = Organizations.get_org_by(provider_login: repo_owner, provider: "github")
1414
current_user_role = OrgAuth.get_user_role(current_user, current_org)
1515

16+
main_bounty_form =
17+
if Member.can_create_bounty?(current_user_role) do
18+
to_form(BountyForm.changeset(%BountyForm{}, %{}))
19+
end
20+
1621
{:cont,
1722
socket
1823
|> assign(:screenshot?, not is_nil(params["screenshot"]))
19-
|> assign(:main_bounty_form, to_form(BountyForm.changeset(%BountyForm{}, %{})))
24+
|> assign(:main_bounty_form, main_bounty_form)
2025
|> assign(:main_bounty_form_open?, false)
2126
|> assign(:current_org, current_org)
2227
|> assign(:current_user_role, current_user_role)
2328
|> assign(:nav, nav_items(current_org.handle, current_user_role))
2429
|> assign(:contacts, [])
2530
|> attach_hook(:active_tab, :handle_params, &handle_active_tab_params/3)
26-
|> attach_hook(:form_toggle, :handle_event, &handle_form_toggle_event/3)}
31+
|> attach_hook(:handle_event, :handle_event, &handle_event/3)}
2732
end
2833

29-
# TODO: handle submit
30-
# TODO: handle validate
34+
defp handle_event("create_bounty_main", %{"bounty_form" => params}, socket) do
35+
changeset = BountyForm.changeset(%BountyForm{}, params)
36+
37+
case apply_action(changeset, :save) do
38+
{:ok, data} ->
39+
bounty_res =
40+
case data.type do
41+
:github ->
42+
Bounties.create_bounty(
43+
%{
44+
creator: socket.assigns.current_user,
45+
owner: socket.assigns.current_org,
46+
amount: data.amount,
47+
ticket_ref: %{
48+
owner: data.ticket_ref.owner,
49+
repo: data.ticket_ref.repo,
50+
number: data.ticket_ref.number
51+
}
52+
},
53+
visibility: get_field(changeset, :visibility),
54+
shared_with: get_field(changeset, :shared_with)
55+
)
56+
57+
:custom ->
58+
Bounties.create_bounty(
59+
%{
60+
creator: socket.assigns.current_user,
61+
owner: socket.assigns.current_org,
62+
amount: data.amount,
63+
title: data.title,
64+
description: data.description
65+
},
66+
visibility: get_field(changeset, :visibility),
67+
shared_with: get_field(changeset, :shared_with)
68+
)
69+
end
70+
71+
case bounty_res do
72+
{:ok, bounty} ->
73+
to =
74+
case data.type do
75+
:github ->
76+
~p"/#{data.ticket_ref.owner}/#{data.ticket_ref.repo}/issues/#{data.ticket_ref.number}"
77+
78+
:custom ->
79+
~p"/org/#{socket.assigns.current_org.handle}/bounties/#{bounty.id}"
80+
end
81+
82+
{:cont, redirect(socket, to: to)}
3183

32-
defp handle_form_toggle_event("open_main_bounty_form", _params, socket) do
84+
{:error, :already_exists} ->
85+
{:cont, put_flash(socket, :warning, "You already have a bounty for this ticket")}
86+
87+
{:error, reason} ->
88+
Logger.error("Failed to create bounty: #{inspect(reason)}")
89+
{:cont, put_flash(socket, :error, "Something went wrong")}
90+
end
91+
92+
{:error, changeset} ->
93+
{:cont, assign(socket, :main_bounty_form, to_form(changeset))}
94+
end
95+
end
96+
97+
defp handle_event("open_main_bounty_form", _params, socket) do
3398
{:cont, assign(socket, :main_bounty_form_open?, true)}
3499
end
35100

36-
defp handle_form_toggle_event("close_main_bounty_form", _params, socket) do
101+
defp handle_event("close_main_bounty_form", _params, socket) do
37102
{:cont, assign(socket, :main_bounty_form_open?, false)}
38103
end
39104

105+
defp handle_event(_event, _params, socket) do
106+
{:cont, socket}
107+
end
108+
40109
defp handle_active_tab_params(_params, _url, socket) do
41110
active_tab =
42111
case {socket.view, socket.assigns.live_action} do

0 commit comments

Comments
 (0)