From 707a932dd5a1d41d2ae9014217f3eaece4286d61 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Mon, 22 May 2023 15:14:23 +0200 Subject: [PATCH] wip --- assets/js/app.js | 3 +- lib/plausible/funnel.ex | 17 +- lib/plausible/funnels.ex | 8 +- .../live/funnel_settings/form.ex | 181 +++++++++++++----- .../live/funnel_settings/input_picker.ex | 19 +- 5 files changed, 176 insertions(+), 52 deletions(-) diff --git a/assets/js/app.js b/assets/js/app.js index 51d51caae..c8c881814 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -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) => { diff --git a/lib/plausible/funnel.ex b/lib/plausible/funnel.ex index 787637324..097dc94c9 100644 --- a/lib/plausible/funnel.ex +++ b/lib/plausible/funnel.ex @@ -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 ) diff --git a/lib/plausible/funnels.ex b/lib/plausible/funnels.ex index 8b229555e..3079be211 100644 --- a/lib/plausible/funnels.ex +++ b/lib/plausible/funnels.ex @@ -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() diff --git a/lib/plausible_web/live/funnel_settings/form.ex b/lib/plausible_web/live/funnel_settings/form.ex index d988f5edd..5fdca209d 100644 --- a/lib/plausible_web/live/funnel_settings/form.ex +++ b/lib/plausible_web/live/funnel_settings/form.ex @@ -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]} />
<%= 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)} - /> - - - + Add another step - +
+
+ <.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) + } + /> +
+ + <.remove_step_button + :if={length(@step_ids) > Funnel.min_steps()} + target={@myself} + step_idx={step_idx} + /> +
+ + <.add_step_button :if={length(@step_ids) < Funnel.max_steps()} target={@myself} />
- - + <%= if has_steps_errors?(f) do %> + <.submit_button_inactive /> + <% else %> + <.submit_button /> + <% end %> + <.cancel_button />
@@ -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""" +
+ + + + + + + +
+ """ + end + + attr :target, :any + + def add_step_button(assigns) do + ~H""" + + + Add another step + + """ + end + + def submit_button(assigns) do + ~H""" + + """ + end + + def submit_button_inactive(assigns) do + ~H""" + + """ + end + + def cancel_button(assigns) do + ~H""" + + """ + 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 diff --git a/lib/plausible_web/live/funnel_settings/input_picker.ex b/lib/plausible_web/live/funnel_settings/input_picker.ex index 90831eeb7..3cb50d7dc 100644 --- a/lib/plausible_web/live/funnel_settings/input_picker.ex +++ b/lib/plausible_web/live/funnel_settings/input_picker.ex @@ -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} /> - + @@ -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