@@ -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 } #{ data . ticket_ref . number } "  
77+ 
78+                 :custom  -> 
79+                   ~p" /org/#{ socket . assigns . current_org . handle } #{ 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