From 3b03e61ae38fa2ec1ea14c8cdb993703c826c82d Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Sun, 3 Dec 2023 00:06:03 +0200 Subject: [PATCH] use template links with 2+ submitters --- app/controllers/start_form_controller.rb | 31 +++++++++++++++++------- app/views/start_form/completed.html.erb | 8 +++--- app/views/start_form/show.html.erb | 3 ++- app/views/templates/_title.html.erb | 2 +- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/app/controllers/start_form_controller.rb b/app/controllers/start_form_controller.rb index e4e493bb..938ca2ab 100644 --- a/app/controllers/start_form_controller.rb +++ b/app/controllers/start_form_controller.rb @@ -14,22 +14,20 @@ class StartFormController < ApplicationController def update @submitter = Submitter.where(submission: @template.submissions.where(deleted_at: nil)) + .order(id: :desc) .then { |rel| params[:resubmit].present? ? rel.where(completed_at: nil) : rel } .find_or_initialize_by(**submitter_params.compact_blank) if @submitter.completed_at? redirect_to start_form_completed_path(@template.slug, email: submitter_params[:email]) else - @submitter.assign_attributes( - uuid: @template.submitters.first['uuid'], - ip: request.remote_ip, - ua: request.user_agent, - preferences: { 'send_email' => true } - ) + if @template.submitters.to_a.size > 1 && @submitter.new_record? + @error_message = 'Not found' - @submitter.submission ||= Submission.new(template: @template, - template_submitters: @template.submitters, - source: :link) + return render :show + end + + assign_submission_attributes(@submitter, @template) if @submitter.new_record? if @submitter.save redirect_to submit_form_path(@submitter.slug) @@ -47,6 +45,21 @@ class StartFormController < ApplicationController private + def assign_submission_attributes(submitter, template) + submitter.assign_attributes( + uuid: template.submitters.first['uuid'], + ip: request.remote_ip, + ua: request.user_agent, + preferences: { 'send_email' => true } + ) + + submitter.submission ||= Submission.new(template:, + template_submitters: template.submitters, + source: :link) + + submitter + end + def submitter_params params.require(:submitter).permit(:email, :phone, :name).tap do |attrs| attrs[:email] = Submissions.normalize_email(attrs[:email]) diff --git a/app/views/start_form/completed.html.erb b/app/views/start_form/completed.html.erb index 90537c20..7259521c 100644 --- a/app/views/start_form/completed.html.erb +++ b/app/views/start_form/completed.html.erb @@ -24,9 +24,11 @@ <%= button_to button_title(title: 'Send copy to Email', disabled_with: 'Sending', icon: svg_icon('mail_forward', class: 'w-6 h-6')), send_submission_email_index_path, params: { submitter_slug: @submitter.slug }, form: { onsubmit: 'event.submitter.disabled = true' }, class: 'base-button w-full' %> <% end %> -
- <%= button_to button_title(title: 'Resubmit', disabled_with: 'Resubmit', icon: svg_icon('reload', class: 'w-6 h-6')), start_form_path(@template.slug), params: { submitter: { email: params[:email] }, resubmit: true }, method: :put, form: { onsubmit: 'event.submitter.disabled = true' }, class: 'white-button w-full' %> -
+ <% if @template.submitters.to_a.size == 1 %> +
+ <%= button_to button_title(title: 'Resubmit', disabled_with: 'Resubmit', icon: svg_icon('reload', class: 'w-6 h-6')), start_form_path(@template.slug), params: { submitter: { email: params[:email] }, resubmit: true }, method: :put, form: { onsubmit: 'event.submitter.disabled = true' }, class: 'white-button w-full' %> +
+ <% end %> <%= render 'shared/attribution', link_path: '/start' %> diff --git a/app/views/start_form/show.html.erb b/app/views/start_form/show.html.erb index c6b4037b..8bdbf226 100644 --- a/app/views/start_form/show.html.erb +++ b/app/views/start_form/show.html.erb @@ -27,7 +27,8 @@ <%= form_for @submitter, url: start_form_path(@template.slug), data: { turbo_frame: :_top }, method: :put, html: { class: 'space-y-4', onsubmit: 'event.submitter.disabled = true' } do |f| %>
<%= f.label :email, class: 'label' %> - <%= f.email_field :email, value: current_user&.email || params[:email], required: true, class: 'base-input', placeholder: 'Provide your email to start' %> + <%= f.email_field :email, value: current_user&.email || params[:email] || @submitter.email, required: true, class: 'base-input', placeholder: 'Provide your email to start' %> + <%= @error_message %>
<%= f.button button_title(title: 'Start', disabled_with: 'Starting'), class: 'base-button' %> diff --git a/app/views/templates/_title.html.erb b/app/views/templates/_title.html.erb index 2effd7dd..6026dfab 100644 --- a/app/views/templates/_title.html.erb +++ b/app/views/templates/_title.html.erb @@ -23,7 +23,7 @@
- <% if template.submitters.to_a.size == 1 && !template.deleted_at? %> + <% if !template.deleted_at? %> <%= render 'shared/clipboard_copy', text: start_form_url(slug: @template.slug), class: 'btn btn-sm btn-neutral text-white', icon_class: 'w-6 h-6 text-white', copy_title: 'Copy Link', copied_title: 'Copied', copy_title_md: 'Link', copied_title_md: 'Copied' %> <% end %> <% if !template.deleted_at? && can?(:destroy, template) %>