@@ -27,6 +27,7 @@ defmodule AlgoraWeb.Org.DashboardLive do
2727 alias AlgoraWeb.Forms.BountyForm
2828 alias AlgoraWeb.Forms.ContractForm
2929 alias AlgoraWeb.Forms.TipForm
30+ alias AlgoraWeb.LocalStore
3031
3132 require Logger
3233
@@ -67,10 +68,6 @@ defmodule AlgoraWeb.Org.DashboardLive do
6768 % { current_org: current_org } = socket . assigns
6869
6970 if Member . can_create_bounty? ( socket . assigns . current_user_role ) do
70- if connected? ( socket ) do
71- Phoenix.PubSub . subscribe ( Algora.PubSub , "auth:#{ socket . id } " )
72- end
73-
7471 previewed_user = get_previewed_user ( current_org )
7572
7673 _experts = Accounts . list_developers ( org_id: current_org . id , earnings_gt: Money . zero ( :USD ) )
@@ -96,6 +93,8 @@ defmodule AlgoraWeb.Org.DashboardLive do
9693 |> Enum . concat ( experts )
9794 |> Enum . concat ( Enum . map ( matches , & & 1 . user ) )
9895
96+ oauth_url = Github . authorize_url ( % { return_to: "/#{ current_org . handle } /dashboard" } )
97+
9998 { :ok ,
10099 socket
101100 |> assign ( :page_title , current_org . name )
@@ -104,6 +103,7 @@ defmodule AlgoraWeb.Org.DashboardLive do
104103 "Share bounties, tips or contracts with #{ header_prefix ( current_org ) } contributors and Algora matches"
105104 )
106105 |> assign ( :screenshot? , not is_nil ( params [ "screenshot" ] ) )
106+ |> assign ( :pending_action , nil )
107107 |> assign ( :ip_address , AlgoraWeb.Util . get_ip ( socket ) )
108108 |> assign ( :admins_last_active , admins_last_active )
109109 |> assign ( :has_fresh_token? , Accounts . has_fresh_token? ( socket . assigns . current_user ) )
@@ -115,7 +115,7 @@ defmodule AlgoraWeb.Org.DashboardLive do
115115 |> assign ( :contributions , contributions )
116116 |> assign ( :developers , developers )
117117 |> assign ( :has_more_bounties , false )
118- |> assign ( :oauth_url , Github . authorize_url ( % { socket_id: socket . id } ) )
118+ |> assign ( :oauth_url , oauth_url )
119119 |> assign ( :bounty_form , to_form ( BountyForm . changeset ( % BountyForm { } , % { } ) ) )
120120 |> assign ( :tip_form , to_form ( TipForm . changeset ( % TipForm { } , % { } ) ) )
121121 |> assign ( :contract_form , to_form ( ContractForm . changeset ( % ContractForm { } , % { } ) ) )
@@ -143,18 +143,6 @@ defmodule AlgoraWeb.Org.DashboardLive do
143143
144144 @ impl true
145145 def handle_params ( params , _uri , socket ) do
146- % { current_org: current_org , previewed_user: previewed_user } = socket . assigns
147-
148- stats = Bounties . fetch_stats ( org_id: current_org . id , current_user: socket . assigns [ :current_user ] )
149-
150- bounties =
151- Bounties . list_bounties (
152- owner_id: previewed_user . id ,
153- limit: page_size ( ) ,
154- status: :open ,
155- current_user: socket . assigns [ :current_user ]
156- )
157-
158146 socket =
159147 if params [ "action" ] == "create_contract" do
160148 assign ( socket , :main_contract_form_open? , true )
@@ -173,9 +161,9 @@ defmodule AlgoraWeb.Org.DashboardLive do
173161
174162 { :noreply ,
175163 socket
176- |> assign ( :bounty_rows , to_bounty_rows ( bounties ) )
177- |> assign ( :has_more_bounties , length ( bounties ) >= page_size ( ) )
178- |> assign ( :stats , stats ) }
164+ |> LocalStore . init ( key: __MODULE__ )
165+ |> LocalStore . subscribe ( )
166+ |> assign_bounties ( ) }
179167 end
180168
181169 @ impl true
@@ -206,7 +194,7 @@ defmodule AlgoraWeb.Org.DashboardLive do
206194 @ impl true
207195 def render ( assigns ) do
208196 ~H"""
209- < div class = "lg:pr-96 " >
197+ < div class = "lg:pr-96 " id = " org-dashboard " phx-hook = " LocalStateStore " >
210198 < div class = "container mx-auto max-w-7xl space-y-8 xl:space-y-16 p-4 sm:p-6 lg:p-8 " >
211199 < . section :if = { @ payable_bounties != % { } } >
212200 < . card >
@@ -530,23 +518,6 @@ defmodule AlgoraWeb.Org.DashboardLive do
530518 """
531519 end
532520
533- @ impl true
534- def handle_info ( { :authenticated , user } , socket ) do
535- socket =
536- socket
537- |> assign ( :current_user , user )
538- |> assign ( :has_fresh_token? , true )
539-
540- case socket . assigns . pending_action do
541- { event , params } ->
542- socket = assign ( socket , :pending_action , nil )
543- handle_event ( event , params , socket )
544-
545- nil ->
546- { :noreply , socket }
547- end
548- end
549-
550521 @ impl true
551522 def handle_info ( % Chat.MessageCreated { message: message } , socket ) do
552523 if message . id in Enum . map ( socket . assigns . messages , & & 1 . id ) do
@@ -556,15 +527,28 @@ defmodule AlgoraWeb.Org.DashboardLive do
556527 end
557528 end
558529
530+ def handle_event ( "restore_settings" , params , socket ) do
531+ socket = LocalStore . restore ( socket , params )
532+
533+ case socket . assigns . pending_action do
534+ nil ->
535+ { :noreply , socket }
536+
537+ { event , params } ->
538+ socket = LocalStore . assign_cached ( socket , :pending_action , nil )
539+ handle_event ( event , params , socket )
540+ end
541+ end
542+
559543 @ impl true
560544 def handle_event ( "install_app" = event , unsigned_params , socket ) do
561545 { :noreply ,
562546 if socket . assigns . has_fresh_token? do
563547 redirect ( socket , external: Github . install_url_select_target ( ) )
564548 else
565549 socket
566- |> assign ( :pending_action , { event , unsigned_params } )
567- |> push_event ( "open_popup" , % { url : socket . assigns . oauth_url } )
550+ |> LocalStore . assign_cached ( :pending_action , { event , unsigned_params } )
551+ |> redirect ( external : socket . assigns . oauth_url )
568552 end }
569553 end
570554
@@ -614,6 +598,7 @@ defmodule AlgoraWeb.Org.DashboardLive do
614598 { :noreply ,
615599 socket
616600 |> assign_achievements ( )
601+ |> assign_bounties ( )
617602 |> put_flash ( :info , "Bounty created" ) }
618603 else
619604 % { valid?: false } ->
@@ -628,8 +613,8 @@ defmodule AlgoraWeb.Org.DashboardLive do
628613 else
629614 { :noreply ,
630615 socket
631- |> assign ( :pending_action , { event , unsigned_params } )
632- |> push_event ( "open_popup" , % { url : socket . assigns . oauth_url } ) }
616+ |> LocalStore . assign_cached ( :pending_action , { event , unsigned_params } )
617+ |> redirect ( external : socket . assigns . oauth_url ) }
633618 end
634619 end
635620
@@ -669,8 +654,8 @@ defmodule AlgoraWeb.Org.DashboardLive do
669654 else
670655 { :noreply ,
671656 socket
672- |> assign ( :pending_action , { event , unsigned_params } )
673- |> push_event ( "open_popup" , % { url : socket . assigns . oauth_url } ) }
657+ |> LocalStore . assign_cached ( :pending_action , { event , unsigned_params } )
658+ |> redirect ( external : socket . assigns . oauth_url ) }
674659 end
675660 end
676661
@@ -934,8 +919,25 @@ defmodule AlgoraWeb.Org.DashboardLive do
934919
935920 defp to_bounty_rows ( bounties ) , do: bounties
936921
922+ defp assign_bounties ( socket ) do
923+ stats = Bounties . fetch_stats ( org_id: socket . assigns . previewed_user . id , current_user: socket . assigns [ :current_user ] )
924+
925+ bounties =
926+ Bounties . list_bounties (
927+ owner_id: socket . assigns . previewed_user . id ,
928+ limit: page_size ( ) ,
929+ status: :open ,
930+ current_user: socket . assigns [ :current_user ]
931+ )
932+
933+ socket
934+ |> assign ( :bounty_rows , to_bounty_rows ( bounties ) )
935+ |> assign ( :has_more_bounties , length ( bounties ) >= page_size ( ) )
936+ |> assign ( :stats , stats )
937+ end
938+
937939 defp assign_more_bounties ( socket ) do
938- % { bounty_rows: rows , current_org: current_org } = socket . assigns
940+ % { bounty_rows: rows } = socket . assigns
939941
940942 last_bounty = List . last ( rows )
941943
@@ -946,7 +948,7 @@ defmodule AlgoraWeb.Org.DashboardLive do
946948
947949 more_bounties =
948950 Bounties . list_bounties (
949- owner_id: current_org . id ,
951+ owner_id: socket . assigns . previewed_user . id ,
950952 limit: page_size ( ) ,
951953 status: :open ,
952954 before: cursor ,
0 commit comments