Load all suggestions asynchronously, unless `Mix.env == :test`

props-settings-ui-keyboard-propagation
Adam Rutkowski 3 years ago
parent bf95366754
commit ce1e2f68f7

@ -17,16 +17,24 @@ defmodule PlausibleWeb.Live.Components.ComboBox do
Any function can be supplied via `suggest_fun` attribute
- see the provided `ComboBox.StaticSearch`.
In case the `suggest_fun` runs an operation that could be deferred,
the `async=true` attr calls it in a background Task and updates the
suggestions asynchronously.
Similarly, the initial `options` don't have to be provided up-front
if e.g. querying the database for suggestions at initial render is
undesirable. In such case, lack of `options` attr value combined
with `async=true` will call `suggest_fun.("", [])` asynchronously
- that special clause can be used to provide the initial set
of suggestions updated right after the initial render.
In most cases the `suggest_fun` runs an operation that could be deferred,
so by default, the `async={true}` attr calls it in a background Task
and updates the suggestions asynchronously. This way, you can render
the component without having to wait for suggestions to load.
If you explicitly need to make the operation sychronous, you may
pass `async={false}` option.
If your initial `options` are not provided up-front at initial render,
lack of `options` attr value combined with `async=true` calls the
`suggest_fun.("", [])` asynchronously - that special clause can be used
to provide the initial set of suggestions updated right after the initial render.
To simplify integration testing, suggestions load up synchronously during
tests. This lets you skip waiting for suggestions messages
to arrive. The asynchronous behaviour alone is already tested in
ComboBox own test suite, so there is no need for additional
verification.
"""
use Phoenix.LiveComponent
alias Phoenix.LiveView.JS
@ -60,7 +68,7 @@ defmodule PlausibleWeb.Live.Components.ComboBox do
attr(:required, :boolean, default: false)
attr(:creatable, :boolean, default: false)
attr(:errors, :list, default: [])
attr(:async, :boolean, default: false)
attr(:async, :boolean, default: Mix.env() != :test)
def render(assigns) do
assigns =

@ -86,7 +86,6 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
]}
module={ComboBox}
suggest_fun={fn input, options -> suggest_page_paths(input, options, @site) end}
async={true}
creatable
/>
@ -185,7 +184,6 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
ComboBox.StaticSearch.suggest(input, options, weight_threshold: 0.8)
end
}
async={true}
/>
</div>
</div>

@ -20,17 +20,12 @@ defmodule PlausibleWeb.Live.PropsSettings.Form do
form = new_form(site)
prop_key_options =
site
|> Plausible.Props.suggest_keys_to_allow()
|> Enum.map(&{&1, &1})
{:ok,
assign(socket,
form: form,
prop_key_options: prop_key_options,
domain: domain,
rendered_by: pid,
prop_key_options_count: 0,
site: site
)}
end
@ -66,8 +61,24 @@ defmodule PlausibleWeb.Live.PropsSettings.Form do
"py-2"
]}
module={ComboBox}
options={@prop_key_options}
suggest_fun={&ComboBox.StaticSearch.suggest/2}
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
/>
@ -85,12 +96,12 @@ defmodule PlausibleWeb.Live.PropsSettings.Form do
</div>
<button
:if={length(@prop_key_options) > 0}
: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 all existing properties.
Already sending custom properties? Click to add <%= @prop_key_options_count %> existing properties we found.
</button>
</.form>
</div>
@ -98,6 +109,14 @@ defmodule PlausibleWeb.Live.PropsSettings.Form do
"""
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} ->

@ -95,20 +95,13 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do
test "currency combo works", %{conn: conn, site: site} do
lv = get_liveview(conn, site)
# Account for asynchronous updates. Here, the options, while static,
# are still a sizeable payload that we can defer before the user manages
# to click "this is revenue goal" switch.
# There's also throttling applied to the ComboBox.
:timer.sleep(200)
type_into_combo(lv, "currency_input", "Polish")
:timer.sleep(200)
html = render(lv)
assert element_exists?(html, ~s/a[phx-value-display-value="PLN - Polish Zloty"]/)
refute element_exists?(html, ~s/a[phx-value-display-value="EUR - Euro"]/)
type_into_combo(lv, "currency_input", "Euro")
:timer.sleep(200)
html = render(lv)
refute element_exists?(html, ~s/a[phx-value-display-value="PLN - Polish Zloty"]/)
@ -135,16 +128,12 @@ defmodule PlausibleWeb.Live.GoalSettings.FormTest do
type_into_combo(lv, "page_path_input", "/go/to/p")
# Account some large-ish margin for Clickhouse latency when providing suggestions asynchronously
# Might be too much, but also not enough on sluggish CI
:timer.sleep(350)
html = render(lv)
assert html =~ "Create &quot;/go/to/p&quot;"
assert html =~ "/go/to/page/1"
refute html =~ "/go/home"
type_into_combo(lv, "page_path_input", "/go/h")
:timer.sleep(350)
html = render(lv)
assert html =~ "/go/home"
refute html =~ "/go/to/page/1"

Loading…
Cancel
Save