diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 4818da9b..bbf54813 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -8,6 +8,7 @@ class DashboardController < ApplicationController return render 'pages/landing' unless signed_in? templates = current_account.templates.active.preload(:author).order(id: :desc) + templates = Templates.search(templates, params[:q]) @pagy, @templates = pagy(templates, items: 12) end diff --git a/app/controllers/templates_archived_controller.rb b/app/controllers/templates_archived_controller.rb index d515817c..a4fccc0b 100644 --- a/app/controllers/templates_archived_controller.rb +++ b/app/controllers/templates_archived_controller.rb @@ -3,6 +3,7 @@ class TemplatesArchivedController < ApplicationController def index templates = current_account.templates.where.not(deleted_at: nil).preload(:author).order(id: :desc) + templates = Templates.search(templates, params[:q]) @pagy, @templates = pagy(templates, items: 12) end diff --git a/app/controllers/templates_archived_submissions_controller.rb b/app/controllers/templates_archived_submissions_controller.rb index d777e2f1..2ea5938f 100644 --- a/app/controllers/templates_archived_submissions_controller.rb +++ b/app/controllers/templates_archived_submissions_controller.rb @@ -4,7 +4,10 @@ class TemplatesArchivedSubmissionsController < ApplicationController def show @template = current_account.templates.find(params[:template_id]) - @pagy, @submissions = pagy(@template.submissions.where.not(deleted_at: nil).preload(:submitters).order(id: :desc)) + submissions = @template.submissions.where.not(deleted_at: nil) + submissions = Submissions.search(submissions, params[:q]) + + @pagy, @submissions = pagy(submissions.preload(:submitters).order(id: :desc)) rescue ActiveRecord::RecordNotFound redirect_to root_path end diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index 93df873f..98fd7e99 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -7,6 +7,7 @@ class TemplatesController < ApplicationController @template = current_account.templates.find(params[:id]) submissions = @template.submissions submissions = submissions.active if @template.deleted_at.blank? + submissions = Submissions.search(submissions, params[:q]) @pagy, @submissions = pagy(submissions.preload(:submitters).order(id: :desc)) rescue ActiveRecord::RecordNotFound diff --git a/app/views/dashboard/index.html.erb b/app/views/dashboard/index.html.erb index 56499a90..c74730eb 100644 --- a/app/views/dashboard/index.html.erb +++ b/app/views/dashboard/index.html.erb @@ -1,8 +1,11 @@ <% if Docuseal.demo? %><%= render 'shared/demo_alert' %><% end %> -<% if @templates.any? %> +<% if @pagy.count > 0 || params[:q].present? %>

Templates

+ <% if params[:q].present? || @pagy.pages > 1 %> + <%= render 'shared/search_input' %> + <% end %> <%= render 'templates/upload_button' %> <%= link_to new_template_path, class: 'btn btn-primary text-base btn-md gap-2', data: { turbo_frame: :modal } do %> <%= svg_icon('plus', class: 'w-6 h-6 stroke-2') %> @@ -10,21 +13,29 @@ <% end %>
-
- <%= render partial: 'templates/template', collection: @templates %> -
- <% view_archived_html = capture do %> - <% if current_account.templates.where.not(deleted_at: nil).exists? %> -
- View Archived + <% if @pagy.count > 0 %> +
+ <%= render partial: 'templates/template', collection: @templates %> +
+ <% view_archived_html = capture do %> + <% if current_account.templates.where.not(deleted_at: nil).exists? %> +
+ View Archived +
+ <% end %> + <% end %> + <% if @pagy.pages > 1 %> + <%= render 'shared/pagination', pagy: @pagy, items_name: 'templates', left_additional_html: view_archived_html %> + <% else %> +
+ <%= view_archived_html %>
<% end %> - <% end %> - <% if @pagy.pages > 1 %> - <%= render 'shared/pagination', pagy: @pagy, items_name: 'templates', left_additional_html: view_archived_html %> - <% else %> -
- <%= view_archived_html %> + <% elsif params[:q].present? %> +
+
+ Templates not Found +
<% end %> <% else %> diff --git a/app/views/icons/_search.html.erb b/app/views/icons/_search.html.erb new file mode 100644 index 00000000..714fde89 --- /dev/null +++ b/app/views/icons/_search.html.erb @@ -0,0 +1,5 @@ + + + + + diff --git a/app/views/shared/_search_input.html.erb b/app/views/shared/_search_input.html.erb new file mode 100644 index 00000000..576a45b6 --- /dev/null +++ b/app/views/shared/_search_input.html.erb @@ -0,0 +1,11 @@ + diff --git a/app/views/templates/show.html.erb b/app/views/templates/show.html.erb index 77e25e32..91104a0d 100644 --- a/app/views/templates/show.html.erb +++ b/app/views/templates/show.html.erb @@ -3,6 +3,9 @@

