2023-09-04 14:44:22 +03:00
|
|
|
defmodule PlausibleWeb.Live.GoalSettings do
|
|
|
|
@moduledoc """
|
|
|
|
LiveView allowing listing, creating and deleting goals.
|
|
|
|
"""
|
2024-01-09 14:28:31 +03:00
|
|
|
use PlausibleWeb, :live_view
|
2023-09-04 14:44:22 +03:00
|
|
|
use Phoenix.HTML
|
|
|
|
|
|
|
|
alias Plausible.{Sites, Goals}
|
2024-01-15 13:39:30 +03:00
|
|
|
alias PlausibleWeb.Live.Components.Modal
|
2023-09-04 14:44:22 +03:00
|
|
|
|
|
|
|
def mount(
|
|
|
|
_params,
|
2023-09-13 15:55:29 +03:00
|
|
|
%{"site_id" => site_id, "domain" => domain, "current_user_id" => user_id},
|
2023-09-04 14:44:22 +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)
|
|
|
|
|> assign_new(:all_goals, fn %{site: site} ->
|
|
|
|
Goals.for_site(site, preload_funnels?: true)
|
|
|
|
end)
|
2024-01-15 13:39:30 +03:00
|
|
|
|> assign_new(:current_user, fn ->
|
|
|
|
Plausible.Repo.get(Plausible.Auth.User, user_id)
|
|
|
|
end)
|
2023-09-04 14:44:22 +03:00
|
|
|
|
|
|
|
{:ok,
|
|
|
|
assign(socket,
|
2023-09-13 15:55:29 +03:00
|
|
|
site_id: site_id,
|
|
|
|
domain: domain,
|
|
|
|
displayed_goals: socket.assigns.all_goals,
|
2023-09-04 14:44:22 +03:00
|
|
|
filter_text: ""
|
|
|
|
)}
|
|
|
|
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="goal-settings-main">
|
2023-11-20 13:31:56 +03:00
|
|
|
<.flash_messages flash={@flash} />
|
2024-01-15 13:39:30 +03:00
|
|
|
<.live_component module={Modal} id="goals-form-modal">
|
|
|
|
<.live_component
|
|
|
|
module={PlausibleWeb.Live.GoalSettings.Form}
|
|
|
|
id="goals-form"
|
|
|
|
domain={@domain}
|
|
|
|
site={@site}
|
|
|
|
current_user={@current_user}
|
|
|
|
on_save_goal={
|
|
|
|
fn goal, socket ->
|
|
|
|
send(self(), {:goal_added, goal})
|
|
|
|
Modal.close(socket, "goals-form-modal")
|
|
|
|
end
|
2023-09-04 14:44:22 +03:00
|
|
|
}
|
2024-01-15 13:39:30 +03:00
|
|
|
/>
|
|
|
|
</.live_component>
|
2023-09-04 14:44:22 +03:00
|
|
|
<.live_component
|
|
|
|
module={PlausibleWeb.Live.GoalSettings.List}
|
|
|
|
id="goals-list"
|
|
|
|
goals={@displayed_goals}
|
|
|
|
domain={@domain}
|
|
|
|
filter_text={@filter_text}
|
2024-02-12 12:43:54 +03:00
|
|
|
site={@site}
|
2023-09-04 14:44:22 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("reset-filter-text", _params, socket) do
|
|
|
|
{:noreply, assign(socket, filter_text: "", displayed_goals: socket.assigns.all_goals)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("filter", %{"filter-text" => filter_text}, socket) do
|
|
|
|
new_list =
|
|
|
|
PlausibleWeb.Live.Components.ComboBox.StaticSearch.suggest(
|
|
|
|
filter_text,
|
|
|
|
socket.assigns.all_goals
|
|
|
|
)
|
|
|
|
|
|
|
|
{:noreply, assign(socket, displayed_goals: new_list, filter_text: filter_text)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("delete-goal", %{"goal-id" => goal_id}, socket) do
|
|
|
|
goal_id = String.to_integer(goal_id)
|
|
|
|
|
|
|
|
case Plausible.Goals.delete(goal_id, socket.assigns.site_id) do
|
|
|
|
:ok ->
|
|
|
|
socket =
|
|
|
|
socket
|
2023-11-20 13:31:56 +03:00
|
|
|
|> put_live_flash(:success, "Goal deleted successfully")
|
2023-09-04 14:44:22 +03:00
|
|
|
|> assign(
|
|
|
|
all_goals: Enum.reject(socket.assigns.all_goals, &(&1.id == goal_id)),
|
|
|
|
displayed_goals: Enum.reject(socket.assigns.displayed_goals, &(&1.id == goal_id))
|
|
|
|
)
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_info({:goal_added, goal}, socket) do
|
|
|
|
socket =
|
|
|
|
socket
|
|
|
|
|> assign(
|
|
|
|
filter_text: "",
|
|
|
|
all_goals: [goal | socket.assigns.all_goals],
|
|
|
|
displayed_goals: [goal | socket.assigns.all_goals]
|
|
|
|
)
|
2023-11-20 13:31:56 +03:00
|
|
|
|> put_live_flash(:success, "Goal saved successfully")
|
2023-09-04 14:44:22 +03:00
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
end
|