From d0c7d449d5715bdda4a536815b09d6c5c68ad4bd Mon Sep 17 00:00:00 2001 From: Alex Turchyn Date: Thu, 29 Jun 2023 02:04:17 +0300 Subject: [PATCH] style template show page --- app/controllers/application_controller.rb | 4 + app/controllers/submissions_controller.rb | 4 +- app/controllers/templates_controller.rb | 6 +- app/javascript/elements/clipboard_copy.js | 153 +------------ app/models/submission.rb | 16 +- app/views/dashboard/index.html.erb | 12 +- .../devise/shared/_error_messages.html.erb | 4 +- app/views/esign_settings/index.html.erb | 2 +- app/views/icons/_link.html.erb | 6 + app/views/icons/_user_number.html.erb | 6 + app/views/pages/landing.html.erb | 2 +- app/views/profile/index.html.erb | 4 +- app/views/shared/_clipboard_copy.html.erb | 17 ++ app/views/shared/_navbar.html.erb | 2 +- app/views/shared/_no_data_banner.html.erb | 2 +- app/views/start_form/show.html.erb | 18 +- .../storage_settings/_disk_form.html.erb | 2 +- app/views/submissions/new.html.erb | 4 +- app/views/submissions/show.html.erb | 2 +- app/views/submit_form/completed.html.erb | 2 +- app/views/templates/show.html.erb | 204 ++++++++++-------- app/views/users/index.html.erb | 2 +- .../20230519144036_create_submissions.rb | 1 + db/schema.rb | 3 + .../generate_result_attachments.rb | 21 +- tailwind.form.config.js | 1 + 26 files changed, 213 insertions(+), 287 deletions(-) create mode 100644 app/views/icons/_link.html.erb create mode 100644 app/views/icons/_user_number.html.erb create mode 100644 app/views/shared/_clipboard_copy.html.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7118ee77..c6f634d6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,6 +11,10 @@ class ApplicationController < ActionController::Base :current_account, :svg_icon + rescue_from Pagy::OverflowError do + redirect_to request.path + end + def default_url_options Docuseal.default_url_options end diff --git a/app/controllers/submissions_controller.rb b/app/controllers/submissions_controller.rb index a337b3de..f591d6b5 100644 --- a/app/controllers/submissions_controller.rb +++ b/app/controllers/submissions_controller.rb @@ -47,7 +47,7 @@ class SubmissionsController < ApplicationController emails = params[:emails].to_s.scan(User::EMAIL_REGEXP) emails.map do |email| - submission = @template.submissions.new + submission = @template.submissions.new(created_by_user: current_user) submission.submitters.new(email:, uuid: @template.submitters.first['uuid'], sent_at: params[:send_email] == '1' ? Time.current : nil) @@ -57,7 +57,7 @@ class SubmissionsController < ApplicationController def create_submissions_from_submitters submissions_params[:submission].to_h.map do |_, attrs| - submission = @template.submissions.new + submission = @template.submissions.new(created_by_user: current_user) attrs[:submitters].each do |submitter_attrs| submission.submitters.new(**submitter_attrs, sent_at: params[:send_email] == '1' ? Time.current : nil) diff --git a/app/controllers/templates_controller.rb b/app/controllers/templates_controller.rb index f8735827..88d1ae78 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.find(params[:id]) + @template = current_account.templates.active.find(params[:id]) - @pagy, @submissions = pagy(@template.submissions.active) + @pagy, @submissions = pagy(@template.submissions.active.preload(:submitters).order(id: :desc)) + rescue ActiveRecord::RecordNotFound + redirect_to root_path end def new diff --git a/app/javascript/elements/clipboard_copy.js b/app/javascript/elements/clipboard_copy.js index 8c248425..d0b83a12 100644 --- a/app/javascript/elements/clipboard_copy.js +++ b/app/javascript/elements/clipboard_copy.js @@ -1,152 +1,15 @@ -// Source: https://github.com/github/clipboard-copy-element -// License: MIT export default class extends HTMLElement { - constructor () { - super() - this.addEventListener('click', clicked) - this.addEventListener('focus', focused) - this.addEventListener('blur', blurred) - } - connectedCallback () { - if (!this.hasAttribute('tabindex')) { - this.setAttribute('tabindex', '0') - } - - if (!this.hasAttribute('role')) { - this.setAttribute('role', 'button') - } - } - - get value () { - return this.getAttribute('value') || '' - } - - set value (text) { - this.setAttribute('value', text) - } -} - -function createNode (text) { - const node = document.createElement('pre') - node.style.width = '1px' - node.style.height = '1px' - node.style.position = 'fixed' - node.style.top = '5px' - node.textContent = text - return node -} - -function copyNode (node) { - if ('clipboard' in navigator) { - return navigator.clipboard.writeText(node.textContent || '') - } - - const selection = getSelection() - if (selection == null) { - return Promise.reject(new Error()) - } - - selection.removeAllRanges() - - const range = document.createRange() - range.selectNodeContents(node) - selection.addRange(range) - - document.execCommand('copy') - selection.removeAllRanges() - return Promise.resolve() -} - -function copyText (text) { - if ('clipboard' in navigator) { - return navigator.clipboard.writeText(text) - } - - const body = document.body - if (!body) { - return Promise.reject(new Error()) - } + this.addEventListener('click', (e) => { + e.stopPropagation() - const node = createNode(text) - body.appendChild(node) - copyNode(node) - body.removeChild(node) - return Promise.resolve() -} - -function copyTarget (content) { - if ( - content instanceof HTMLInputElement || - content instanceof HTMLTextAreaElement - ) { - return copyText(content.value) - } else if ( - content instanceof HTMLAnchorElement && - content.hasAttribute('href') - ) { - return copyText(content.href) - } else { - return copyNode(content) - } -} - -async function copy (button) { - const id = button.getAttribute('for') - const text = button.getAttribute('value') - - function trigger () { - button.dispatchEvent(new CustomEvent('clipboard-copy', { bubbles: true })) + navigator.clipboard.writeText(this.dataset.text || this.innerText.trim()) + }) } - function toggleActiveIcon () { - if (button.classList.contains('swap')) { - button.classList.toggle('swap-active') - } + disconnectedCallback () { + this.querySelectorAll('input').forEach((e) => { + e.checked = false + }) } - - if (text) { - await copyText(text) - trigger() - toggleActiveIcon() - } else if (id) { - const root = 'getRootNode' in Element.prototype ? button.getRootNode() : button.ownerDocument - - if (!(root instanceof Document || ('ShadowRoot' in window && root instanceof ShadowRoot))) return - - const node = root.getElementById(id) - - if (node) { - await copyTarget(node) - trigger() - toggleActiveIcon() - } - } -} - -function clicked (event) { - const button = event.currentTarget - - if (button instanceof HTMLElement) { - copy(button) - } -} - -function keydown (event) { - if (event.key === ' ' || event.key === 'Enter') { - const button = event.currentTarget - - if (button instanceof HTMLElement) { - event.preventDefault() - copy(button) - } - } -} - -function focused (event) { - event.currentTarget.addEventListener('keydown', keydown) -} - -function blurred (event) { - event.currentTarget.removeEventListener('keydown', keydown) } diff --git a/app/models/submission.rb b/app/models/submission.rb index e6bde570..04579f08 100644 --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -4,22 +4,26 @@ # # Table name: submissions # -# id :bigint not null, primary key -# deleted_at :datetime -# created_at :datetime not null -# updated_at :datetime not null -# template_id :bigint not null +# id :bigint not null, primary key +# deleted_at :datetime +# created_at :datetime not null +# updated_at :datetime not null +# created_by_user_id :bigint +# template_id :bigint not null # # Indexes # -# index_submissions_on_template_id (template_id) +# index_submissions_on_created_by_user_id (created_by_user_id) +# index_submissions_on_template_id (template_id) # # Foreign Keys # +# fk_rails_... (created_by_user_id => users.id) # fk_rails_... (template_id => templates.id) # class Submission < ApplicationRecord belongs_to :template + belongs_to :created_by_user, class_name: 'User', optional: true has_many :submitters, dependent: :destroy diff --git a/app/views/dashboard/index.html.erb b/app/views/dashboard/index.html.erb index 2b56d230..f396001e 100644 --- a/app/views/dashboard/index.html.erb +++ b/app/views/dashboard/index.html.erb @@ -1,8 +1,8 @@ <% if @templates.any? %> -
+

Templates

- <%= link_to new_template_path, class: 'btn btn-primary btn-md gap-2', data: { turbo_frame: :modal } do %> - <%= svg_icon('plus', class: 'w-6 h-6') %> + <%= 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') %> <% end %>
@@ -25,13 +25,13 @@