Submissions

+ <% if params[:q].present? || @pagy.pages > 1 %> + <%= render 'shared/search_input' %> + <% end %> <%= link_to new_template_submissions_export_path(@template), class: 'order-3 md:order-1 btn btn-ghost text-base', data: { turbo_frame: 'modal' } do %> <%= svg_icon('download', class: 'w-6 h-6 stroke-2') %> Export @@ -12,7 +15,7 @@ <%= render 'shared/clipboard_copy', text: start_form_url(slug: @template.slug), class: 'base-button', icon_class: 'w-6 h-6 text-white', copy_title: 'Copy Share Link', copied_title: 'Copied to Clipboard', copy_title_md: 'Copy', copied_title_md: 'Copied' %> <% end %> - <% if !@pagy.count.zero? && !@template.deleted_at? %> + <% if (!@pagy.count.zero? || params[:q].present?) && !@template.deleted_at? %> <%= link_to new_template_submission_path(@template), class: 'order-1 btn btn-primary text-base', data: { turbo_frame: 'modal' } do %> <%= svg_icon('plus', class: 'w-6 h-6 stroke-2') %> Add @@ -44,7 +47,7 @@

There are no Submissions

- <% if @template.deleted_at.blank? %> + <% if @template.deleted_at.blank? && params[:q].blank? %>

Send an invitation to fill and complete the form

<%= link_to new_template_submission_path(@template), class: 'base-button mt-6', data: { turbo_frame: 'modal' } do %> diff --git a/app/views/templates_archived/index.html.erb b/app/views/templates_archived/index.html.erb index 0d173208..8e194c2d 100644 --- a/app/views/templates_archived/index.html.erb +++ b/app/views/templates_archived/index.html.erb @@ -6,8 +6,19 @@

Templates Archived

+ <% if params[:q].present? || @pagy.pages > 1 %> + <%= render 'shared/search_input' %> + <% end %>
-
- <%= render partial: 'templates/template', collection: @templates %> -
+<% if @pagy.count > 0 %> +
+ <%= render partial: 'templates/template', collection: @templates %> +
+<% elsif params[:q].present? %> +
+
+ Templates not Found +
+
+<% end %> <%= render 'shared/pagination', pagy: @pagy, items_name: 'templates' %> diff --git a/app/views/templates_archived_submissions/show.html.erb b/app/views/templates_archived_submissions/show.html.erb index 31bae929..24a4f9e8 100644 --- a/app/views/templates_archived_submissions/show.html.erb +++ b/app/views/templates_archived_submissions/show.html.erb @@ -8,13 +8,24 @@

Submissions Archived

+ <% if params[:q].present? || @pagy.pages > 1 %> + <%= render 'shared/search_input' %> + <% end %> <%= link_to new_template_submissions_export_path(@template), class: 'order-3 md:order-1 btn btn-ghost text-base', data: { turbo_frame: 'modal' } do %> <%= svg_icon('download', class: 'w-6 h-6 stroke-2') %> Export <% end %>
-
- <%= render partial: 'templates/submission', collection: @submissions %> -
+<% if @pagy.count > 0 %> +
+ <%= render partial: 'templates/submission', collection: @submissions %> +
+<% elsif params[:q].present? %> +
+
+ Submissions not Found +
+
+<% end %> <%= render 'shared/pagination', pagy: @pagy, items_name: 'submissions' %> diff --git a/lib/submissions.rb b/lib/submissions.rb index f1276466..1403aebc 100644 --- a/lib/submissions.rb +++ b/lib/submissions.rb @@ -5,6 +5,20 @@ module Submissions module_function + def search(submissions, keyword) + return submissions if keyword.blank? + + term = "%#{keyword.downcase}%" + + arel_table = Submitter.arel_table + + arel = arel_table[:email].lower.matches(term) + .or(arel_table[:phone].matches(term)) + .or(arel_table[:name].lower.matches(term)) + + submissions.joins(:submitters).where(arel).distinct + end + def update_template_fields!(submission) submission.template_fields = submission.template.fields submission.template_schema = submission.template.schema diff --git a/lib/templates.rb b/lib/templates.rb index e1503f5c..a93fd300 100644 --- a/lib/templates.rb +++ b/lib/templates.rb @@ -17,4 +17,10 @@ module Templates hash end + + def search(templates, keyword) + return templates if keyword.blank? + + templates.where(Template.arel_table[:name].lower.matches("%#{keyword.downcase}%")) + end end