diff --git a/app/controllers/start_form_controller.rb b/app/controllers/start_form_controller.rb index fb2a9786..03944f26 100644 --- a/app/controllers/start_form_controller.rb +++ b/app/controllers/start_form_controller.rb @@ -33,7 +33,7 @@ class StartFormController < ApplicationController @submitter = find_or_initialize_submitter(@template, submitter_params) if @submitter.completed_at? - redirect_to start_form_completed_path(@template.slug, email: submitter_params[:email]) + redirect_to start_form_completed_path(@template.slug, submitter_params.compact_blank) else if filter_undefined_submitters(@template).size > 1 && @submitter.new_record? @error_message = multiple_submitters_error_message @@ -49,7 +49,7 @@ class StartFormController < ApplicationController @submitter.assign_attributes(ip: request.remote_ip, ua: request.user_agent) end - if @submitter.save + if @submitter.errors.blank? && @submitter.save if is_new_record enqueue_submission_create_webhooks(@submitter) @@ -71,9 +71,20 @@ class StartFormController < ApplicationController def completed return redirect_to start_form_path(@template.slug) if !@template.shared_link? || @template.archived_at? + submitter_params = params.permit(:name, :email, :phone).tap do |attrs| + attrs[:email] = Submissions.normalize_email(attrs[:email]) + end + + required_fields = @template.preferences.fetch('link_form_fields', ['email']) + + required_params = required_fields.index_with { |key| submitter_params[key] } + + raise ActionController::RoutingError, I18n.t('not_found') if required_params.any? { |_, v| v.blank? } || + required_params.except('name').compact_blank.blank? + @submitter = Submitter.where(submission: @template.submissions) .where.not(completed_at: nil) - .find_by!(email: params[:email]) + .find_by!(required_params) end private @@ -104,7 +115,16 @@ class StartFormController < ApplicationController end def find_or_initialize_submitter(template, submitter_params) - Submitter + required_fields = template.preferences.fetch('link_form_fields', ['email']) + + required_params = required_fields.index_with { |key| submitter_params[key] } + + find_params = required_params.except('name') + + submitter = Submitter.new if find_params.compact_blank.blank? + + submitter ||= + Submitter .where(submission: template.submissions.where(expire_at: Time.current..) .or(template.submissions.where(expire_at: nil)).where(archived_at: nil)) .order(id: :desc) @@ -112,7 +132,15 @@ class StartFormController < ApplicationController .where(external_id: nil) .where(ip: [nil, request.remote_ip]) .then { |rel| params[:resubmit].present? || params[:selfsign].present? ? rel.where(completed_at: nil) : rel } - .find_or_initialize_by(email: submitter_params[:email], **submitter_params.compact_blank) + .find_or_initialize_by(find_params) + + submitter.name = required_params['name'] if submitter.new_record? + + required_params.each do |key, value| + submitter.errors.add(key.to_sym, :blank) if value.blank? + end + + submitter end def assign_submission_attributes(submitter, template) diff --git a/app/controllers/templates_preferences_controller.rb b/app/controllers/templates_preferences_controller.rb index e2ec9ee3..eae2fa9f 100644 --- a/app/controllers/templates_preferences_controller.rb +++ b/app/controllers/templates_preferences_controller.rb @@ -31,7 +31,7 @@ class TemplatesPreferencesController < ApplicationController completed_notification_email_subject completed_notification_email_body completed_notification_email_enabled completed_notification_email_attach_audit] + [completed_message: %i[title body], - submitters: [%i[uuid request_email_subject request_email_body]]] + submitters: [%i[uuid request_email_subject request_email_body]], link_form_fields: []] ).tap do |attrs| attrs[:preferences].delete(:submitters) if params[:request_email_per_submitter] != '1' diff --git a/app/javascript/application.js b/app/javascript/application.js index f951b8fe..6ff4b401 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -35,6 +35,7 @@ import SetDateButton from './elements/set_date_button' import IndeterminateCheckbox from './elements/indeterminate_checkbox' import AppTour from './elements/app_tour' import DashboardDropzone from './elements/dashboard_dropzone' +import RequiredCheckboxGroup from './elements/required_checkbox_group' import * as TurboInstantClick from './lib/turbo_instant_click' @@ -105,6 +106,7 @@ safeRegisterElement('indeterminate-checkbox', IndeterminateCheckbox) safeRegisterElement('app-tour', AppTour) safeRegisterElement('dashboard-dropzone', DashboardDropzone) safeRegisterElement('check-on-click', CheckOnClick) +safeRegisterElement('required-checkbox-group', RequiredCheckboxGroup) safeRegisterElement('template-builder', class extends HTMLElement { connectedCallback () { diff --git a/app/javascript/elements/required_checkbox_group.js b/app/javascript/elements/required_checkbox_group.js new file mode 100644 index 00000000..e4dc13d7 --- /dev/null +++ b/app/javascript/elements/required_checkbox_group.js @@ -0,0 +1,17 @@ +export default class extends HTMLElement { + connectedCallback () { + this.querySelectorAll('input[type="checkbox"]').forEach(checkbox => { + checkbox.addEventListener('change', this.handleChange) + }) + } + + handleChange = () => { + if (this.checkedCount !== 0) { + this.closest('form')?.requestSubmit() + } + } + + get checkedCount () { + return this.querySelectorAll('input[type="checkbox"]:checked').length + } +} diff --git a/app/views/start_form/completed.html.erb b/app/views/start_form/completed.html.erb index 003005d8..c21b3d45 100644 --- a/app/views/start_form/completed.html.erb +++ b/app/views/start_form/completed.html.erb @@ -28,7 +28,7 @@ <% end %> <% if Templates.filter_undefined_submitters(@template.submitters).size == 1 && %w[api embed].exclude?(@submitter.submission.source) && @submitter.account.account_configs.find_or_initialize_by(key: AccountConfig::ALLOW_TO_RESUBMIT).value != false && @template.shared_link? %> - <%= button_to button_title(title: t('resubmit'), disabled_with: t('resubmit'), icon: svg_icon('reload', class: 'w-6 h-6')), start_form_path(@template.slug), params: { submitter: { email: params[:email] }, resubmit: true }, method: :put, class: 'white-button w-full' %> + <%= button_to button_title(title: t('resubmit'), disabled_with: t('resubmit'), icon: svg_icon('reload', class: 'w-6 h-6')), start_form_path(@template.slug), params: { submitter: params.permit(:name, :email, :phone).compact_blank, resubmit: true }, method: :put, class: 'white-button w-full' %> <% end %> diff --git a/app/views/start_form/show.html.erb b/app/views/start_form/show.html.erb index e8d6db5c..75c42e28 100644 --- a/app/views/start_form/show.html.erb +++ b/app/views/start_form/show.html.erb @@ -29,13 +29,32 @@ <% if !@template.archived_at? && !@template.account.archived_at? %> <%= form_for @submitter, url: start_form_path(@template.slug), data: { turbo_frame: :_top }, method: :put, html: { class: 'space-y-4' } do |f| %> -
- <%= f.label :email, t('email'), class: 'label' %> - <%= f.email_field :email, value: current_user&.email || params[:email] || @submitter.email, required: true, class: 'base-input', placeholder: t('provide_your_email_to_start') %> - <% if @error_message %> - <%= @error_message %> - <% end %> -
+ <% if @error_message %> +
+ <%= svg_icon('info_circle', class: 'stroke-current shrink-0 h-6 w-6 mt-1') %> +
<%= @error_message %>
+
+ <% end %> + <% link_form_fields = @template.preferences.fetch('link_form_fields', ['email']) %> + <% multiple_fields = link_form_fields.size > 1 %> + <% if link_form_fields.include?('name') %> +
+ <%= f.label :name, t('name'), class: 'label' %> + <%= f.text_field :name, value: current_user&.full_name || params[:name] || @submitter.name, required: true, class: 'base-input', placeholder: t(multiple_fields ? 'provide_your_name' : 'provide_your_name_to_start') %> +
+ <% end %> + <% if link_form_fields.include?('email') %> +
+ <%= f.label :email, t('email'), class: 'label' %> + <%= f.email_field :email, value: current_user&.email || params[:email] || @submitter.email, required: true, class: 'base-input', placeholder: t(multiple_fields ? 'provide_your_email' : 'provide_your_email_to_start') %> +
+ <% end %> + <% if link_form_fields.include?('phone') %> +
+ <%= f.label :phone, t('phone'), class: 'label' %> + <%= f.telephone_field :phone, value: params[:phone] || @submitter.phone, pattern: '^\+[0-9\s\-]+$', oninvalid: "this.value ? this.setCustomValidity('#{t('use_international_format_1xxx_')}') : ''", oninput: "this.setCustomValidity('')", required: true, class: 'base-input', placeholder: t(multiple_fields ? 'provide_your_phone_in_international_format' : 'provide_your_phone_in_international_format_to_start') %> +
+ <% end %> <%= f.button button_title(title: t('start'), disabled_with: t('starting')), class: 'base-button' %> diff --git a/app/views/templates_preferences/_recipients.html.erb b/app/views/templates_preferences/_recipients.html.erb new file mode 100644 index 00000000..89cf9861 --- /dev/null +++ b/app/views/templates_preferences/_recipients.html.erb @@ -0,0 +1,83 @@ +<% close_on_submit = local_assigns.fetch(:close_on_submit, true) %> +<%= form_for template, url: template_recipients_path(template), method: :post, html: { autocomplete: 'off', class: 'mt-1', id: :submitters_form }, data: { close_on_submit: } do |f| %> + <% unless close_on_submit %> + + <% end %> +
+ <% template.submitters.each_with_index do |submitter, index| %> +
+ <%= f.fields_for :submitters, item = Struct.new(:name, :uuid, :is_requester, :email, :invite_by_uuid, :optional_invite_by_uuid, :linked_to_uuid, :option).new(*submitter.values_at('name', 'uuid', 'is_requester', 'email', 'invite_by_uuid', 'optional_invite_by_uuid', 'linked_to_uuid')), index: do |ff| %> + <% item.option = item.is_requester.present? ? 'is_requester' : (item.email.present? ? 'email' : (item.linked_to_uuid.present? ? "linked_to_#{item.linked_to_uuid}" : (item.invite_by_uuid.present? ? "invite_by_#{item.invite_by_uuid}" : (item.optional_invite_by_uuid.present? ? "optional_invite_by_#{item.optional_invite_by_uuid}" : '')))) %> + <%= ff.hidden_field :uuid %> +
+ <%= ff.text_field :name, class: 'w-full outline-none border-transparent focus:border-transparent focus:ring-0 bg-base-100 px-1 peer mb-2', autocomplete: 'off', placeholder: "#{index + 1}#{(index + 1).ordinal} Party", required: true %> + <% if template.submitters.size == 2 %> + <%= tag.input name: ff.field_name(:email), value: ff.object.email, type: :email, class: 'base-input', multiple: true, autocomplete: 'off', placeholder: t('default_email'), disabled: ff.object.is_requester || ff.object.invite_by_uuid.present? || ff.object.optional_invite_by_uuid.present?, id: field_uuid = SecureRandom.uuid %> + <% else %> + + <%= ff.select :option, [[t('not_specified'), 'not_set'], [t('submission_requester'), 'is_requester'], [t('specified_email'), 'email'], *(template.submitters - [submitter]).flat_map { |e| [[t('invite_by_name', name: e['name']), "invite_by_#{e['uuid']}"], [t('invite_by_name', name: e['name']) + " (#{t(:optional).capitalize})", "optional_invite_by_#{e['uuid']}"]] }, *(template.submitters - [submitter]).map { |e| [t('same_as_name', name: e['name']), "linked_to_#{e['uuid']}"] }], {}, class: 'base-select mb-3' %> + + <%= tag.input name: ff.field_name(:email), type: :email, value: ff.object.email, multiple: true, class: "base-input #{'hidden' if item.option != 'email'}", autocomplete: 'off', placeholder: t('default_email'), id: email_field_uuid %> + <% end %> +
+ <% if template.submitters.size == 2 %> + + + <% if index == 1 %> + + <% end %> + + <% end %> + <% end %> +
+ <% end %> +
+<% end %> +<% unless current_account.account_configs.exists?(key: AccountConfig::ENFORCE_SIGNING_ORDER_KEY, value: true) %> + <%= form_for template, url: template_preferences_path(template), method: :post, html: { autocomplete: 'off', class: 'mt-2' }, data: { close_on_submit: false } do |f| %> +
+ + <%= t('enforce_recipients_order') %> + + <%= f.fields_for :preferences, Struct.new(:submitters_order).new(template.preferences['submitters_order']) do |ff| %> + <%= ff.check_box :submitters_order, { class: 'toggle', onchange: 'this.form.requestSubmit()' }, 'preserved', '' %> + <% end %> +
+ <% end %> +<% end %> +<% if can?(:manage, :personalization_advanced) %> + <%= form_for template, url: template_preferences_path(template), method: :post, html: { autocomplete: 'off', class: 'mt-2' }, data: { close_on_submit: false } do |f| %> +
+ + <%= t('ensure_unique_recipients') %> + + <%= f.fields_for :preferences, Struct.new(:validate_unique_submitters).new(template.preferences['validate_unique_submitters']) do |ff| %> + <%= ff.check_box :validate_unique_submitters, { class: 'toggle', onchange: 'this.form.requestSubmit()' }, 'true', '' %> + <% end %> +
+ <% end %> +<% end %> +
+ <%= button_tag button_title(title: t('save'), disabled_with: t('updating')), class: 'base-button', form: :submitters_form %> + <% unless close_on_submit %> +
+ +
+ <% end %> +
diff --git a/app/views/templates_preferences/show.html.erb b/app/views/templates_preferences/show.html.erb index 165def27..5d7cb397 100644 --- a/app/views/templates_preferences/show.html.erb +++ b/app/views/templates_preferences/show.html.erb @@ -287,91 +287,8 @@ <%= render 'templates_code_modal/preferences', class: 'pt-2' %> <% if show_recipients %> -