WIP
parent
0007c0c108
commit
014c391e19
@ -0,0 +1,83 @@
|
||||
<.live_component id="embedded_liveview_flash" module={PlausibleWeb.Live.Flash} flash={@flash} />
|
||||
|
||||
<section>
|
||||
<div>
|
||||
<div class="border-t border-gray-200 pt-4 sm:flex sm:items-center sm:justify-between">
|
||||
<form id="filter-form" phx-change="filter">
|
||||
<div class="text-gray-800 text-sm inline-flex items-center">
|
||||
<div class="relative mt-2 rounded-md shadow-sm flex">
|
||||
<div class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
|
||||
<Heroicons.magnifying_glass class="feather mr-1 dark:text-gray-300" />
|
||||
</div>
|
||||
<input
|
||||
type="text"
|
||||
name="filter-text"
|
||||
id="filter-text"
|
||||
class="pl-8 shadow-sm dark:bg-gray-900 dark:text-gray-300 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:bg-gray-800"
|
||||
placeholder="Search Properties"
|
||||
value={@filter_text}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Heroicons.backspace
|
||||
:if={String.trim(@filter_text) != ""}
|
||||
class="feather ml-2 cursor-pointer hover:text-red-500 dark:text-gray-300 dark:hover:text-red-500 mt-2"
|
||||
phx-click="reset-filter-text"
|
||||
id="reset-filter"
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
<div class="mt-4 flex sm:ml-4 sm:mt-0">
|
||||
<.modal id="add-prop">
|
||||
<:trigger>
|
||||
<.button>+ Add Property</.button>
|
||||
</:trigger>
|
||||
|
||||
<:title>Add property for <%= @site.domain %></:title>
|
||||
<:panel>
|
||||
<.live_component
|
||||
id={:new}
|
||||
module={FormComponent}
|
||||
site={@site}
|
||||
/>
|
||||
</:panel>
|
||||
</.modal>
|
||||
</div>
|
||||
</div>
|
||||
<%= if is_list(@displayed_props) && length(@displayed_props) > 0 do %>
|
||||
<ul id="allowed-props" class="mt-12 divide-gray-200 divide-y dark:divide-gray-600">
|
||||
<li :for={{prop, index} <- Enum.with_index(@displayed_props)} id={"prop-#{index}"} class="flex py-4">
|
||||
<span class="flex-1 truncate font-medium text-sm text-gray-800 dark:text-gray-200">
|
||||
<%= prop %>
|
||||
</span>
|
||||
<button
|
||||
id={"disallow-prop-#{prop}"}
|
||||
data-confirm={delete_confirmation_text(prop)}
|
||||
phx-click="disallow-prop"
|
||||
phx-value-prop={prop}
|
||||
class="w-4 h-4 text-red-600 hover:text-red-700"
|
||||
aria-label={"Remove #{prop} property"}
|
||||
>
|
||||
<Heroicons.trash class="feather feather-sm" />
|
||||
</button>
|
||||
</li>
|
||||
</ul>
|
||||
<% else %>
|
||||
<p class="text-sm text-gray-800 dark:text-gray-200 mt-12 mb-8 text-center">
|
||||
<span :if={String.trim(@filter_text) != ""}>
|
||||
No properties found for this site. Please refine or
|
||||
<a
|
||||
class="text-indigo-500 cursor-pointer underline"
|
||||
phx-click="reset-filter-text"
|
||||
id="reset-filter-hint"
|
||||
>
|
||||
reset your search.
|
||||
</a>
|
||||
</span>
|
||||
<span :if={String.trim(@filter_text) == "" && Enum.empty?(@displayed_props)}>
|
||||
No properties configured for this site.
|
||||
</span>
|
||||
</p>
|
||||
<% end %>
|
||||
</div>
|
||||
</section>
|
||||
@ -1,159 +0,0 @@
|
||||
defmodule PlausibleWeb.Live.PropsSettings.Form do
|
||||
@moduledoc """
|
||||
Live view for the custom props creation form
|
||||
"""
|
||||
use Phoenix.LiveView
|
||||
import PlausibleWeb.Live.Components.Form
|
||||
alias PlausibleWeb.Live.Components.ComboBox
|
||||
|
||||
def mount(
|
||||
_params,
|
||||
%{
|
||||
"site_id" => _site_id,
|
||||
"current_user_id" => user_id,
|
||||
"domain" => domain,
|
||||
"rendered_by" => pid
|
||||
},
|
||||
socket
|
||||
) do
|
||||
site = Plausible.Sites.get_for_user!(user_id, domain, [:owner, :admin, :super_admin])
|
||||
|
||||
form = new_form(site)
|
||||
|
||||
{:ok,
|
||||
assign(socket,
|
||||
form: form,
|
||||
domain: domain,
|
||||
rendered_by: pid,
|
||||
prop_key_options_count: 0,
|
||||
site: site
|
||||
)}
|
||||
end
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div
|
||||
class="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity z-50"
|
||||
phx-window-keydown="cancel-allow-prop"
|
||||
phx-key="Escape"
|
||||
>
|
||||
</div>
|
||||
<div class="fixed inset-0 flex items-center justify-center mt-16 z-50 overflow-y-auto overflow-x-hidden">
|
||||
<div class="w-1/2 h-full">
|
||||
<.form
|
||||
:let={f}
|
||||
for={@form}
|
||||
class="max-w-md w-full mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"
|
||||
phx-submit="allow-prop"
|
||||
phx-click-away="cancel-allow-prop"
|
||||
>
|
||||
<h2 class="text-xl font-black dark:text-gray-100">Add Property for <%= @domain %></h2>
|
||||
|
||||
<div class="py-2">
|
||||
<.label for="prop_input">
|
||||
Property
|
||||
</.label>
|
||||
|
||||
<.live_component
|
||||
id="prop_input"
|
||||
submit_name="prop"
|
||||
class={[
|
||||
"py-2"
|
||||
]}
|
||||
module={ComboBox}
|
||||
suggest_fun={
|
||||
pid = self()
|
||||
|
||||
fn
|
||||
"", [] ->
|
||||
options =
|
||||
@site
|
||||
|> Plausible.Props.suggest_keys_to_allow()
|
||||
|> Enum.map(&{&1, &1})
|
||||
|
||||
send(pid, {:update_prop_key_options_count, Enum.count(options)})
|
||||
|
||||
options
|
||||
|
||||
input, options ->
|
||||
ComboBox.StaticSearch.suggest(input, options)
|
||||
end
|
||||
}
|
||||
creatable
|
||||
/>
|
||||
|
||||
<.error :for={{msg, opts} <- f[:allowed_event_props].errors}>
|
||||
<%= Enum.reduce(opts, msg, fn {key, value}, acc ->
|
||||
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
|
||||
end) %>
|
||||
</.error>
|
||||
</div>
|
||||
|
||||
<div class="py-4">
|
||||
<button type="submit" class="button text-base font-bold w-full">
|
||||
Add Property →
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<button
|
||||
:if={@prop_key_options_count > 0}
|
||||
title="Use this to add any existing properties from your past events into your settings. This allows you to set up properties without having to manually enter each item."
|
||||
class="mt-2 text-sm hover:underline text-indigo-600 dark:text-indigo-400 text-left"
|
||||
phx-click="allow-existing-props"
|
||||
>
|
||||
Already sending custom properties? Click to add <%= @prop_key_options_count %> existing properties we found.
|
||||
</button>
|
||||
</.form>
|
||||
</div>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def handle_info({:update_prop_key_options_count, count}, socket) do
|
||||
{:noreply, assign(socket, prop_key_options_count: count)}
|
||||
end
|
||||
|
||||
def handle_info({:selection_made, %{submit_value: _prop}}, socket) do
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
def handle_event("allow-prop", %{"prop" => prop}, socket) do
|
||||
case Plausible.Props.allow(socket.assigns.site, prop) do
|
||||
{:ok, site} ->
|
||||
send(socket.assigns.rendered_by, {:prop_allowed, prop})
|
||||
|
||||
{:noreply,
|
||||
assign(socket,
|
||||
site: site,
|
||||
form: new_form(site)
|
||||
)}
|
||||
|
||||
{:error, changeset} ->
|
||||
{:noreply,
|
||||
assign(socket,
|
||||
form: to_form(Map.put(changeset, :action, :validate))
|
||||
)}
|
||||
end
|
||||
end
|
||||
|
||||
def handle_event("allow-existing-props", _params, socket) do
|
||||
{:ok, site} = Plausible.Props.allow_existing_props(socket.assigns.site)
|
||||
send(socket.assigns.rendered_by, {:props_allowed, site.allowed_event_props})
|
||||
|
||||
{:noreply,
|
||||
assign(socket,
|
||||
site: site,
|
||||
form: new_form(site),
|
||||
prop_key_options_count: 0
|
||||
)}
|
||||
end
|
||||
|
||||
def handle_event("cancel-allow-prop", _value, socket) do
|
||||
send(socket.assigns.rendered_by, :cancel_add_prop)
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
||||
defp new_form(site) do
|
||||
to_form(Plausible.Props.allow_changeset(site, []))
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,109 @@
|
||||
defmodule PlausibleWeb.Live.PropsSettings.FormComponent do
|
||||
@moduledoc """
|
||||
Live component for the custom props creation form
|
||||
"""
|
||||
use PlausibleWeb, :live_component
|
||||
import PlausibleWeb.Live.Components.Form
|
||||
alias PlausibleWeb.Live.Components.ComboBox
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div>
|
||||
<.form :let={f} for={@form} phx-target={@myself} phx-submit="allow-prop">
|
||||
<div class="py-2">
|
||||
<.label for="prop_input">
|
||||
Property
|
||||
</.label>
|
||||
|
||||
<.live_component
|
||||
id="prop_input"
|
||||
submit_name="prop"
|
||||
class="mt-2"
|
||||
module={ComboBox}
|
||||
on_select={&send_update(@myself, selection: &1)}
|
||||
suggest_fun={
|
||||
fn
|
||||
"", [] ->
|
||||
options =
|
||||
@site
|
||||
|> Plausible.Props.suggest_keys_to_allow()
|
||||
|> Enum.map(&{&1, &1})
|
||||
|
||||
# Use of @myself here required LiveView upgrade https://github.com/phoenixframework/phoenix_live_view/blob/v0.20.1/CHANGELOG.md#enhancements-1
|
||||
send_update(@myself, prop_key_options_count: Enum.count(options))
|
||||
|
||||
options
|
||||
|
||||
input, options ->
|
||||
ComboBox.StaticSearch.suggest(input, options)
|
||||
end
|
||||
}
|
||||
creatable
|
||||
/>
|
||||
|
||||
<.error :for={{msg, opts} <- f[:allowed_event_props].errors}>
|
||||
<%= Enum.reduce(opts, msg, fn {key, value}, acc ->
|
||||
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
|
||||
end) %>
|
||||
</.error>
|
||||
</div>
|
||||
|
||||
<div class="mt-5 sm:mt-6">
|
||||
<.button
|
||||
phx-disable-with="Saving..."
|
||||
type="submit"
|
||||
class="group w-full inline-flex justify-center custom-class phx-submit-loading:animate-pulse"
|
||||
>
|
||||
<Heroicons.arrow_right_circle class="group-[.custom-class]:animate-spin h-4 w-4" />
|
||||
Add Property →
|
||||
</.button>
|
||||
</div>
|
||||
|
||||
<button
|
||||
:if={@prop_key_options_count > 0}
|
||||
title="Use this to add any existing properties from your past events into your settings. This allows you to set up properties without having to manually enter each item."
|
||||
class="mt-2 text-sm hover:underline text-indigo-600 dark:text-indigo-400 text-left"
|
||||
phx-click="allow-existing-props"
|
||||
>
|
||||
Already sending custom properties? Click to add <%= @prop_key_options_count %> existing properties we found.
|
||||
</button>
|
||||
</.form>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def update(%{site: site}, socket) do
|
||||
changeset = Plausible.Props.allow_changeset(site, [])
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(site: site, form: to_form(changeset))
|
||||
|> assign_new(:prop_key_options_count, fn -> 0 end)}
|
||||
end
|
||||
|
||||
def update(%{prop_key_options_count: _count} = assigns, socket) do
|
||||
{:ok, socket |> assign(assigns)}
|
||||
end
|
||||
|
||||
def update(%{selection: _selection}, socket) do
|
||||
# noop
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("allow-prop", %{"prop" => prop}, socket) do
|
||||
case Plausible.Props.allow(socket.assigns.site, prop) do
|
||||
{:ok, _site} ->
|
||||
send(self(), {__MODULE__, {:prop_allowed, prop}})
|
||||
{:noreply, socket}
|
||||
|
||||
{:error, changeset} ->
|
||||
{:noreply,
|
||||
assign(socket,
|
||||
form: to_form(Map.put(changeset, :action, :validate))
|
||||
)}
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,100 +0,0 @@
|
||||
defmodule PlausibleWeb.Live.PropsSettings.List do
|
||||
@moduledoc """
|
||||
Phoenix LiveComponent module that renders a list of custom properties
|
||||
"""
|
||||
use Phoenix.LiveComponent
|
||||
use Phoenix.HTML
|
||||
|
||||
use Plausible.Funnel
|
||||
|
||||
attr(:props, :list, required: true)
|
||||
attr(:domain, :string, required: true)
|
||||
attr(:filter_text, :string)
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div>
|
||||
<div class="border-t border-gray-200 pt-4 sm:flex sm:items-center sm:justify-between">
|
||||
<form id="filter-form" phx-change="filter">
|
||||
<div class="text-gray-800 text-sm inline-flex items-center">
|
||||
<div class="relative mt-2 rounded-md shadow-sm flex">
|
||||
<div class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
|
||||
<Heroicons.magnifying_glass class="feather mr-1 dark:text-gray-300" />
|
||||
</div>
|
||||
<input
|
||||
type="text"
|
||||
name="filter-text"
|
||||
id="filter-text"
|
||||
class="pl-8 shadow-sm dark:bg-gray-900 dark:text-gray-300 focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 dark:border-gray-500 rounded-md dark:bg-gray-800"
|
||||
placeholder="Search Properties"
|
||||
value={@filter_text}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<Heroicons.backspace
|
||||
:if={String.trim(@filter_text) != ""}
|
||||
class="feather ml-2 cursor-pointer hover:text-red-500 dark:text-gray-300 dark:hover:text-red-500 mt-2"
|
||||
phx-click="reset-filter-text"
|
||||
id="reset-filter"
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
<div class="mt-4 flex sm:ml-4 sm:mt-0">
|
||||
<button
|
||||
type="button"
|
||||
phx-click="add-prop"
|
||||
class="mt-2 block items-center rounded-md bg-indigo-600 p-2 text-sm text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
|
||||
>
|
||||
+ Add Property
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<%= if is_list(@props) && length(@props) > 0 do %>
|
||||
<ul id="allowed-props" class="mt-12 divide-gray-200 divide-y dark:divide-gray-600">
|
||||
<li :for={{prop, index} <- Enum.with_index(@props)} id={"prop-#{index}"} class="flex py-4">
|
||||
<span class="flex-1 truncate font-medium text-sm text-gray-800 dark:text-gray-200">
|
||||
<%= prop %>
|
||||
</span>
|
||||
<button
|
||||
id={"disallow-prop-#{prop}"}
|
||||
data-confirm={delete_confirmation_text(prop)}
|
||||
phx-click="disallow-prop"
|
||||
phx-value-prop={prop}
|
||||
class="w-4 h-4 text-red-600 hover:text-red-700"
|
||||
aria-label={"Remove #{prop} property"}
|
||||
>
|
||||
<Heroicons.trash class="feather feather-sm" />
|
||||
</button>
|
||||
</li>
|
||||
</ul>
|
||||
<% else %>
|
||||
<p class="text-sm text-gray-800 dark:text-gray-200 mt-12 mb-8 text-center">
|
||||
<span :if={String.trim(@filter_text) != ""}>
|
||||
No properties found for this site. Please refine or
|
||||
<a
|
||||
class="text-indigo-500 cursor-pointer underline"
|
||||
phx-click="reset-filter-text"
|
||||
id="reset-filter-hint"
|
||||
>
|
||||
reset your search.
|
||||
</a>
|
||||
</span>
|
||||
<span :if={String.trim(@filter_text) == "" && Enum.empty?(@props)}>
|
||||
No properties configured for this site.
|
||||
</span>
|
||||
</p>
|
||||
<% end %>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp delete_confirmation_text(prop) do
|
||||
"""
|
||||
Are you sure you want to remove the following property:
|
||||
|
||||
#{prop}
|
||||
|
||||
This will just affect the UI, all of your analytics data will stay intact.
|
||||
"""
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue