@@ -19,11 +19,15 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
1919 |> Plausible.Goal . changeset ( )
2020 |> to_form ( )
2121
22- selected_tab =
23- case assigns . goal do
24- % { page_path: p , scroll_threshold: s } when not is_nil ( p ) and s > - 1 -> "scroll"
25- % { page_path: p } when not is_nil ( p ) -> "pageviews"
26- _goal_or_nil -> "custom_events"
22+ form_type =
23+ if assigns . goal do
24+ case assigns . goal do
25+ % { page_path: p , scroll_threshold: s } when not is_nil ( p ) and s > - 1 -> "scroll"
26+ % { page_path: p } when not is_nil ( p ) -> "pageviews"
27+ _ -> "custom_events"
28+ end
29+ else
30+ assigns [ :goal_type ] || "custom_events"
2731 end
2832
2933 socket =
@@ -37,14 +41,14 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
3741 current_user: assigns . current_user ,
3842 site_team: assigns . site_team ,
3943 domain: assigns . domain ,
40- selected_tab: selected_tab ,
41- tab_sequence_id: 0 ,
44+ form_type: form_type ,
4245 site: site ,
4346 has_access_to_revenue_goals?: has_access_to_revenue_goals? ,
4447 existing_goals: assigns . existing_goals ,
4548 on_save_goal: assigns . on_save_goal ,
4649 on_autoconfigure: assigns . on_autoconfigure ,
47- goal: assigns . goal
50+ goal: assigns . goal ,
51+ goal_type: assigns [ :goal_type ]
4852 )
4953
5054 { :ok , socket }
@@ -68,7 +72,7 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
6872 < . title > Edit goal for { @ domain } </ . title >
6973
7074 < . custom_event_fields
71- :if = { @ selected_tab == "custom_events" }
75+ :if = { @ form_type == "custom_events" }
7276 f = { f }
7377 suffix = { @ context_unique_id }
7478 current_user = { @ current_user }
@@ -80,14 +84,14 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
8084 has_access_to_revenue_goals? = { @ has_access_to_revenue_goals? }
8185 />
8286 < . pageview_fields
83- :if = { @ selected_tab == "pageviews" }
87+ :if = { @ form_type == "pageviews" }
8488 f = { f }
8589 goal = { @ goal }
8690 suffix = { @ context_unique_id }
8791 site = { @ site }
8892 />
8993 < . scroll_fields
90- :if = { @ selected_tab == "scroll" }
94+ :if = { @ form_type == "scroll" }
9195 f = { f }
9296 goal = { @ goal }
9397 suffix = { @ context_unique_id }
@@ -103,58 +107,41 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
103107
104108 def create_form ( assigns ) do
105109 ~H"""
106- < . form
107- :let = { f }
108- x-data = "{ tabSelectionInProgress: false } "
109- for = { @ form }
110- phx-submit = "save-goal "
111- phx-target = { @ myself }
112- >
110+ < . form :let = { f } for = { @ form } phx-submit = "save-goal " phx-target = { @ myself } >
113111 < . title >
114112 Add goal for { Plausible.Sites . display_name ( @ site ) }
115113 </ . title >
116114
117- < . tabs current_user = { @ current_user } site = { @ site } selected_tab = { @ selected_tab } myself = { @ myself } />
118-
119115 < . custom_event_fields
120- :if = { @ selected_tab == "custom_events" }
121- x-show = "!tabSelectionInProgress "
116+ :if = { @ form_type == "custom_events" }
122117 f = { f }
123- suffix = { suffix ( @ context_unique_id , @ tab_sequence_id ) }
118+ suffix = { @ context_unique_id }
124119 current_user = { @ current_user }
125120 site_team = { @ site_team }
126121 site = { @ site }
127122 existing_goals = { @ existing_goals }
128123 goal_options = { @ event_name_options }
129124 has_access_to_revenue_goals? = { @ has_access_to_revenue_goals? }
130- x-init = "tabSelectionInProgress = false "
131125 />
132126 < . pageview_fields
133- :if = { @ selected_tab == "pageviews" }
134- x-show = "!tabSelectionInProgress "
127+ :if = { @ form_type == "pageviews" }
135128 f = { f }
136- suffix = { suffix ( @ context_unique_id , @ tab_sequence_id ) }
129+ suffix = { @ context_unique_id }
137130 site = { @ site }
138- x-init = "tabSelectionInProgress = false "
139131 />
140132 < . scroll_fields
141- :if = { @ selected_tab == "scroll" }
142- x-show = "!tabSelectionInProgress "
133+ :if = { @ form_type == "scroll" }
143134 f = { f }
144- suffix = { suffix ( @ context_unique_id , @ tab_sequence_id ) }
135+ suffix = { @ context_unique_id }
145136 site = { @ site }
146- x-init = "tabSelectionInProgress = false "
147137 />
148138
149- < div x-show = "!tabSelectionInProgress " >
150- < . button type = "submit " class = "w-full " >
151- Add goal
152- </ . button >
153- </ div >
139+ < . button type = "submit " class = "w-full " >
140+ Add goal
141+ </ . button >
154142
155143 < button
156- :if = { @ selected_tab == "custom_events" && @ event_name_options_count > 0 }
157- x-show = "!tabSelectionInProgress "
144+ :if = { @ form_type == "custom_events" && @ event_name_options_count > 0 }
158145 class = "mt-4 text-sm hover:underline text-indigo-600 dark:text-indigo-400 text-left "
159146 phx-click = "autoconfigure "
160147 phx-target = { @ myself }
@@ -441,73 +428,6 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
441428 """
442429 end
443430
444- def tabs ( assigns ) do
445- ~H"""
446- < div class = "text-sm mt-6 font-medium dark:text-gray-100 " > Goal trigger</ div >
447- < div class = "my-2 p-1 text-sm w-full flex gap-1 overflow-hidden rounded-lg border border-gray-300 dark:border-gray-700 " >
448- < . tab
449- id = "event-tab "
450- tab_value = "custom_events "
451- selected? = { @ selected_tab == "custom_events" }
452- myself = { @ myself }
453- >
454- Custom event
455- </ . tab >
456- < . tab
457- id = "pageview-tab "
458- tab_value = "pageviews "
459- selected? = { @ selected_tab == "pageviews" }
460- myself = { @ myself }
461- >
462- Pageview
463- </ . tab >
464- < . tab
465- id = "scroll-tab "
466- tab_value = "scroll "
467- selected? = { @ selected_tab == "scroll" }
468- myself = { @ myself }
469- >
470- Scroll depth
471- </ . tab >
472- </ div >
473- """
474- end
475-
476- attr ( :id , :string , required: true )
477- attr ( :tab_value , :string , required: true )
478- attr ( :selected? , :boolean , required: true )
479- attr ( :myself , :any , required: true )
480- slot ( :inner_block , required: true )
481-
482- defp tab ( assigns ) do
483- ~H"""
484- < a
485- class = { [
486- "flex-1 text-center py-2 px-3 rounded-md font-medium hover:bg-gray-100 dark:hover:bg-gray-750 transition-colors duration-150" ,
487- "cursor-pointer" ,
488- @ selected? && "bg-gray-150 dark:bg-gray-700 text-gray-800 dark:text-white" ,
489- ! @ selected? && "dark:text-gray-200 text-gray-600 hover:text-gray-800 dark:hover:text-white"
490- ] }
491- id = { @ id }
492- x-on:click = { ! @ selected? && "tabSelectionInProgress = true" }
493- phx-click = "switch-tab "
494- phx-value-tab = { @ tab_value }
495- phx-target = { @ myself }
496- >
497- { render_slot ( @ inner_block ) }
498- </ a >
499- """
500- end
501-
502- def handle_event ( "switch-tab" , % { "tab" => tab } , socket ) do
503- socket =
504- socket
505- |> assign ( :selected_tab , tab )
506- |> update ( :tab_sequence_id , & ( & 1 + 1 ) )
507-
508- { :noreply , socket }
509- end
510-
511431 def handle_event ( "save-goal" , % { "goal" => goal_params } , % { assigns: % { goal: nil } } = socket ) do
512432 case Plausible.Goals . create ( socket . assigns . site , goal_params ) do
513433 { :ok , goal } ->
@@ -572,10 +492,6 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
572492 |> Enum . map ( fn name -> { name , name } end )
573493 end
574494
575- defp suffix ( context_unique_id , tab_sequence_id ) do
576- "#{ context_unique_id } -tabseq#{ tab_sequence_id } "
577- end
578-
579495 on_ee do
580496 defp currency_option ( nil ) , do: nil
581497
0 commit comments