Skip to content

Flop.Phoenix.table breaks with multiple dates in a filter  #295

@mjrusso

Description

@mjrusso

I have filtering by date implemented in a similar manner to #155 (same field repeated, with :<= and :>= filters respectively).

I'm attempting to consolidate into a single field (to capture both the start and end of the range). It's relatively straightforward: I built a custom "daterange" input, which itself renders two normal date inputs.

Relevant part of the schema:

    adapter_opts: [
      custom_fields: [
        started_at_date: [
          filter: {CustomFlopFilters, :date_range_filter, [source: :started_at]},
          ecto_type: :date,
          operators: [:in]
        ]
      ]
    ]

And the filter field:

    started_at_date: [
      label: "Started at",
      multiple: true,
      op: :in,
      type: "daterange"
    ]

Using the :in operator in this way (and multiple: true) is definitely a hack. I don't love it, but it (almost) works.

Params come in from the form like this, as we would expect:

%{
  "filters" => %{
    "0" => %{
      "field" => "started_at_date",
      "op" => "in",
      "value" => ["2023-01-04", "2023-12-04"]
    }
  }
}

(The custom filter interprets the first value as "from" and the second value as "to" in the range.)

And serializing to and from the URL works properly.

The only trouble is in Flop.Phoenix.table, which crashes in build_path/3 (similar-ish to #282):

[error] GenServer #PID<0.1082.0> terminating
** (ArgumentError) cannot encode maps inside lists when the map has 0 or more than 1 element, got: ~D[2023-01-04]
    (plug 1.15.2) lib/plug/conn/query.ex:370: anonymous fn/3 in Plug.Conn.Query.encode_pair/3
    (elixir 1.15.4) lib/enum.ex:4317: Enum.flat_map_list/2
    (plug 1.15.2) lib/plug/conn/query.ex:379: Plug.Conn.Query.encode_pair/3
    (plug 1.15.2) lib/plug/conn/query.ex:406: anonymous fn/3 in Plug.Conn.Query.encode_kv/3
    (elixir 1.15.4) lib/enum.ex:1260: anonymous fn/3 in Enum.flat_map/2
    (stdlib 5.0.2) maps.erl:416: :maps.fold_1/4
    (elixir 1.15.4) lib/enum.ex:2522: Enum.flat_map/2
    (plug 1.15.2) lib/plug/conn/query.ex:410: Plug.Conn.Query.encode_kv/3
    (plug 1.15.2) lib/plug/conn/query.ex:406: anonymous fn/3 in Plug.Conn.Query.encode_kv/3
    (elixir 1.15.4) lib/enum.ex:1260: anonymous fn/3 in Enum.flat_map/2
    (stdlib 5.0.2) maps.erl:416: :maps.fold_1/4
    (elixir 1.15.4) lib/enum.ex:2522: Enum.flat_map/2
    (plug 1.15.2) lib/plug/conn/query.ex:410: Plug.Conn.Query.encode_kv/3
    (plug 1.15.2) lib/plug/conn/query.ex:406: anonymous fn/3 in Plug.Conn.Query.encode_kv/3
    (elixir 1.15.4) lib/enum.ex:1260: anonymous fn/3 in Enum.flat_map/2
    (stdlib 5.0.2) maps.erl:416: :maps.fold_1/4
    (elixir 1.15.4) lib/enum.ex:2522: Enum.flat_map/2
    (plug 1.15.2) lib/plug/conn/query.ex:410: Plug.Conn.Query.encode_kv/3
    (plug 1.15.2) lib/plug/conn/query.ex:348: Plug.Conn.Query.encode/2
    (flop_phoenix 0.22.4) lib/flop_phoenix.ex:1863: Flop.Phoenix.build_path/3

It looks like Flop Phoenix is doing something like the following, which is crashing with the error above:

 Plug.Conn.Query.encode(%{
   "filters" => %{
     "0" => %{
       "field" => "started_at_date",
       "op" => "in",
       "value" => [~D[2023-01-12], ~D[2023-05-12]]
     }
   }
 })

([~D[2023-01-12]] doesn't work either. ~D[2023-01-12] does, which is what we'd expect otherwise casting anything as a date wouldn't work.)

All this being said, I'm not sure if Flop Phoenix should do anything about this, but I did want to file this issue as a form of documentation in case anyone runs into a similar issue.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions