diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb index 36e45b01..1fdfa33d 100644 --- a/app/controllers/transactions_controller.rb +++ b/app/controllers/transactions_controller.rb @@ -24,7 +24,16 @@ class TransactionsController < ApplicationController session.delete(ransack_session_key) elsif params[:remove_param] current_params = session[ransack_session_key] || {} - updated_params = delete_search_param(current_params, params[:remove_param], value: params[:remove_param_value]) + if params[:remove_param] == "date_range" + updated_params = current_params.except("date_gteq", "date_lteq") + elsif params[:remove_param_value] + key_to_remove = params[:remove_param] + value_to_remove = params[:remove_param_value] + updated_params = current_params.deep_dup + updated_params[key_to_remove] = updated_params[key_to_remove] - [ value_to_remove ] + else + updated_params = current_params.except(params[:remove_param]) + end session[ransack_session_key] = updated_params elsif params[:q] session[ransack_session_key] = params[:q] diff --git a/app/helpers/transactions_helper.rb b/app/helpers/transactions_helper.rb index 36098d12..d50ea4d1 100644 --- a/app/helpers/transactions_helper.rb +++ b/app/helpers/transactions_helper.rb @@ -1,2 +1,20 @@ module TransactionsHelper + def transaction_filters + [ + { name: "Account", partial: "account_filter", icon: "layers" }, + { name: "Date", partial: "date_filter", icon: "calendar" }, + { name: "Type", partial: "type_filter", icon: "shapes" }, + { name: "Amount", partial: "amount_filter", icon: "hash" }, + { name: "Category", partial: "category_filter", icon: "tag" }, + { name: "Merchant", partial: "merchant_filter", icon: "store" } + ] + end + + def transaction_filter_id(filter) + "txn-#{filter[:name].downcase}-filter" + end + + def transaction_filter_by_name(name) + transaction_filters.find { |filter| filter[:name] == name } + end end diff --git a/app/models/transaction.rb b/app/models/transaction.rb index 890736f3..b778cd08 100644 --- a/app/models/transaction.rb +++ b/app/models/transaction.rb @@ -23,6 +23,8 @@ class Transaction < ApplicationRecord def self.build_filter_list(params, family) filters = [] + date_filters = { gteq: nil, lteq: nil } + if params params.each do |key, value| next if value.blank? @@ -38,8 +40,16 @@ class Transaction < ApplicationRecord end when "category_name_or_account_name_or_name_cont" filters << { type: "search", value: value, original: { key: key, value: nil } } + when "date_gteq" + date_filters[:gteq] = value + when "date_lteq" + date_filters[:lteq] = value end end + + unless date_filters.values.compact.empty? + filters << { type: "date_range", value: date_filters, original: { key: "date_range", value: nil } } + end end filters diff --git a/app/views/transactions/_filter.html.erb b/app/views/transactions/_filter.html.erb index 3e22bbba..19405d8b 100644 --- a/app/views/transactions/_filter.html.erb +++ b/app/views/transactions/_filter.html.erb @@ -16,11 +16,25 @@ <%= lucide_icon "text", class: "w-5 h-5 text-gray-500" %>

<%= "\"#{filter[:value]}\"".truncate(20) %>

+ <% when "date_range" %> +
+ <%= lucide_icon "calendar", class: "w-5 h-5 text-gray-500" %> +

+ <% if filter[:value][:gteq] && filter[:value][:lteq] %> + <%= filter[:value][:gteq] %> → <%= filter[:value][:lteq] %> + <% elsif filter[:value][:gteq] %> + on or after <%= filter[:value][:gteq] %> + <% elsif filter[:value][:lteq] %> + on or before <%= filter[:value][:lteq] %> + <% end %> +

+
<% end %> <%= form_with url: search_transactions_path, html: { class: "flex items-center" } do |form| %> <%= form.hidden_field :remove_param, value: filter[:original][:key] %> <% if filter[:original][:value] %> <%= form.hidden_field :remove_param_value, value: filter[:original][:value] %> + <% else %> <% end %> <%= form.button type: "submit", class: "hover:text-gray-900" do %> <%= lucide_icon "x", class: "w-4 h-4 text-gray-500" %> diff --git a/app/views/transactions/_filters.html.erb b/app/views/transactions/_filters.html.erb index 5a7d2b7e..5349f2b0 100644 --- a/app/views/transactions/_filters.html.erb +++ b/app/views/transactions/_filters.html.erb @@ -1,7 +1,7 @@ <%# locals: (filters:) %>
<%= turbo_frame_tag "transactions_filters" do %> -
+
<% filters.each do |filter| %> <%= render partial: "transactions/filter", locals: { filter: filter } %> <% end %> diff --git a/app/views/transactions/_search_form.html.erb b/app/views/transactions/_search_form.html.erb index c595540c..7087c712 100644 --- a/app/views/transactions/_search_form.html.erb +++ b/app/views/transactions/_search_form.html.erb @@ -1,7 +1,7 @@ <%# locals: (q:) %>
<%= turbo_frame_tag "transactions_search_form" do %> - <%= search_form_for @q, url: search_transactions_path, html: { method: :post, data: { turbo_frame: "transactions_list", "controller": "auto-submit-form" } } do |form| %> + <%= search_form_for @q, url: search_transactions_path, html: { method: :post, data: { turbo_frame: "transactions_list" } } do |form| %>
<%= render partial: "transactions/search_form/search_filter", locals: { form: form } %> @@ -11,52 +11,40 @@ <%= lucide_icon("list-filter", class: "w-5 h-5 text-gray-500") %>

Filter

- <%# TODO: Refactor this for readability and maintainability %> -