From 7f6c0af0e817559957863e6ac84afb949f04888b Mon Sep 17 00:00:00 2001 From: Alex Turchyn Date: Fri, 15 Sep 2023 00:42:51 +0300 Subject: [PATCH] add archived template and submission pages --- .../templates_archived_controller.rb | 9 +++ ...mplates_archived_submissions_controller.rb | 11 ++++ app/controllers/templates_controller.rb | 6 +- .../templates_restore_controller.rb | 11 ++++ app/views/dashboard/index.html.erb | 45 +++++--------- app/views/icons/_rotate.html.erb | 4 ++ app/views/shared/_pagination.html.erb | 1 + app/views/templates/_submission.html.erb | 8 ++- app/views/templates/_template.html.erb | 38 ++++++++++++ app/views/templates/_title.html.erb | 28 +++++++++ app/views/templates/show.html.erb | 58 +++++++++---------- app/views/templates_archived/index.html.erb | 13 +++++ .../show.html.erb | 20 +++++++ config/routes.rb | 3 + spec/system/template_spec.rb | 1 - 15 files changed, 190 insertions(+), 66 deletions(-) create mode 100644 app/controllers/templates_archived_controller.rb create mode 100644 app/controllers/templates_archived_submissions_controller.rb create mode 100644 app/controllers/templates_restore_controller.rb create mode 100644 app/views/icons/_rotate.html.erb create mode 100644 app/views/templates/_template.html.erb create mode 100644 app/views/templates/_title.html.erb create mode 100644 app/views/templates_archived/index.html.erb create mode 100644 app/views/templates_archived_submissions/show.html.erb diff --git a/app/controllers/templates_archived_controller.rb b/app/controllers/templates_archived_controller.rb new file mode 100644 index 00000000..d515817c --- /dev/null +++ b/app/controllers/templates_archived_controller.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class TemplatesArchivedController < ApplicationController + def index + templates = current_account.templates.where.not(deleted_at: nil).preload(:author).order(id: :desc) + + @pagy, @templates = pagy(templates, items: 12) + end +end diff --git a/app/controllers/templates_archived_submissions_controller.rb b/app/controllers/templates_archived_submissions_controller.rb new file mode 100644 index 00000000..d777e2f1 --- /dev/null +++ b/app/controllers/templates_archived_submissions_controller.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +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)) + rescue ActiveRecord::RecordNotFound + redirect_to root_path + end +end diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index e4043bec..93df873f 100644 --- a/app/controllers/templates_controller.rb +++ b/app/controllers/templates_controller.rb @@ -4,9 +4,11 @@ class TemplatesController < ApplicationController before_action :load_base_template, only: %i[new create] def show - @template = current_account.templates.active.find(params[:id]) + @template = current_account.templates.find(params[:id]) + submissions = @template.submissions + submissions = submissions.active if @template.deleted_at.blank? - @pagy, @submissions = pagy(@template.submissions.active.preload(:submitters).order(id: :desc)) + @pagy, @submissions = pagy(submissions.preload(:submitters).order(id: :desc)) rescue ActiveRecord::RecordNotFound redirect_to root_path end diff --git a/app/controllers/templates_restore_controller.rb b/app/controllers/templates_restore_controller.rb new file mode 100644 index 00000000..d691bed2 --- /dev/null +++ b/app/controllers/templates_restore_controller.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class TemplatesRestoreController < ApplicationController + def create + template = current_account.templates.find(params[:template_id]) + + template.update!(deleted_at: nil) + + redirect_to template_path(template), notice: 'Template has been unarchived' + end +end diff --git a/app/views/dashboard/index.html.erb b/app/views/dashboard/index.html.erb index b47a8fbb..56499a90 100644 --- a/app/views/dashboard/index.html.erb +++ b/app/views/dashboard/index.html.erb @@ -11,39 +11,22 @@
- <% @templates.each do |template| %> -
- -
- <%= template.name %> -
-
-

- <%= svg_icon('user', class: 'w-4 h-4') %> - <%= template.author.full_name.presence || template.author.email %> -

-

- <%= svg_icon('calendar', class: 'w-4 h-4') %> - <%= l(template.created_at.in_time_zone(current_account.timezone), format: :short, locale: current_account.locale) %> -

