Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update boolean input filter UI #328

Merged
merged 4 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,444 changes: 4 additions & 1,440 deletions assets/package-lock.json

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions lib/torch/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,67 @@ defmodule Torch.Helpers do
def paginate(query, repo, params, settings \\ [page_size: 10]) do
Scrivener.paginate(query, Scrivener.Config.new(repo, settings, params))
end

@doc """
Removes any "un-set" boolean parameters from the filter params list.

Due to the nature of boolean params (on/off) it becomes hard to include
the "filter on true" and "filter on false" states while also including a
third option of "don't filter at all" on this boolean argument. Since the
parameter is always sent in the filter form (due to the checkbox).

We need a way to encode 3 states for a boolean field (on, off, any|ignore).

This function takes a list of boolean field names, and will remove from the
params argument, any matching boolean fields whose current value is set to
"any" (which is the default placeholder Torch UI uses to signify this third
boolean state).

## Examples

iex> strip_unset_booleans(%{}, "post", [])
%{}

iex> strip_unset_booleans(%{"post" => %{"title_contains" => "foo"}}, "post", [])
%{"post" => %{"title_contains" => "foo"}}

iex> strip_unset_booleans(%{"post" => %{"title_equals" => "true"}}, "post", [:title])
%{"post" => %{"title_equals" => "true"}}

iex> strip_unset_booleans(%{"post" => %{"title_equals" => "any"}}, "post", [:title])
%{"post" => %{}}

iex> strip_unset_booleans(%{"post" => %{"name_contains" => "foo", "title_equals" => "any"}}, "post", [:title])
%{"post" => %{"name_contains" => "foo"}}

"""
@spec strip_unset_booleans(params, binary, [atom]) :: params
def strip_unset_booleans(params, _, []), do: params

def strip_unset_booleans(params, schema_name, [bool_field | rest] = bool_fields)
when is_list(bool_fields) and is_binary(schema_name) do
params
|> strip_unset_boolean(schema_name, bool_field)
|> strip_unset_booleans(schema_name, rest)
end

defp strip_unset_boolean(params, schema_name, bool_field) when is_atom(bool_field) do
strip_unset_boolean(params, schema_name, to_string(bool_field))
end

defp strip_unset_boolean(params, schema_name, bool_field) when is_binary(bool_field) do
field_name = bool_field <> "_equals"

case Map.fetch(params, schema_name) do
{:ok, schema_params} ->
case Map.get(schema_params, field_name) do
nil -> params
"any" -> Map.put(params, schema_name, Map.drop(schema_params, [field_name]))
_v -> params
end

:error ->
params
end
end
end
3 changes: 3 additions & 0 deletions lib/torch/i18n/backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ defmodule Torch.I18n.Backend do
def message("Select End Date"), do: dgettext("default", "Select End Date")
def message("< Prev"), do: dgettext("default", "< Prev")
def message("Next >"), do: dgettext("default", "Next >")
def message("Any"), do: dgettext("default", "Any")
def message("True"), do: dgettext("default", "True")
def message("False"), do: dgettext("default", "False")
end
13 changes: 10 additions & 3 deletions lib/torch/views/filter_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,24 @@ defmodule Torch.FilterView do

