diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 4047dda3..fe309a80 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -7,66 +7,18 @@ class DashboardController < ApplicationController before_action :maybe_render_landing before_action :maybe_redirect_mfa_setup - load_and_authorize_resource :template_folder, parent: false - load_and_authorize_resource :template, parent: false - - SHOW_TEMPLATES_FOLDERS_THRESHOLD = 9 - TEMPLATES_PER_PAGE = 12 - FOLDERS_PER_PAGE = 18 + skip_authorization_check def index - @template_folders = filter_template_folders(@template_folders) - - @pagy, @template_folders = pagy( - @template_folders, - items: FOLDERS_PER_PAGE, - page: @template_folders.count > SHOW_TEMPLATES_FOLDERS_THRESHOLD ? params[:page] : 1 - ) - - if @pagy.count > SHOW_TEMPLATES_FOLDERS_THRESHOLD - @templates = @templates.none + if cookies.permanent[:dashboard_view] == 'submissions' + SubmissionsDashboardController.dispatch(:index, request, response) else - @template_folders = @template_folders.reject { |e| e.name == TemplateFolder::DEFAULT_NAME } - @templates = filter_templates(@templates) - - items = - if @template_folders.size < 4 - TEMPLATES_PER_PAGE - else - (@template_folders.size < 7 ? 9 : 6) - end - - @pagy, @templates = pagy(@templates, items:) + TemplatesDashboardController.dispatch(:index, request, response) end end private - def filter_template_folders(template_folders) - rel = template_folders.joins(:active_templates) - .order(id: :desc) - .distinct - - TemplateFolders.search(rel, params[:q]) - end - - def filter_templates(templates) - rel = templates.active.preload(:author).order(id: :desc) - - if params[:q].blank? - if Docuseal.multitenant? && !current_account.testing? - rel = rel.where(folder_id: current_account.default_template_folder.id) - else - shared_template_ids = - TemplateSharing.where(account_id: [current_account.id, TemplateSharing::ALL_ID]).select(:template_id) - - rel = rel.where(folder_id: current_account.default_template_folder.id).or(rel.where(id: shared_template_ids)) - end - end - - Templates.search(rel, params[:q]) - end - def maybe_redirect_product_url return if !Docuseal.multitenant? || signed_in? diff --git a/app/controllers/submissions_archived_controller.rb b/app/controllers/submissions_archived_controller.rb new file mode 100644 index 00000000..d0e6d764 --- /dev/null +++ b/app/controllers/submissions_archived_controller.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class SubmissionsArchivedController < ApplicationController + load_and_authorize_resource :submission, parent: false + + def index + @submissions = @submissions.joins(:template) + @submissions = @submissions.where.not(archived_at: nil) + .or(@submissions.where.not(templates: { archived_at: nil })) + .preload(:created_by_user, template: :author) + @submissions = Submissions.search(@submissions, params[:q], search_template: true) + + @pagy, @submissions = pagy(@submissions.preload(:submitters).order(id: :desc)) + end +end diff --git a/app/controllers/submissions_dashboard_controller.rb b/app/controllers/submissions_dashboard_controller.rb new file mode 100644 index 00000000..57fb1e47 --- /dev/null +++ b/app/controllers/submissions_dashboard_controller.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class SubmissionsDashboardController < ApplicationController + load_and_authorize_resource :submission, parent: false + + def index + @submissions = @submissions.joins(:template) + + @submissions = @submissions.where(archived_at: nil) + .where(templates: { archived_at: nil }) + .preload(:created_by_user, template: :author) + + @submissions = Submissions.search(@submissions, params[:q], search_template: true) + + @submissions = @submissions.pending if params[:status] == 'pending' + @submissions = @submissions.completed if params[:status] == 'completed' + + @pagy, @submissions = pagy(@submissions.preload(:submitters).order(id: :desc)) + end +end diff --git a/app/controllers/templates_dashboard_controller.rb b/app/controllers/templates_dashboard_controller.rb new file mode 100644 index 00000000..12a6db8a --- /dev/null +++ b/app/controllers/templates_dashboard_controller.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +class TemplatesDashboardController < ApplicationController + load_and_authorize_resource :template_folder, parent: false + load_and_authorize_resource :template, parent: false + + SHOW_TEMPLATES_FOLDERS_THRESHOLD = 9 + TEMPLATES_PER_PAGE = 12 + FOLDERS_PER_PAGE = 18 + + def index + @template_folders = filter_template_folders(@template_folders) + + @pagy, @template_folders = pagy( + @template_folders, + items: FOLDERS_PER_PAGE, + page: @template_folders.count > SHOW_TEMPLATES_FOLDERS_THRESHOLD ? params[:page] : 1 + ) + + if @pagy.count > SHOW_TEMPLATES_FOLDERS_THRESHOLD + @templates = @templates.none + else + @template_folders = @template_folders.reject { |e| e.name == TemplateFolder::DEFAULT_NAME } + @templates = filter_templates(@templates) + + items = + if @template_folders.size < 4 + TEMPLATES_PER_PAGE + else + (@template_folders.size < 7 ? 9 : 6) + end + + @pagy, @templates = pagy(@templates, items:) + end + end + + private + + def filter_template_folders(template_folders) + rel = template_folders.joins(:active_templates) + .order(id: :desc) + .distinct + + TemplateFolders.search(rel, params[:q]) + end + + def filter_templates(templates) + rel = templates.active.preload(:author).order(id: :desc) + + if params[:q].blank? + if Docuseal.multitenant? && !current_account.testing? + rel = rel.where(folder_id: current_account.default_template_folder.id) + else + shared_template_ids = + TemplateSharing.where(account_id: [current_account.id, TemplateSharing::ALL_ID]).select(:template_id) + + rel = rel.where(folder_id: current_account.default_template_folder.id).or(rel.where(id: shared_template_ids)) + end + end + + Templates.search(rel, params[:q]) + end +end diff --git a/app/javascript/application.js b/app/javascript/application.js index fc2e95ce..5a209c92 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -6,6 +6,7 @@ import TemplateBuilder from './template_builder/builder' import ImportList from './template_builder/import_list' import ToggleVisible from './elements/toggle_visible' +import ToggleCookies from './elements/toggle_cookies' import DisableHidden from './elements/disable_hidden' import TurboModal from './elements/turbo_modal' import FileDropzone from './elements/file_dropzone' @@ -56,6 +57,7 @@ window.customElements.define('signature-form', SignatureForm) window.customElements.define('submit-form', SubmitForm) window.customElements.define('prompt-password', PromptPassword) window.customElements.define('emails-textarea', EmailsTextarea) +window.customElements.define('toggle-cookies', ToggleCookies) document.addEventListener('turbo:before-fetch-request', encodeMethodIntoRequestBody) document.addEventListener('turbo:submit-end', async (event) => { diff --git a/app/javascript/elements/toggle_cookies.js b/app/javascript/elements/toggle_cookies.js new file mode 100644 index 00000000..8d030cb3 --- /dev/null +++ b/app/javascript/elements/toggle_cookies.js @@ -0,0 +1,17 @@ +export default class extends HTMLElement { + connectedCallback () { + this.button.addEventListener('click', () => { + const expirationDate = new Date() + + expirationDate.setFullYear(expirationDate.getFullYear() + 10) + + const expires = expirationDate.toUTCString() + + document.cookie = this.dataset.key + '=' + this.dataset.value + '; expires=' + expires + '; path=/' + }) + } + + get button () { + return this.querySelector('button') + } +} diff --git a/app/views/dashboard/_toggle_view.html.erb b/app/views/dashboard/_toggle_view.html.erb new file mode 100644 index 00000000..1cc869fa --- /dev/null +++ b/app/views/dashboard/_toggle_view.html.erb @@ -0,0 +1,12 @@ +
diff --git a/app/views/dashboard/index.html.erb b/app/views/dashboard/index.html.erb deleted file mode 100644 index 4fa74712..00000000 --- a/app/views/dashboard/index.html.erb +++ /dev/null @@ -1,80 +0,0 @@ -<% if Docuseal.demo? %><%= render 'shared/demo_alert' %><% end %> -