-
-
- + <%= render partial: 'templates/template', collection: @templates %> +
+ <% view_archived_html = capture do %> + <% if current_account.templates.where.not(deleted_at: nil).exists? %> +
+ View Archived
<% end %> -
- <%= render 'shared/pagination', pagy: @pagy, items_name: 'templates' %> + <% end %> + <% if @pagy.pages > 1 %> + <%= render 'shared/pagination', pagy: @pagy, items_name: 'templates', left_additional_html: view_archived_html %> + <% else %> +
+ <%= view_archived_html %> +
+ <% end %> <% else %>
diff --git a/app/views/icons/_rotate.html.erb b/app/views/icons/_rotate.html.erb new file mode 100644 index 00000000..eff3a99f --- /dev/null +++ b/app/views/icons/_rotate.html.erb @@ -0,0 +1,4 @@ + + + + diff --git a/app/views/shared/_pagination.html.erb b/app/views/shared/_pagination.html.erb index d3ceb6dc..1a2771ae 100644 --- a/app/views/shared/_pagination.html.erb +++ b/app/views/shared/_pagination.html.erb @@ -3,6 +3,7 @@
<% if @pagy.prev %> diff --git a/app/views/templates/_submission.html.erb b/app/views/templates/_submission.html.erb index 16dc6015..e9f402b5 100644 --- a/app/views/templates/_submission.html.erb +++ b/app/views/templates/_submission.html.erb @@ -38,7 +38,9 @@ <%= render 'shared/clipboard_copy', text: submit_form_url(slug: submitter.slug), class: 'btn btn-sm btn-neutral text-white md:w-36 flex', icon_class: 'w-6 h-6 text-white', copy_title: 'Copy Link', copy_title_md: 'Copy', copied_title_md: 'Copied' %> <% end %> View - <%= button_to button_title(title: nil, disabled_with: 'Remov', icon: svg_icon('trash', class: 'w-6 h-6')), submission_path(submission), class: 'btn btn-outline btn-sm', title: 'Delete', method: :delete, data: { turbo_confirm: 'Are you sure?' }, onclick: 'event.stopPropagation()' %> + <% unless submission.deleted_at? %> + <%= button_to button_title(title: nil, disabled_with: 'Remov', icon: svg_icon('trash', class: 'w-6 h-6')), submission_path(submission), class: 'btn btn-outline btn-sm', title: 'Delete', method: :delete, data: { turbo_confirm: 'Are you sure?' }, onclick: 'event.stopPropagation()' %> + <% end %>
<% else %>
@@ -110,7 +112,9 @@ <% end %> View - <%= button_to button_title(title: nil, disabled_with: 'Remov', icon: svg_icon('trash', class: 'w-6 h-6')), submission_path(submission), class: 'btn btn-outline btn-sm', title: 'Delete', method: :delete, data: { turbo_confirm: 'Are you sure?' }, onclick: 'event.stopPropagation()' %> + <% unless submission.deleted_at? %> + <%= button_to button_title(title: nil, disabled_with: 'Remov', icon: svg_icon('trash', class: 'w-6 h-6')), submission_path(submission), class: 'btn btn-outline btn-sm', title: 'Delete', method: :delete, data: { turbo_confirm: 'Are you sure?' }, onclick: 'event.stopPropagation()' %> + <% end %>
<% end %> diff --git a/app/views/templates/_template.html.erb b/app/views/templates/_template.html.erb new file mode 100644 index 00000000..1665f630 --- /dev/null +++ b/app/views/templates/_template.html.erb @@ -0,0 +1,38 @@ +
+ +
+ <%= template.name %> +
+
+

+ <%= svg_icon('user', class: 'w-4 h-4') %> + <%= template.author.full_name.presence || template.author.email %> +

+

+ <%= svg_icon('calendar', class: 'w-4 h-4') %> + <%= l(template.created_at.in_time_zone(current_account.timezone), format: :short, locale: current_account.locale) %> +

+
+
+ +
diff --git a/app/views/templates/_title.html.erb b/app/views/templates/_title.html.erb new file mode 100644 index 00000000..286d6bc9 --- /dev/null +++ b/app/views/templates/_title.html.erb @@ -0,0 +1,28 @@ +
+

+ <%= template.name %> + <% if template.deleted_at? %> + Archived + <% end %> +

+
+ <% unless template.deleted_at? %> + <%= button_to button_title(title: 'Remove', disabled_with: 'Removing', icon: svg_icon('trash', class: 'w-6 h-6')), template_path(template), class: 'btn btn-outline btn-sm', method: :delete, data: { turbo_confirm: 'Are you sure?' } %> + <% end %> + <%= link_to new_template_path(base_template_id: template.id), class: 'btn btn-outline btn-sm', data: { turbo_frame: :modal } do %> + <%= svg_icon('copy', class: 'w-6 h-6') %> + Clone + <% end %> + <% unless template.deleted_at? %> + <%= link_to edit_template_path(template), class: 'btn btn-outline btn-sm' do %> + + <%= svg_icon('pencil', class: 'w-6 h-6') %> + Edit + + <% end %> + <% end %> + <% if template.deleted_at? %> + <%= button_to button_title(title: 'Restore', disabled_with: 'Restoring', icon: svg_icon('rotate', class: 'w-6 h-6')), template_restore_index_path(template), class: 'btn btn-outline btn-sm' %> + <% end %> +
+
diff --git a/app/views/templates/show.html.erb b/app/views/templates/show.html.erb index c7d9dd2e..77e25e32 100644 --- a/app/views/templates/show.html.erb +++ b/app/views/templates/show.html.erb @@ -1,21 +1,4 @@ -
-

