diff --git a/assets/js/dashboard/components/combobox.js b/assets/js/dashboard/components/combobox.js index 45325345d..9514d4c80 100644 --- a/assets/js/dashboard/components/combobox.js +++ b/assets/js/dashboard/components/combobox.js @@ -13,12 +13,10 @@ function Spinner() { } export default function MyCombobox(props) { - const [selectedOption, setSelectedOption] = useState(props.initialSelectedItem.name) const [options, setOptions] = useState([]) const [loading, setLoading] = useState(false) function fetchOptions(query) { - setSelectedOption(query) setLoading(true) return props.fetchOptions(query).then((loadedOptions) => { @@ -31,18 +29,14 @@ export default function MyCombobox(props) { function onOpen() { setOptions([]) - fetchOptions(selectedOption || '') + fetchOptions(props.selection.value) } - function onSelect(item) { - props.onSelect(item) - props.onInput(item) - setSelectedOption(item) - } - - function onInput(input) { - props.onInput(input) - debouncedFetchOptions(input) + function onBlur(e) { + !props.strict && props.onChange({ + value: e.target.value, + label: e.target.value + }) } @@ -62,18 +56,16 @@ export default function MyCombobox(props) { } else { return options.map((option) => { - const displayValue = option.name && option.name || option - return ( `relative cursor-default select-none py-2 px-3 ${active ? 'bg-indigo-600 text-white' : 'text-gray-900' }` } value={option} > - {displayValue} + {option.label} ) }) @@ -81,14 +73,15 @@ export default function MyCombobox(props) { } return ( - + props.onChange(val)}>
item && item.name && item.name || item} - onChange={(event) => onInput(event.target.value)} + displayValue={(item) => item && item.label} + onChange={(event) => debouncedFetchOptions(event.target.value)} + onBlur={onBlur} />
{!loading && } diff --git a/assets/js/dashboard/stats/modals/filter.js b/assets/js/dashboard/stats/modals/filter.js index 751e24d74..1d075d586 100644 --- a/assets/js/dashboard/stats/modals/filter.js +++ b/assets/js/dashboard/stats/modals/filter.js @@ -32,8 +32,8 @@ function getFormState(filterGroup, query) { const propValue = valueWithoutPrefix(entries[0][1]) return { - 'prop_key': { name: propKey, value: propKey, type: FILTER_TYPES.is }, - 'prop_value': { name: propValue, value: propValue, type: toFilterType(entries[0][1]) } + 'prop_key': { label: propKey, value: propKey, type: FILTER_TYPES.is }, + 'prop_value': { label: propValue, value: propValue, type: toFilterType(entries[0][1]) } } } } @@ -43,18 +43,18 @@ function getFormState(filterGroup, query) { const type = toFilterType(rawFilterValue) const filterValue = valueWithoutPrefix(rawFilterValue) - let filterName = filterValue + let filterLabel = filterValue if (filter === 'country' && filterValue !== '') { - filterName = (new URLSearchParams(window.location.search)).get('country_name') + filterLabel = (new URLSearchParams(window.location.search)).get('country_name') } if (filter === 'region' && filterValue !== '') { - filterName = (new URLSearchParams(window.location.search)).get('region_name') + filterLabel = (new URLSearchParams(window.location.search)).get('region_name') } if (filter === 'city' && filterValue !== '') { - filterName = (new URLSearchParams(window.location.search)).get('city_name') + filterLabel = (new URLSearchParams(window.location.search)).get('city_name') } - return Object.assign(result, { [filter]: { name: filterName, value: filterValue, type } }) + return Object.assign(result, { [filter]: { label: filterLabel, value: filterValue, type } }) }, {}) } @@ -161,10 +161,10 @@ class FilterModal extends React.Component { handleSubmit() { const { formState } = this.state; - const filters = Object.entries(formState).reduce((res, [filterKey, { type, value, name }]) => { - if (filterKey === 'country') { res.push({ filter: 'country_name', value: name }) } - if (filterKey === 'region') { res.push({ filter: 'region_name', value: name }) } - if (filterKey === 'city') { res.push({ filter: 'city_name', value: name }) } + const filters = Object.entries(formState).reduce((res, [filterKey, { type, value, label }]) => { + if (filterKey === 'country') { res.push({ filter: 'country_name', value: label }) } + if (filterKey === 'region') { res.push({ filter: 'region_name', value: label }) } + if (filterKey === 'city') { res.push({ filter: 'city_name', value: label }) } if (filterKey === 'prop_value') { return res } if (filterKey === 'prop_key') { let propValue = formState['prop_value'] @@ -180,27 +180,11 @@ class FilterModal extends React.Component { this.selectFiltersAndCloseModal(filters) } - onSelect(filterName) { - return (value) => { - console.log('onSelect: ' + value) + onChange(filterName) { + return (selection) => { this.setState(prevState => ({ formState: Object.assign(prevState.formState, { - [filterName]: Object.assign(prevState.formState[filterName], { value: value.code, name: value.name }) - }) - })) - } - } - - onInput(filterName) { - if (this.state.selectedFilterGroup === 'location') { - return () => { } - } - - return (value) => { - console.log('onInput: ' + value) - this.setState(prevState => ({ - formState: Object.assign(prevState.formState, { - [filterName]: Object.assign(prevState.formState[filterName], { value }) + [filterName]: Object.assign(prevState.formState[filterName], selection) }) })) } @@ -260,7 +244,8 @@ class FilterModal extends React.Component { } renderSearchBox(filter) { - return + const isStrict = this.state.selectedFilterGroup === 'location' + return } renderFilterInputs() { diff --git a/lib/plausible/stats/filter_suggestions.ex b/lib/plausible/stats/filter_suggestions.ex index 99144a0eb..68c460406 100644 --- a/lib/plausible/stats/filter_suggestions.ex +++ b/lib/plausible/stats/filter_suggestions.ex @@ -21,8 +21,8 @@ defmodule Plausible.Stats.FilterSuggestions do |> Enum.slice(0..24) |> Enum.map(fn match -> %{ - code: match.alpha_2, - name: match.name + value: match.alpha_2, + label: match.name } end) end @@ -41,8 +41,8 @@ defmodule Plausible.Stats.FilterSuggestions do subdiv = Location.get_subdivision(c) %{ - code: c, - name: subdiv.name + value: c, + label: subdiv.name } end) end @@ -64,8 +64,8 @@ defmodule Plausible.Stats.FilterSuggestions do |> Enum.slice(0..24) |> Enum.map(fn subdiv -> %{ - code: subdiv.code, - name: subdiv.name + value: subdiv.code, + label: subdiv.name } end) end @@ -84,8 +84,8 @@ defmodule Plausible.Stats.FilterSuggestions do city = Location.get_city(c) %{ - code: Integer.to_string(c), - name: (city && city.name) || "N/A" + value: Integer.to_string(c), + label: (city && city.name) || "N/A" } end) end @@ -111,8 +111,8 @@ defmodule Plausible.Stats.FilterSuggestions do |> Enum.slice(0..24) |> Enum.map(fn c -> %{ - code: Integer.to_string(c.id), - name: c.name + value: Integer.to_string(c.id), + label: c.name } end) end @@ -127,6 +127,7 @@ defmodule Plausible.Stats.FilterSuggestions do String.downcase(filter_search) ) end) + |> wrap_suggestions() end def filter_suggestions(site, query, "prop_key", filter_search) do @@ -151,6 +152,7 @@ defmodule Plausible.Stats.FilterSuggestions do ClickhouseRepo.all(q) |> Enum.filter(fn suggestion -> suggestion != "" end) + |> wrap_suggestions() end def filter_suggestions(site, query, "prop_value", filter_search) do @@ -175,6 +177,7 @@ defmodule Plausible.Stats.FilterSuggestions do ClickhouseRepo.all(q) |> Enum.filter(fn suggestion -> suggestion != "" end) + |> wrap_suggestions() end def filter_suggestions(site, query, filter_name, filter_search) do @@ -298,5 +301,10 @@ defmodule Plausible.Stats.FilterSuggestions do ClickhouseRepo.all(q) |> Enum.filter(fn suggestion -> suggestion != "" end) + |> wrap_suggestions() + end + + defp wrap_suggestions(list) do + Enum.map(list, fn val -> %{value: val, label: val} end) end end diff --git a/test/plausible_web/controllers/api/stats_controller/suggestions_test.exs b/test/plausible_web/controllers/api/stats_controller/suggestions_test.exs index ff0dbaab0..6742f42a7 100644 --- a/test/plausible_web/controllers/api/stats_controller/suggestions_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/suggestions_test.exs @@ -7,14 +7,22 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do test "returns suggestions for pages without a query", %{conn: conn, site: site} do conn = get(conn, "/api/stats/#{site.domain}/suggestions/page?period=month&date=2019-01-01") - assert json_response(conn, 200) == ["/", "/register", "/contact", "/irrelevant"] + assert json_response(conn, 200) == [ + %{"label" => "/", "value" => "/"}, + %{"label" => "/register", "value" => "/register"}, + %{"label" => "/contact", "value" => "/contact"}, + %{"label" => "/irrelevant", "value" => "/irrelevant"} + ] end test "returns suggestions for pages with a query", %{conn: conn, site: site} do conn = get(conn, "/api/stats/#{site.domain}/suggestions/page?period=month&date=2019-01-01&q=re") - assert json_response(conn, 200) == ["/register", "/irrelevant"] + assert json_response(conn, 200) == [ + %{"label" => "/register", "value" => "/register"}, + %{"label" => "/irrelevant", "value" => "/irrelevant"} + ] end test "returns suggestions for pages without any suggestions", %{conn: conn, site: site} do @@ -37,7 +45,10 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do conn = get(conn, "/api/stats/#{site.domain}/suggestions/source?period=month&date=2019-01-01") - assert json_response(conn, 200) == ["10words", "Bing"] + assert json_response(conn, 200) == [ + %{"label" => "10words", "value" => "10words"}, + %{"label" => "Bing", "value" => "Bing"} + ] end test "returns suggestions for countries", %{conn: conn, site: site} do @@ -47,7 +58,7 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do "/api/stats/#{site.domain}/suggestions/country?period=month&date=2019-01-01&q=Unit" ) - assert json_response(conn, 200) == [%{"code" => "US", "name" => "United States"}] + assert json_response(conn, 200) == [%{"value" => "US", "label" => "United States"}] end test "returns suggestions for regions", %{conn: conn, user: user} do @@ -64,7 +75,7 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do "/api/stats/#{site.domain}/suggestions/region?q=Har" ) - assert json_response(conn, 200) == [%{"code" => "EE-37", "name" => "Harjumaa"}] + assert json_response(conn, 200) == [%{"value" => "EE-37", "label" => "Harjumaa"}] end test "returns suggestions for cities", %{conn: conn, user: user} do @@ -81,7 +92,7 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do "/api/stats/#{site.domain}/suggestions/city?q=Kär" ) - assert json_response(conn, 200) == [%{"code" => "591632", "name" => "Kärdla"}] + assert json_response(conn, 200) == [%{"value" => "591632", "label" => "Kärdla"}] end test "returns suggestions for countries without country in search", %{conn: conn, site: site} do @@ -98,14 +109,14 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do conn = get(conn, "/api/stats/#{site.domain}/suggestions/screen?period=month&date=2019-01-01") - assert json_response(conn, 200) == ["Desktop"] + assert json_response(conn, 200) == [%{"value" => "Desktop", "label" => "Desktop"}] end test "returns suggestions for browsers", %{conn: conn, site: site} do conn = get(conn, "/api/stats/#{site.domain}/suggestions/browser?period=month&date=2019-01-01") - assert json_response(conn, 200) == ["Chrome"] + assert json_response(conn, 200) == [%{"label" => "Chrome", "value" => "Chrome"}] end test "returns suggestions for browser versions", %{conn: conn, site: site} do @@ -117,13 +128,13 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do "/api/stats/#{site.domain}/suggestions/browser_version?period=month&date=2019-01-01&filters=#{filters}" ) - assert json_response(conn, 200) == ["78.0"] + assert json_response(conn, 200) == [%{"value" => "78.0", "label" => "78.0"}] end test "returns suggestions for OS", %{conn: conn, site: site} do conn = get(conn, "/api/stats/#{site.domain}/suggestions/os?period=month&date=2019-01-01") - assert json_response(conn, 200) == ["Mac"] + assert json_response(conn, 200) == [%{"value" => "Mac", "label" => "Mac"}] end test "returns suggestions for OS versions", %{conn: conn, site: site} do @@ -135,7 +146,7 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do "/api/stats/#{site.domain}/suggestions/os_version?period=month&date=2019-01-01&filters=#{filters}" ) - assert json_response(conn, 200) == ["10.15"] + assert json_response(conn, 200) == [%{"label" => "10.15", "value" => "10.15"}] end test "returns suggestions for OS versions with search", %{conn: conn, site: site} do @@ -154,7 +165,9 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do conn = get(conn, "/api/stats/#{site.domain}/suggestions/referrer?period=month&date=2019-01-01") - assert json_response(conn, 200) == ["10words.com/page1"] + assert json_response(conn, 200) == [ + %{"value" => "10words.com/page1", "label" => "10words.com/page1"} + ] end end @@ -183,7 +196,11 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do conn = get(conn, "/api/stats/#{site.domain}/suggestions/prop_key?period=day&date=2022-01-01") - assert json_response(conn, 200) |> Enum.sort() == ["author", "dark_mode", "logged_in"] + assert json_response(conn, 200) |> Enum.map(& &1["value"]) |> Enum.sort() == [ + "author", + "dark_mode", + "logged_in" + ] end test "returns suggestions for prop key with value filter", %{conn: conn, site: site} do @@ -213,7 +230,10 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do "/api/stats/#{site.domain}/suggestions/prop_key?period=day&date=2022-01-01&filters=#{filters}" ) - assert json_response(conn, 200) |> Enum.sort() == ["dark_mode", "logged_in"] + assert json_response(conn, 200) |> Enum.map(& &1["value"]) |> Enum.sort() == [ + "dark_mode", + "logged_in" + ] end test "returns suggestions for prop value with no filter", %{conn: conn, site: site} do @@ -238,7 +258,11 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do conn = get(conn, "/api/stats/#{site.domain}/suggestions/prop_value?period=day&date=2022-01-01") - assert json_response(conn, 200) |> Enum.sort() == ["Uku Taht", "false", "true"] + assert json_response(conn, 200) |> Enum.map(& &1["value"]) |> Enum.sort() == [ + "Uku Taht", + "false", + "true" + ] end test "returns suggestions for prop value with filter on prop key", %{conn: conn, site: site} do @@ -268,7 +292,7 @@ defmodule PlausibleWeb.Api.StatsController.SuggestionsTest do "/api/stats/#{site.domain}/suggestions/prop_value?period=day&date=2022-01-01&filters=#{filters}" ) - assert json_response(conn, 200) |> Enum.sort() == ["Uku Taht"] + assert json_response(conn, 200) |> Enum.map(& &1["value"]) |> Enum.sort() == ["Uku Taht"] end test "when date is borked, bad request is returned", %{