diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a48146ee..dd463d4a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -115,4 +115,21 @@ class ApplicationController < ActionController::Base redirect_to request.url.gsub('.co/', '.com/'), allow_other_host: true, status: :moved_permanently end + + def date_range(key) + p = params.permit(key => %i[from to]).fetch(key, {}) + timezone = ActiveSupport::TimeZone[current_account.timezone] || Time.zone + start_date = timezone.parse(p[:from]) if p[:from].present? + end_date = timezone.parse(p[:to]) if p[:to].present? + + if start_date.present? && end_date.present? && start_date < end_date + start_date..end_date + elsif start_date.present? && end_date.present? && start_date == end_date + start_date.beginning_of_day..end_date.end_of_day + elsif start_date.present? + start_date.. + elsif end_date.present? + ..end_date + end + end end diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 5a578685..6b483df6 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -4,6 +4,7 @@ class TemplatesController < ApplicationController load_and_authorize_resource :template before_action :load_base_template, only: %i[new create] + helper_method :created_at_range, :completed_at_range def show submissions = @template.submissions.accessible_by(current_ability) @@ -14,12 +15,40 @@ class TemplatesController < ApplicationController submissions = submissions.pending if params[:status] == 'pending' submissions = submissions.completed if params[:status] == 'completed' + submissions = submissions.where(created_at: created_at_range) if created_at_range.present? + + if completed_at_range.present? + submissions = + Submission.from(submissions.completed + .joins(:submitters) + .select('submissions.*, max(completed_at) AS last_completed_at') + .group('submissions.id'), :submissions) + .where(last_completed_at: completed_at_range) + end + + if params[:author].present? + submissions = + submissions.joins(:created_by_user) + .where("concat_ws(' ', NULLIF(users.first_name, ''), NULLIF(last_name, '')) = ?", params[:author]) + end @pagy, @submissions = pagy(submissions.preload(submitters: :start_form_submission_events).order(id: :desc)) rescue ActiveRecord::RecordNotFound redirect_to root_path end + def filter + return unless params[:filter] == 'author' + + submissions = @template.submissions.accessible_by(current_ability) + submissions = submissions.active if @template.archived_at.blank? + @creator_names = submissions.includes(:created_by_user) + .filter_map(&:created_by_user) + .map(&:full_name) + .uniq + .sort + end + def new @template.name = "#{@base_template.name} (#{I18n.t('clone')})" if @base_template end @@ -147,4 +176,12 @@ class TemplatesController < ApplicationController @base_template = Template.accessible_by(current_ability).find_by(id: params[:base_template_id]) end + + def created_at_range + @created_at_range ||= date_range(:created_at) + end + + def completed_at_range + @completed_at_range ||= date_range(:completed_at) + end end diff --git a/app/javascript/application.js b/app/javascript/application.js index 03339e0c..15f31a72 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -30,6 +30,7 @@ import ToggleAttribute from './elements/toggle_attribute' import LinkedInput from './elements/linked_input' import CheckboxGroup from './elements/checkbox_group' import MaskedInput from './elements/masked_input' +import SetDateButton from './elements/set_date_button' import * as TurboInstantClick from './lib/turbo_instant_click' @@ -97,6 +98,7 @@ safeRegisterElement('toggle-attribute', ToggleAttribute) safeRegisterElement('linked-input', LinkedInput) safeRegisterElement('checkbox-group', CheckboxGroup) safeRegisterElement('masked-input', MaskedInput) +safeRegisterElement('set-date-button', SetDateButton) safeRegisterElement('template-builder', class extends HTMLElement { connectedCallback () { diff --git a/app/javascript/elements/set_date_button.js b/app/javascript/elements/set_date_button.js new file mode 100644 index 00000000..f61543d7 --- /dev/null +++ b/app/javascript/elements/set_date_button.js @@ -0,0 +1,17 @@ +export default class extends HTMLElement { + connectedCallback () { + this.dateFrom = this.dataset.fromValue + this.dateTo = this.dataset.toValue + this.dateFromInput = document.getElementById(this.dataset.fromId) + this.dateToInput = document.getElementById(this.dataset.toId) + + this.button.addEventListener('click', () => { + this.dateFromInput.value = this.dateFrom || '' + this.dateToInput.value = this.dateTo || '' + }) + } + + get button () { + return this.querySelector('button') + } +} diff --git a/app/views/icons/_calendar_check.html.erb b/app/views/icons/_calendar_check.html.erb new file mode 100644 index 00000000..a0a4ec57 --- /dev/null +++ b/app/views/icons/_calendar_check.html.erb @@ -0,0 +1,8 @@ + diff --git a/app/views/icons/_filter.html.erb b/app/views/icons/_filter.html.erb new file mode 100644 index 00000000..e742c062 --- /dev/null +++ b/app/views/icons/_filter.html.erb @@ -0,0 +1,4 @@ + diff --git a/app/views/icons/_x.html.erb b/app/views/icons/_x.html.erb new file mode 100644 index 00000000..be52086b --- /dev/null +++ b/app/views/icons/_x.html.erb @@ -0,0 +1,4 @@ + diff --git a/app/views/templates/filter.html.erb b/app/views/templates/filter.html.erb new file mode 100644 index 00000000..87d962f6 --- /dev/null +++ b/app/views/templates/filter.html.erb @@ -0,0 +1,94 @@ +<%= render 'shared/turbo_modal', title: t(params[:filter], default: '') do %> + <%= form_for @template, method: :get, data: { turbo_frame: :_top }, html: { autocomplete: :off } do |f| %> + <%= hidden_field_tag :status, params[:status] if params[:status].present? %> + <%= hidden_field_tag :q, params[:q] if params[:q].present? %> +
+
<%= t('there_are_no_submissions') %>
<% if @template.archived_at.blank? && params[:q].blank? %> -<%= t('send_an_invitation_to_fill_and_complete_the_form') %>
-<%= t('send_an_invitation_to_fill_and_complete_the_form') %>
+