- <%= @template.name %> -

-
- <%= button_to button_title(title: 'Remove', disabled_with: 'Removing', icon: svg_icon('trash', class: 'w-6 h-6')), template_path(@template), class: 'btn btn-outline btn-sm', method: :delete, data: { turbo_confirm: 'Are you sure?' } %> - <%= link_to new_template_path(base_template_id: @template.id), class: 'btn btn-outline btn-sm', data: { turbo_frame: :modal } do %> - <%= svg_icon('copy', class: 'w-6 h-6') %> - Clone - <% end %> - <%= link_to edit_template_path(@template), class: 'btn btn-outline btn-sm' do %> - - <%= svg_icon('pencil', class: 'w-6 h-6') %> - Edit - - <% end %> -
-
+<%= render 'title', template: @template %> <% if !@pagy.count.zero? || @template.submitters.to_a.size == 1 %>

Submissions

@@ -24,12 +7,12 @@ <%= svg_icon('download', class: 'w-6 h-6 stroke-2') %> Export <% end %> - <% if @template.submitters.to_a.size == 1 %> + <% if @template.submitters.to_a.size == 1 && !@template.deleted_at? %> <%= 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 %> - <% unless @pagy.count.zero? %> + <% if !@pagy.count.zero? && !@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 @@ -42,21 +25,36 @@
<%= render partial: 'submission', collection: @submissions %>
- <%= render 'shared/pagination', pagy: @pagy, items_name: 'submissions' %> + <% view_archived_html = capture do %> + <% if @template.submissions.where.not(deleted_at: nil).exists? && !@template.deleted_at? %> + + <% end %> + <% end %> + <% if @pagy.pages > 1 %> + <%= render 'shared/pagination', pagy: @pagy, items_name: 'submissions', left_additional_html: view_archived_html %> + <% else %> +
+ <%= view_archived_html %> +
+ <% end %> <% else %>

There are no Submissions

-

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 %> - <%= svg_icon('plus', class: 'w-6 h-6 stroke-2') %> - Add Recipients - <% end %> - <%= link_to start_form_url(slug: @template.slug), class: 'white-button mt-6', target: '_blank', rel: 'noopener' do %> - <%= svg_icon('writing', class: 'w-6 h-6') %> - Submit it Yourself + <% if @template.deleted_at.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 %> + <%= svg_icon('plus', class: 'w-6 h-6 stroke-2') %> + Add Recipients + <% end %> + <%= link_to start_form_url(slug: @template.slug), class: 'white-button mt-6', target: '_blank', rel: 'noopener' do %> + <%= svg_icon('writing', class: 'w-6 h-6') %> + Submit it Yourself + <% end %> <% end %>
diff --git a/app/views/templates_archived/index.html.erb b/app/views/templates_archived/index.html.erb new file mode 100644 index 00000000..0d173208 --- /dev/null +++ b/app/views/templates_archived/index.html.erb @@ -0,0 +1,13 @@ +
+ <%= link_to root_path(@template) do %> + ← + Back to Active + <% end %> +
+
+

Templates Archived

+
+
+ <%= render partial: 'templates/template', collection: @templates %> +
+<%= 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 new file mode 100644 index 00000000..31bae929 --- /dev/null +++ b/app/views/templates_archived_submissions/show.html.erb @@ -0,0 +1,20 @@ +<%= render 'templates/title', template: @template %> +
+ <%= link_to template_path(@template) do %> + ← + Back to Active + <% end %> +
+
+

Submissions Archived

+
+ <%= 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 %> +
+<%= render 'shared/pagination', pagy: @pagy, items_name: 'submissions' %> diff --git a/config/routes.rb b/config/routes.rb index 38b3c067..8535d769 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -48,7 +48,10 @@ Rails.application.routes.draw do resources :submissions, only: %i[show destroy] resources :console_redirect, only: %i[index] resource :templates_upload, only: %i[create] + resources :templates_archived, only: %i[index], path: 'archived' resources :templates, only: %i[new create edit show destroy] do + resources :restore, only: %i[create], controller: 'templates_restore' + resource :archived, only: %i[show], controller: 'templates_archived_submissions' resources :submissions, only: %i[new create] resources :submissions_export, only: %i[index new] end diff --git a/spec/system/template_spec.rb b/spec/system/template_spec.rb index d95a1632..dd092a43 100644 --- a/spec/system/template_spec.rb +++ b/spec/system/template_spec.rb @@ -51,7 +51,6 @@ RSpec.describe 'Template' do end.to change { Template.active.count }.by(-1) expect(page).to have_content('Template has been archived') - expect(page).to have_current_path(root_path, ignore_query: true) end it 'edits a template' do