Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

4.9 KB
Raw Blame
defmodule PlausibleWeb.Live.FunnelSettings do
  @moduledoc """
  LiveView allowing listing, creating and deleting funnels.
  """
  use PlausibleWeb, :live_view

  use Plausible.Funnel

  alias Plausible.Funnels
  alias Plausible.Teams

  def mount(
        _params,
        %{"site_id" => site_id, "domain" => domain},
        socket
      ) do
    socket =
      socket
      |> assign_new(:site, fn %{current_user: current_user} ->
        Plausible.Sites.get_for_user!(current_user, domain,
          roles: [
            :owner,
            :admin,
            :editor,
            :super_admin
          ]
        )
      end)
      |> assign_new(:all_funnels, fn %{site: %{id: ^site_id} = site} ->
        Funnels.list(site)
      end)

    {:ok,
     assign(socket,
       site_team: Teams.with_subscription(socket.assigns.site.team),
       domain: domain,
       displayed_funnels: socket.assigns.all_funnels,
       setup_funnel?: false,
       filter_text: "",
       funnel_id: nil
     )}
  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" class="group">
      <.flash_messages flash={@flash} />

      <.tile
        docs="funnel-analysis"
        feature_mod={Plausible.Billing.Feature.Funnels}
        feature_toggle?={true}
        show_content?={!Plausible.Billing.Feature.Funnels.opted_out?(@site)}
        site={@site}
        current_user={@current_user}
        current_team={@site_team}
      >
        <:title>
          Funnels
        </:title>
        <:subtitle :if={Enum.count(@all_funnels) > 0}>
          Compose goals into funnels to track user flows and conversion rates.
        </:subtitle>
        <div
          :if={@setup_funnel?}
          class="fixed flex inset-0 bg-gray-500/75 transition-opacity z-50"
          phx-window-keydown="cancel-setup-funnel"
          phx-key="Escape"
        >
          <div class="w-full self-center block group-has-[#funnel-form]:hidden">
            <div class="text-center">
              <.spinner class="inline-block h-8 w-8" />
            </div>
          </div>
        </div>
        <%= if @setup_funnel? do %>
          {live_render(
            @socket,
            PlausibleWeb.Live.FunnelSettings.Form,
            id: "funnels-form",
            session: %{
              "domain" => @domain,
              "funnel_id" => @funnel_id
            }
          )}
        <% end %>
        <div>
          <.live_component
            module={PlausibleWeb.Live.FunnelSettings.List}
            id="funnels-list"
            funnels={@displayed_funnels}
            filter_text={@filter_text}
          />
        </div>
      </.tile>
    </div>
    """
  end

  def handle_event("reset-filter-text", _params, socket) do
    {:noreply, assign(socket, filter_text: "", displayed_funnels: socket.assigns.all_funnels)}
  end

  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, setup_funnel?: true)}
  end

  def handle_event("cancel-setup-funnel", _value, socket) do
    send(self(), :cancel_setup_funnel)
    {:noreply, socket}
  end

  def handle_event("edit-funnel", %{"funnel-id" => id}, socket) do
    {:noreply, assign(socket, setup_funnel?: true, funnel_id: String.to_integer(id))}
  end

  def handle_event("delete-funnel", %{"funnel-id" => id}, socket) do
    site =
      Plausible.Sites.get_for_user!(
        socket.assigns.current_user,
        socket.assigns.domain,
        roles: [:owner, :admin, :editor]
      )

    id = String.to_integer(id)
    :ok = Funnels.delete(site, id)
    socket = put_live_flash(socket, :success, "Funnel deleted successfully")

    {: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))
     )}
  end

  def handle_info({:funnel_saved, _funnel}, socket) do
    socket = put_live_flash(socket, :success, "Funnel saved successfully")

    funnels = Funnels.list(socket.assigns.site)

    {:noreply,
     assign(socket,
       setup_funnel?: false,
       all_funnels: funnels,
       funnel_id: nil,
       displayed_funnels: funnels
     )}
  end

  def handle_info(:cancel_setup_funnel, socket) do
    {:noreply, assign(socket, setup_funnel?: false, funnel_id: nil)}
  end

  def handle_info({:feature_toggled, flash_msg, updated_site}, socket) do
    {:noreply, assign(put_flash(socket, :success, flash_msg), site: updated_site)}
  end
end