iex> params = %{"post" => %{"draft_equals" => "false"}}
iex> filter_boolean_input(:post, :draft, params) |> safe_to_string()
"<input name=\\"post[draft_equals]\\" type=\\"hidden\\" value=\\"false\\"><input id=\\"post_draft_equals\\" name=\\"post[draft_equals]\\" type=\\"checkbox\\" value=\\"true\\">"
"<select class=\\"boolean-type\\" id=\\"post_draft_equals\\" name=\\"post[draft_equals]\\"><option value=\\"any\\"></option><option value=\\"true\\">True</option><option selected value=\\"false\\">False</option></select>"
"""
@spec filter_boolean_input(prefix, field, map) :: Phoenix.HTML.safe()
def filter_boolean_input(prefix, field, params) do
value =
case get_in(params, [to_string(prefix), "#{field}_equals"]) do
nil -> nil
nil -> "any"
"any" -> "any"
string when is_binary(string) -> string == "true"
end

checkbox(prefix, :"#{field}_equals", value: value)
opts = [
{"", "any"},
{message("True"), "true"},
{message("False"), "false"}
]

select(prefix, :"#{field}_equals", opts, class: "boolean-type", value: value)
end

defp torch_date_input(name, value) do
Expand Down
30 changes: 30 additions & 0 deletions priv/gettext/de/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -21,62 +21,92 @@ msgstr ""
"X-Generator: Poedit 2.4.1\n"
"X-Poedit-SourceCharset: UTF-8\n"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:22
msgid "< Prev"
msgstr "< Zurück"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:23
msgid "Next >"
msgstr "> Vorwärts"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:11
msgid "Choose one"
msgstr "Wählen Sie eine"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:10
msgid "Equals"
msgstr "Ist gleich"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:9
msgid "Contains"
msgstr "Beinhaltet"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:14
msgid "Greater Than"
msgstr "Grösser als"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:15
msgid "Greater Than Or Equal"
msgstr "Grösser als oder gleich"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:16
msgid "Less Than"
msgstr "Kleiner als"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:17
msgid "start"
msgstr "Start"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:18
msgid "end"
msgstr "Ende"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:20
msgid "Select Start Date"
msgstr "Startdatum auswählen"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:21
msgid "Select End Date"
msgstr "Enddatum auswählen"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:13
msgid "After"
msgstr "Danach"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:12
msgid "Before"
msgstr "Davor"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:19
msgid "Select Date"
msgstr "Datum wählen"

#, elixir-autogen, elixir-format
#: lib/torch/i18n/backend.ex:24
msgid "Any"
msgstr "Jeder"

#, elixir-autogen, elixir-format
#: lib/torch/i18n/backend.ex:26
msgid "False"
msgstr "Falsch"

#, elixir-autogen, elixir-format
#: lib/torch/i18n/backend.ex:25
msgid "True"
msgstr "Wahr"
30 changes: 30 additions & 0 deletions priv/gettext/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,92 @@ msgstr ""
"X-Generator: Poedit 2.4.1\n"
"X-Poedit-SourceCharset: UTF-8\n"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:22
msgid "< Prev"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:23
msgid "Next >"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:11
msgid "Choose one"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:10
msgid "Equals"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:9
msgid "Contains"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:14
msgid "Greater Than"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:15
msgid "Greater Than Or Equal"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:16
msgid "Less Than"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:17
msgid "start"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:18
msgid "end"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:20
msgid "Select Start Date"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:21
msgid "Select End Date"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:13
msgid "After"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:12
msgid "Before"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:19
msgid "Select Date"
msgstr ""

#, elixir-autogen, elixir-format
#: lib/torch/i18n/backend.ex:24
msgid "Any"
msgstr ""

#, elixir-autogen, elixir-format
#: lib/torch/i18n/backend.ex:26
msgid "False"
msgstr ""

#, elixir-autogen, elixir-format
#: lib/torch/i18n/backend.ex:25
msgid "True"
msgstr ""
30 changes: 30 additions & 0 deletions priv/gettext/en/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,92 @@ msgstr ""
"X-Generator: Poedit 2.4.1\n"
"X-Poedit-SourceCharset: UTF-8\n"

#, elixir-autogen
#: lib/torch/i18n/backend.ex:22
msgid "< Prev"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:23
msgid "Next >"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:11
msgid "Choose one"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:10
msgid "Equals"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:9
msgid "Contains"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:14
msgid "Greater Than"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:15
msgid "Greater Than Or Equal"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:16
msgid "Less Than"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:17
msgid "start"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:18
msgid "end"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:20
msgid "Select Start Date"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:21
msgid "Select End Date"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:13
msgid "After"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:12
msgid "Before"
msgstr ""

#, elixir-autogen
#: lib/torch/i18n/backend.ex:19
msgid "Select Date"
msgstr ""

#, elixir-autogen, elixir-format
#: lib/torch/i18n/backend.ex:24
msgid "Any"
msgstr ""

#, elixir-autogen, elixir-format
#: lib/torch/i18n/backend.ex:26
msgid "False"
msgstr ""

#, elixir-autogen, elixir-format
#: lib/torch/i18n/backend.ex:25
msgid "True"
msgstr ""
Loading