https://avo.cool logo
Help with DateTimeFilter reacting with a Select...
# avo-3
l
Help with DateTimeFilter reacting with a SelectFilter
m
Thread automatically created by Lucas Geron in #1138182212655788054
l
Hey @loud-jewelry-99127, nice to meet you. I need some help, I think you're the right person to help me. I'm trying to implement a
DateTimeFilter
filter using the
react
hook. But I'm not sure if I missed something along the way. Here's roughly what I want to do...
Copy code
rb
# my_resource.rb
def filters
  filter Filters::CreatedAtPresetFilter
  filter Filters::CreatedAtFilter
end
Copy code
rb
# app/avo/filters/created_at_preset_filter.rb
module Filters
  class CreatedAtPresetFilter < Avo::Filters::SelectFilter

    def options
      {
        past_week: 'Past Week',
        past_month: 'Past Month',
        today: 'Today'
      }
    end

    def apply(_request, query, value)
      query.where(created_at: from(value)..to)
    end

    def react
      puts '@' * 120
      puts 'Reacting hook from CreatedAtPresetFilter triggered'.
      puts '@' * 120
    end

    private
    def to
      @to ||= Time.zone.now.end_of_day
    end

    def from(value)
      case value
      when 'past_week'
        1.week.ago.beginning_of_day
      when 'past_month'
        1.month.ago.beginning_of_day
      when 'today'
        Time.zone.now.beginning_of_day
      end
    end
  end
and...
Copy code
rb
# app/avo/filters/created_at_filter.rb
module Filters
  class CreatedAtFilter < Avo::Filters::DateTimeFilter
   
    def apply(_request, query, value)
      from, to = value.split(' to ')

      puts '=' * 120
      puts applied_filters.inspect
      puts '=' * 120

      query.where(created_at: from..to)
    end

    # def default # This is not working too
    #   "#{1.week.ago.strftime('%Y-%m-%d')} to #{Time.zone.now.strftime('%Y-%m-%d')}"
    # end

    def react
      puts '@' * 120
      puts 'Reacting hook from CreatedAtFilter triggered'
      puts '@' * 120

      if applied_filters['Avo::Filters::CreatedAtPresetFilter '].present? && applied_filters['Avo::Filters::CreatedAtFilter '].blank?

       preset_value = applied_filters['Avo::Filters::ArwenBalancePointInTimePreset']
      from_date = parse_from_preset(preset_value)
      to_date = Time.zone.now.end_of_day

      "#{from_date.strftime('%Y-%m-%d')} to #{to_date.strftime('%Y-%m-%d')}"
      end
    end

    private
    def parse_from_preset(value)
      case value
      when 'past_week'
        1.week.ago.beginning_of_day
      when 'past_month'
        1.month.ago.beginning_of_day
      when 'today'
        Time.zone.now.beginning_of_day
      end
    end
  end
end
But when i select ' Past week' the only logged message is:
Copy code
rb
# =========...
{"Avo::Filters::CreatedAtPresetFilter" => "past_week"}
# ==========
* I'm using Avo Advanced. Not sure if need to setup something related to the
react
hook, or, what is the expected default/injected value of the DateTimeFilter...
l
Hi @little-article-64926 👋 Could you explain what you're trying to achieve?
l
So, the idea is filter by a custom date range, or, selecting a preset, like the 'past week'. The goal is to fill the created_at range when an option is selected... when the preset option is selected, auto-fill the created at range. Does it make sense? https://cdn.discordapp.com/attachments/1471951972532879452/1471957676010377378/image.png?ex=6990d2e9&is=698f8169&hm=31771f506fce62254d626be81398c14e7888a5395879506c0650f8c82219e681&
l
Yes it makes sense, I think you're on the right path, I'm on mobile right now and can't play arround with code, my guess is that date filter have some caveat with react, maybe it expects a specific format, I'll give it a try tomorrow and get back to you with feedback
The filter reacts whenever other filter is applied, not when other filter is selected
Maybe you're expecting it to react on select?
l
Hey Paul, sorry for the delay in replying. I did some tests here and actually, the React event works when the option is selected. The real problem I'm facing is that I'm applying the filter in a nested view, and because of this, the React method isn't being triggered as I expected. Consider my code (which is working), let's call it the 'Car' resource (for example). If I visit Car Resources and apply the filters, the filter is applied as expected and React works as it should. However, in my real case, I'm applying the filter in the 'Garage' resource...
Copy code
rb
# app/avo/resources/garage.rb
... field :cars, as: :has_many
#...
This renders the Car filters in the Garage resource. In the Car resource, the configuration is as I mentioned before... In this scenario, React doesn't work. In the main resource (Car), this works as expected, but nested (through a 'field cars, as: :has_many') it doesn't. Question: Is it possible to fix this?
l
Hi, I confirmed that this is the case. It seems to not be working within association tables. I created a GitHub issue to track this. https://github.com/avo-hq/avo/issues/4266
7 Views