2023-06-22 10:00:07 +03:00
|
|
|
defmodule PlausibleWeb.Live.FunnelSettings do
|
|
|
|
@moduledoc """
|
|
|
|
LiveView allowing listing, creating and deleting funnels.
|
|
|
|
"""
|
|
|
|
use Phoenix.LiveView
|
|
|
|
use Phoenix.HTML
|
|
|
|
|
|
|
|
use Plausible.Funnel
|
|
|
|
|
|
|
|
alias Plausible.{Sites, Goals, Funnels}
|
|
|
|
|
|
|
|
def mount(
|
|
|
|
_params,
|
2023-09-13 15:55:29 +03:00
|
|
|
%{"site_id" => site_id, "domain" => domain, "current_user_id" => user_id},
|
2023-06-22 10:00:07 +03:00
|
|
|
socket
|
|
|
|
) do
|
2023-09-13 15:55:29 +03:00
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign_new(:site, fn ->
|
|
|
|
Sites.get_for_user!(user_id, domain, [:owner, :admin, :super_admin])
|
|
|
|
end)
|
2023-09-13 16:07:04 +03:00
|
|
|
|> assign_new(:all_funnels, fn %{site: %{id: ^site_id} = site} ->
|
2023-09-13 15:55:29 +03:00
|
|
|
Funnels.list(site)
|
|
|
|
end)
|
|
|
|
|> assign_new(:goal_count, fn %{site: site} ->
|
|
|
|
Goals.count(site)
|
|
|
|
end)
|
2023-06-22 10:00:07 +03:00
|
|
|
|
|
|
|
{:ok,
|
|
|
|
assign(socket,
|
2023-09-13 15:55:29 +03:00
|
|
|
domain: domain,
|
2023-09-13 16:07:04 +03:00
|
|
|
displayed_funnels: socket.assigns.all_funnels,
|
2023-06-22 10:00:07 +03:00
|
|
|
add_funnel?: false,
|
2023-09-13 16:07:04 +03:00
|
|
|
filter_text: "",
|
2023-06-22 10:00:07 +03:00
|
|
|
current_user_id: user_id
|
|
|
|
)}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Flash sharing with live views within dead views can be done via re-rendering the flash partial.
|
|
|
|
# Normally, we'd have to use live_patch which we can't do with views unmounted at the router it seems.
|
|
|
|
def render(assigns) do
|
|
|
|
~H"""
|
|
|
|
<div id="funnel-settings-main">
|
|
|
|
<.live_component id="embedded_liveview_flash" module={PlausibleWeb.Live.Flash} flash={@flash} />
|
|
|
|
<%= if @add_funnel? do %>
|
|
|
|
<%= live_render(
|
|
|
|
@socket,
|
|
|
|
PlausibleWeb.Live.FunnelSettings.Form,
|
|
|
|
id: "funnels-form",
|
|
|
|
session: %{
|
|
|
|
"current_user_id" => @current_user_id,
|
|
|
|
"domain" => @domain
|
|
|
|
}
|
|
|
|
) %>
|
|
|
|
<% else %>
|
|
|
|
<div :if={@goal_count >= Funnel.min_steps()}>
|
|
|
|
<.live_component
|
|
|
|
module={PlausibleWeb.Live.FunnelSettings.List}
|
|
|
|
id="funnels-list"
|
2023-09-13 16:07:04 +03:00
|
|
|
funnels={@displayed_funnels}
|
|
|
|
filter_text={@filter_text}
|
2023-06-22 10:00:07 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div :if={@goal_count < Funnel.min_steps()}>
|
2023-06-28 15:11:48 +03:00
|
|
|
<PlausibleWeb.Components.Generic.notice class="mt-4" title="Not enough goals">
|
2023-06-22 10:00:07 +03:00
|
|
|
You need to define at least two goals to create a funnel. Go ahead and <%= link(
|
|
|
|
"add goals",
|
2023-09-04 14:44:22 +03:00
|
|
|
to: PlausibleWeb.Router.Helpers.site_path(@socket, :settings_goals, @domain),
|
2023-06-22 10:00:07 +03:00
|
|
|
class: "text-indigo-500 w-full text-center"
|
|
|
|
) %> to proceed.
|
|
|
|
</PlausibleWeb.Components.Generic.notice>
|
|
|
|
</div>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2023-09-13 16:07:04 +03:00
|
|
|
def handle_event("reset-filter-text", _params, socket) do
|
|
|
|
{:noreply, assign(socket, filter_text: "", displayed_funnels: socket.assigns.all_funnels)}
|
2023-06-22 10:00:07 +03:00
|
|
|
end
|
|
|
|
|
2023-09-13 16:07:04 +03:00
|
|
|
def handle_event("filter", %{"filter-text" => filter_text}, socket) do
|
|
|
|
new_list =
|
|
|
|
PlausibleWeb.Live.Components.ComboBox.StaticSearch.suggest(
|
|
|
|
filter_text,
|
|
|
|
socket.assigns.all_funnels,
|
|
|
|
to_string: & &1.name
|
|
|
|
)
|
|
|
|
|
|
|
|
{:noreply, assign(socket, displayed_funnels: new_list, filter_text: filter_text)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("add-funnel", _value, socket) do
|
|
|
|
{:noreply, assign(socket, add_funnel?: true)}
|
2023-06-22 10:00:07 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("delete-funnel", %{"funnel-id" => id}, socket) do
|
2023-06-28 14:20:19 +03:00
|
|
|
site =
|
|
|
|
Sites.get_for_user!(socket.assigns.current_user_id, socket.assigns.domain, [:owner, :admin])
|
|
|
|
|
2023-06-22 10:00:07 +03:00
|
|
|
id = String.to_integer(id)
|
2023-06-28 14:20:19 +03:00
|
|
|
:ok = Funnels.delete(site, id)
|
2023-06-22 10:00:07 +03:00
|
|
|
socket = put_flash(socket, :success, "Funnel deleted successfully")
|
|
|
|
Process.send_after(self(), :clear_flash, 5000)
|
2023-09-13 16:07:04 +03:00
|
|
|
|
|
|
|
{:noreply,
|
|
|
|
assign(socket,
|
|
|
|
all_funnels: Enum.reject(socket.assigns.all_funnels, &(&1.id == id)),
|
|
|
|
displayed_funnels: Enum.reject(socket.assigns.displayed_funnels, &(&1.id == id))
|
|
|
|
)}
|
2023-06-22 10:00:07 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_info({:funnel_saved, funnel}, socket) do
|
|
|
|
socket = put_flash(socket, :success, "Funnel saved successfully")
|
|
|
|
Process.send_after(self(), :clear_flash, 5000)
|
2023-09-13 16:07:04 +03:00
|
|
|
|
|
|
|
{:noreply,
|
|
|
|
assign(socket,
|
|
|
|
add_funnel?: false,
|
|
|
|
all_funnels: [funnel | socket.assigns.all_funnels],
|
|
|
|
displayed_funnels: [funnel | socket.assigns.displayed_funnels]
|
|
|
|
)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_info(:cancel_add_funnel, socket) do
|
|
|
|
{:noreply, assign(socket, add_funnel?: false)}
|
2023-06-22 10:00:07 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_info(:clear_flash, socket) do
|
|
|
|
{:noreply, clear_flash(socket)}
|
|
|
|
end
|
|
|
|
end
|