-
-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: date time filter * lint * use button instead submit on close * tweaks and tests * lint & tweaks * tweak
- Loading branch information
Showing
14 changed files
with
248 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
app/javascript/js/controllers/date_time_filter_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import flatpickr from 'flatpickr' | ||
|
||
import BaseFilterController from './filter_controller' | ||
|
||
export default class extends BaseFilterController { | ||
static targets = ['input'] | ||
|
||
static values = { | ||
class: String, | ||
pickerOptions: Object, | ||
} | ||
|
||
getFilterValue() { | ||
return this.inputTarget.value | ||
} | ||
|
||
getFilterClass() { | ||
return this.classValue | ||
} | ||
|
||
connect() { | ||
this.initFlatpickr() | ||
} | ||
|
||
initFlatpickr() { | ||
this.pickerInstance = flatpickr(this.inputTarget, this.pickerOptionsValue) | ||
} | ||
|
||
clear() { | ||
this.inputTarget._flatpickr.clear() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<%= content_tag :div, data: { | ||
controller: "date-time-filter", | ||
filter_name: filter.name, | ||
date_time_filter_class_value: filter.class.to_s, | ||
date_time_filter_keep_filters_panel_open_value: @resource.keep_filters_panel_open, | ||
date_time_filter_picker_options_value: filter.picker_options(@applied_filters[filter.class.to_s]) | ||
} do %> | ||
<%= filter_wrapper name: filter.name do %> | ||
<div class="flex relative"> | ||
<div class="self-center w-full" data-slot="value"> | ||
<%= text_field_tag :value, "", | ||
class: input_classes('w-full mb-0 !pr-9'), | ||
placeholder: filter.class.empty_message, | ||
data: { | ||
date_time_filter_target: "input", | ||
} %> | ||
</div> | ||
<%= content_tag :button, | ||
class: "absolute right-0 self-center mr-4 uppercase font-semibold text-xs", | ||
id: :reset, | ||
type: :button, | ||
title: t("avo.reset").capitalize, | ||
data: { | ||
action: "click->date-time-filter#clear", | ||
tippy: :tooltip | ||
} do %> | ||
<%= svg "avo/times", class: "h-4" %> | ||
<% end %> | ||
</div> | ||
<div class="flex justify-end"> | ||
<%= a_button class: 'mt-4', color: :primary, data: { action: "date-time-filter#changeFilter" }, size: :xs do %> | ||
<%= filter.button_label || "#{t("avo.filter_by")} #{filter.name}" %> | ||
<% end %> | ||
</div> | ||
<%= link_to 'url_redirect', request.url, data: { 'date-time-filter-target': 'urlRedirect', 'turbo-frame': params[:turbo_frame] }, style: 'hidden', class: 'hidden' %> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module Avo | ||
module Filters | ||
class DateTimeFilter < BaseFilter | ||
class_attribute :type, default: :date_time | ||
class_attribute :mode, default: :range | ||
|
||
self.template = "avo/base/date_time_filter" | ||
|
||
def picker_format | ||
case type | ||
when :date | ||
"Y-m-d" | ||
when :date_time | ||
"Y-m-d H:i:S" | ||
when :time | ||
"H:i:S" | ||
end | ||
end | ||
|
||
def picker_options(value) | ||
{ | ||
defaultDate: value, | ||
enableTime: has_time?, | ||
enableSeconds: has_time?, | ||
time_24hr: has_time? ? true : nil, | ||
noCalendar: type == :time, | ||
mode: mode, | ||
dateFormat: picker_format, | ||
minuteIncrement: has_time? ? 1 : nil | ||
}.compact | ||
end | ||
|
||
def has_time? | ||
@has_time ||= type.in?([:time, :date_time]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
class Avo::Filters::<%= class_name.camelize %> < Avo::Filters::DateTimeFilter | ||
self.name = "<%= name.underscore.humanize %>" | ||
# self.type = :date_time | ||
# self.mode = :range | ||
# self.visible = -> do | ||
# true | ||
# end | ||
|
||
def apply(request, query, value) | ||
query | ||
end | ||
|
||
# def picker_format | ||
# case type | ||
# when :date_time | ||
# "Y-m-d H:i:S" | ||
# when :time | ||
# "H:i:S" | ||
# end | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class Avo::Filters::Birthday < Avo::Filters::DateTimeFilter | ||
self.name = "Birthday" | ||
self.button_label = "Apply birthday filter" | ||
self.empty_message = "Filter by birthday" | ||
self.type = :date | ||
|
||
def apply(request, query, value) | ||
start_date, end_date = value.split(" to ") | ||
|
||
query.where(birthday: start_date..end_date) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class Avo::Filters::StartingAt < Avo::Filters::DateTimeFilter | ||
self.name = "The starting at filter" | ||
self.button_label = "Filter by start time" | ||
self.empty_message = "Search by start time" | ||
self.type = :time | ||
self.mode = :single | ||
|
||
def apply(request, query, value) | ||
query.where("to_char(starting_at, 'HH24:MI:SS') = ?", value) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters