pull/2859/head
Adam Rutkowski 4 years ago
parent 62a2d25f46
commit 707a932dd5

@ -9,7 +9,7 @@ import { Socket } from "phoenix"
import { LiveSocket } from "phoenix_live_view"
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, { params: { _csrf_token: csrfToken } })
let liveSocket = new LiveSocket("/live", Socket, { params: { _csrf_token: csrfToken }, hooks: {} })
// Connect if there are any LiveViews on the page
liveSocket.connect()
@ -25,6 +25,7 @@ window.liveSocket = liveSocket
window.addEventListener(`phx:update-value`, (e) => {
let el = document.getElementById(e.detail.id)
el.value = e.detail.value
el.dispatchEvent(new Event("input", { bubbles: true }))
})
window.addEventListener(`phx:hide`, (e) => {

@ -2,6 +2,21 @@ defmodule Plausible.Funnel do
use Ecto.Schema
import Ecto.Changeset
@min_steps 2
@max_steps 5
defmacro min_steps() do
quote do
unquote(@min_steps)
end
end
defmacro max_steps() do
quote do
unquote(@max_steps)
end
end
defmodule Step do
use Ecto.Schema
import Ecto.Changeset
@ -53,7 +68,7 @@ defmodule Plausible.Funnel do
|> cast(attrs, [:name])
|> validate_required([:name])
|> cast_assoc(:steps, with: &Step.changeset/2, required: true)
|> validate_length(:steps, min: 2, max: 5)
|> validate_length(:steps, min: @min_steps, max: @max_steps)
|> unique_constraint(:name,
name: :funnels_name_site_id_index
)

@ -1,7 +1,7 @@
defmodule Plausible.Funnels do
@funnel_window_duration 86_400
@min_funnel_size 2
@max_funnel_size 5
require Plausible.Funnel
alias Plausible.Funnel
alias Plausible.Repo
@ -11,8 +11,8 @@ defmodule Plausible.Funnels do
import Ecto.Query
def create(site, name, steps)
when is_list(steps) and length(steps) >= @min_funnel_size and
length(steps) <= @max_funnel_size do
when is_list(steps) and length(steps) >= Funnel.min_steps() and
length(steps) <= Funnel.max_steps() do
site
|> create_changeset(name, steps)
|> Repo.insert()

@ -2,8 +2,11 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
use Phoenix.LiveComponent
use Phoenix.HTML
require Plausible.Funnel
alias Plausible.Funnel
def mount(socket) do
{:ok, assign(socket, step_count: 2)}
{:ok, assign(socket, step_ids: Enum.to_list(1..Funnel.min_steps()))}
end
def update(assigns, socket) do
@ -28,41 +31,43 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
onkeydown="return event.key != 'Enter';"
>
<%= label(f, "Funnel name",
class: "block text-sm font-medium text-gray-700 dark:text-gray-300"
class: "block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"
) %>
<.input field={f[:name]} />
<div :if={String.trim(f[:name].value) != ""} id="steps-builder">
<%= label(f, "Funnel Steps",
class: "mt-6 block text-sm font-medium text-gray-700 dark:text-gray-300"
class: "mt-6 block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2"
) %>
<.live_component
:for={step_number <- 1..@step_count}
submit_name="funnel[steps][][goal_id]"
module={PlausibleWeb.Live.FunnelSettings.InputPicker}
id={"step-#{step_number}"}
options={Enum.map(@goals, fn goal -> {goal.id, Plausible.Goal.display_name(goal)} end)}
/>
<a
:if={@step_count < 5}
class="underline text-indigo-600 text-sm cursor-pointer mt-6"
phx-click="add-step"
phx-target={@myself}
>
+ Add another step
</a>
<div :for={step_idx <- @step_ids} class="flex flex-items-cente">
<div class="inline-flex items-center">
<.live_component
submit_name="funnel[steps][][goal_id]"
module={PlausibleWeb.Live.FunnelSettings.InputPicker}
id={"step-#{step_idx}"}
options={
Enum.map(@goals, fn goal -> {goal.id, Plausible.Goal.display_name(goal)} end)
}
/>
</div>
<.remove_step_button
:if={length(@step_ids) > Funnel.min_steps()}
target={@myself}
step_idx={step_idx}
/>
</div>
<.add_step_button :if={length(@step_ids) < Funnel.max_steps()} target={@myself} />
<div class="mt-6">
<button type="submit" class="button mt-6">Save</button>
<button
type="button"
class="inline-block mt-4 ml-2 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-red-700 bg-white dark:bg-gray-800 hover:text-red-500 dark:hover:text-red-400 focus:outline-none focus:border-blue-300 focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150 "
phx-click="cancel_add_funnel"
>
Cancel
</button>
<%= if has_steps_errors?(f) do %>
<.submit_button_inactive />
<% else %>
<.submit_button />
<% end %>
<.cancel_button />
</div>
</div>
</.form>
@ -100,20 +105,100 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
"""
end
attr :step_idx, :integer, required: true
attr :target, :any
def remove_step_button(assigns) do
~H"""
<div class="inline-flex items-center ml-2 mb-2 text-red-600">
<svg
class="feather feather-sm cursor-pointer"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
phx-click="remove-step"
phx-target={@target}
phx-value-step-idx={@step_idx}
>
<polyline points="3 6 5 6 21 6"></polyline>
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2">
</path>
<line x1="10" y1="11" x2="10" y2="17"></line>
<line x1="14" y1="11" x2="14" y2="17"></line>
</svg>
</div>
"""
end
attr :target, :any
def add_step_button(assigns) do
~H"""
<a
class="underline text-indigo-600 text-sm cursor-pointer mt-6"
phx-click="add-step"
phx-target={@target}
>
+ Add another step
</a>
"""
end
def submit_button(assigns) do
~H"""
<button type="submit" class="button mt-6">Save</button>
"""
end
def submit_button_inactive(assigns) do
~H"""
<button
type="none"
class="inline-block mt-4 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-gray-300 bg-white dark:bg-gray-800 hover:text-gray-500 dark:hover:text-gray-400 focus:outline-none focus:border-blue-300 focus:ring active:text-gray-800 active:bg-gray-50 transition ease-in-out duration-150 cursor-not-allowed"
>
Save
</button>
"""
end
def cancel_button(assigns) do
~H"""
<button
type="button"
class="inline-block mt-4 ml-2 px-4 py-2 border border-gray-300 dark:border-gray-500 text-sm leading-5 font-medium rounded-md text-red-700 bg-white dark:bg-gray-800 hover:text-red-500 dark:hover:text-red-400 focus:outline-none focus:border-blue-300 focus:ring active:text-red-800 active:bg-gray-50 transition ease-in-out duration-150 "
phx-click="cancel_add_funnel"
>
Cancel
</button>
"""
end
def handle_event("add-step", _value, socket) do
step_count = socket.assigns.step_count
step_ids = socket.assigns.step_ids
if step_count < 5 do
{:noreply, assign(socket, step_count: step_count + 1)}
if length(step_ids) < Funnel.max_steps() do
first_free_idx = find_sequence_break(step_ids)
new_ids = step_ids ++ [first_free_idx]
{:noreply, assign(socket, step_ids: new_ids)}
else
{:noreply, socket}
end
end
def handle_event("remove-step", %{"step-idx" => idx}, socket) do
idx = String.to_integer(idx)
step_ids = List.delete(socket.assigns.step_ids, idx)
{:noreply, assign(socket, step_ids: step_ids)}
end
def handle_event("validate", %{"funnel" => params}, socket) do
changeset =
Plausible.Funnels.create_changeset(
socket.assigns.site,
socket.assigns.site
|> Plausible.Funnels.create_changeset(
params["name"],
params["steps"] || []
)
@ -123,23 +208,33 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
end
def handle_event("save", %{"funnel" => params}, %{assigns: %{site: site}} = socket) do
IO.inspect(:saving)
case Plausible.Funnels.create(site, params["name"], params["steps"]) do
{:ok, funnel} ->
send(self(), {:funnel_saved, funnel})
send(self(), {:funnel_saved, Map.put(funnel, :steps_count, length(params["steps"]))})
{:noreply, socket}
{:error, changeset} ->
IO.inspect(changeset)
IO.inspect(to_form(changeset))
{:noreply,
assign(socket,
form: to_form(Map.put(changeset, :action, :validate))
)}
end
end
Ecto.Changeset.traverse_errors(changeset, fn thing ->
IO.inspect(thing)
end)
defp find_sequence_break(input) do
input
|> Enum.sort()
|> Enum.with_index(1)
|> Enum.reduce_while(nil, fn {x, order}, _ ->
if x != order do
{:halt, order}
else
{:cont, order + 1}
end
end)
end
{:noreply, assign(socket, form: to_form(changeset))}
end
defp has_steps_errors?(f) do
not f.source.valid?
end
end

@ -9,8 +9,6 @@ defmodule PlausibleWeb.Live.FunnelSettings.InputPicker do
socket
|> assign(assigns)
|> assign(:candidate, 0)
|> assign_new(:display_value, fn -> "" end)
|> assign_new(:submit_value, fn -> "" end)
|> assign_new(:choices, fn ->
Enum.take(assigns.options, @max_options_displayed)
end)
@ -22,6 +20,8 @@ defmodule PlausibleWeb.Live.FunnelSettings.InputPicker do
attr(:id, :any, default: nil)
attr(:options, :list, required: true)
attr(:submit_name, :string, required: true)
attr(:display_value, :string, default: "")
attr(:submit_value, :string, default: "")
def render(assigns) do
~H"""
@ -47,7 +47,14 @@ defmodule PlausibleWeb.Live.FunnelSettings.InputPicker do
<.dropdown_anchor id={@id} />
<input type="hidden" name={@submit_name} value={@submit_value} />
<input
type="hidden"
name={@submit_name}
value={@submit_value}
phx-change="update-value"
phx-target={@myself}
id={"submit-#{@id}"}
/>
</div>
</div>
@ -264,6 +271,12 @@ defmodule PlausibleWeb.Live.FunnelSettings.InputPicker do
end
end
def handle_event(a, b, socket) do
a |> IO.inspect(label: :a)
b |> IO.inspect(label: :b)
{:noreply, socket}
end
defp do_select(socket, submit_value, display_value) do
id = socket.assigns.id

Loading…
Cancel
Save