Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix survey validation #228

Merged
merged 10 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ gem "decidim-spam_detection", git: "https://github.com/OpenSourcePolitics/decidi
gem "decidim-templates"
gem "decidim-term_customizer", git: "https://github.com/mainio/decidim-module-term_customizer.git"
gem "omniauth-france_connect", git: "https://github.com/OpenSourcePolitics/omniauth-france_connect"
gem "omniauth-publik", git: "https://github.com/OpenSourcePolitics/omniauth-publik", branch: "v0.0.9"
gem "omniauth-publik", git: "https://github.com/OpenSourcePolitics/omniauth-publik"

gem "dotenv-rails"

Expand Down
5 changes: 2 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ GIT

GIT
remote: https://github.com/OpenSourcePolitics/omniauth-publik
revision: 1e7c6ab77084bfcb0fc652f89e309f6c71e1bfd3
branch: v0.0.9
revision: ab703a565c402b773ce0025593554b329f603e5c
specs:
omniauth-publik (0.0.9)
omniauth-publik (0.1.1)
omniauth (~> 2.0)
omniauth-oauth2 (>= 1.7.2, < 2.0)

Expand Down
37 changes: 37 additions & 0 deletions app/cells/decidim/forms/step_navigation/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<div class="button--double form-general-submit answer-questionnaire__submit">
<% if first_step? || errors %>
<a></a>
<% else %>
<%= link_to(
icon("caret-left", class: "icon--small", role: "img", "aria-hidden": true) + " " + t("decidim.forms.step_navigation.show.back"),
"#",
class: "hollow secondary back_survey",
data: {
toggle: [previous_step_dom_id, current_step_dom_id].join(" ")
}
) %>
<% end %>

<% if last_step? %>
<%= form.submit(
t("decidim.forms.step_navigation.show.submit"),
class: "button button--sc submit_survey",
disabled: button_disabled?,
data: {
confirm: t("decidim.forms.step_navigation.show.are_you_sure"),
disable: true
}
) %>
<% elsif errors %>
<a></a>
<% else %>
<%= link_to(
t("decidim.forms.step_navigation.show.continue"),
"#",
class: "button button--sc next_survey",
data: {
toggle: [next_step_dom_id, current_step_dom_id].join(" ")
}
) %>
<% end %>
</div>
50 changes: 50 additions & 0 deletions app/cells/decidim/forms/step_navigation_cell.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module Decidim
module Forms
# This cell renders the navigation of a questionnaire step.
class StepNavigationCell < Decidim::ViewModel
include Decidim::LayoutHelper

def current_step_index
model
end

def first_step?
current_step_index.zero?
end

def last_step?
current_step_index + 1 == total_steps
end

def total_steps
options[:total_steps]
end

def form
options[:form]
end

def button_disabled?
options[:button_disabled]
end

def previous_step_dom_id
"step-#{current_step_index - 1}"
end

def next_step_dom_id
"step-#{current_step_index + 1}"
end

def current_step_dom_id
"step-#{current_step_index}"
end

def errors
eliegaboriau marked this conversation as resolved.
Show resolved Hide resolved
options[:errors]
end
end
end
end
28 changes: 28 additions & 0 deletions app/helpers/decidim/forms/application_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module Decidim
module Forms
# Custom helpers, scoped to the forms engine.
module ApplicationHelper
# Show cell for selected models
def show_represent_user_group?
model_name = questionnaire_for.model_name.element

permited_models.include?(model_name)
end
alias show_public_participation? show_represent_user_group?

def permited_models
%(meeting)
end

def invalid?(responses)
bool = false
eliegaboriau marked this conversation as resolved.
Show resolved Hide resolved
responses.each do |response|
bool ||= response.errors.any?
end
bool
end
end
end
end
35 changes: 35 additions & 0 deletions app/packs/src/decidim/decidim_application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,38 @@

// Load images
require.context("../../images", true)

import $ from "jquery"
import "jquery-validation"

$(() => {
if($(".submit_survey").length) {
$("body").on('DOMNodeInserted', '.confirm-reveal', function () {
$(".confirm-reveal .button:first").on("mouseup", function () {
$("form.answer-questionnaire").validate({
ignore: "thrhwrt",
errorPlacement: function (error, element) {},
focusInvalid: false,
invalidHandler: function(form, validator) {

$(".questionnaire-step").each(function () {
$(this).removeClass("hide");
});
$(".next_survey").hide();
$(".back_survey").hide();

if (!validator.numberOfInvalids())
return;

const y = validator.errorList[0].element.parentElement.getBoundingClientRect().top + window.pageYOffset - 10;

window.scrollTo({top: y, behavior: 'smooth'});
}
});
if ($("form.answer-questionnaire").valid()) {
$(".survey-form").submit();
}
});
});
}
});
155 changes: 155 additions & 0 deletions app/views/decidim/forms/questionnaires/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<% add_decidim_meta_tags({
title: translated_attribute(questionnaire.title),
description: translated_attribute(questionnaire.description),
}) %>

<% columns = allow_answers? && visitor_can_answer? && @form.responses.map(&:question).any?(&:matrix?) ? 9 : 6 %>

<%= render partial: "decidim/shared/component_announcement" if current_component.manifest_name == "surveys" %>

<div class="row columns">
<h2 class="section-heading"><%= translated_attribute questionnaire.title %></h2>
<div class="row">
<div class="columns large-<%= columns %> medium-centered lead">
<%= decidim_sanitize_editor translated_attribute questionnaire.description %>
</div>
</div>
</div>

<div class="row">
<div class="columns large-<%= columns %> medium-centered">
<div class="card">
<div class="card__content">
<% unless questionnaire_for.try(:component)&.try(:published?) %>
<div class="section">
<div class="callout warning">
<p><%= t(".questionnaire_not_published.body") %></p>
</div>
</div>
<% end %>

<% if allow_answers? %>
<% if visitor_can_answer? %>
<% if visitor_already_answered? %>
<div class="section">
<div class="callout success">
<h3 class="heading5"><%= t(".questionnaire_answered.title") %></h3>
<p><%= t(".questionnaire_answered.body") %></p>
</div>
</div>
<% else %>
<div class="answer-questionnaire">
<noscript>
<div class="section">
<div class="callout warning">
<h3 class="heading5"><%= t(".questionnaire_js_disabled.title") %></h3>
<p><%= t(".questionnaire_js_disabled.body") %></p>
</div>
</div>
</noscript>
<% unless current_participatory_space.can_participate?(current_user) %>
<div class="section">
<div class="callout alert">
<h3 class="heading5"><%= t(".questionnaire_for_private_users.title") %></h3>
<p><%= t(".questionnaire_for_private_users.body") %></p>
</div>
</div>
<% end %>

<%= decidim_form_for(@form, url: update_url, method: :post, html: { multipart: true, class: "form answer-questionnaire" }, data: { "safe-path" => form_path }) do |form| %>
<%= form_required_explanation %>
<%= invisible_captcha %>
<% answer_idx = 0 %>
<% cleaned_answer_idx = 0 %>
<% @form.responses_by_step.each_with_index do |step_answers, step_index| %>
<div id="step-<%= step_index %>" class="<%= (step_index.zero? || invalid?(@form.responses)) ? "questionnaire-step" : "questionnaire-step hide" %>" data-toggler=".hide">
<% if @form.total_steps > 1 %>
<h3 class="section-heading">
<%= t(".current_step", step: step_index + 1) %>
<span class="answer-questionnaire__steps"><%= t(".of_total_steps", total_steps: @form.total_steps) %></span>
</h3>
<% end %>

<% step_answers.each do |answer| %>
<div class="row column answer question" data-max-choices="<%= answer.question.max_choices %>" data-conditioned="<%= answer.question.display_conditions.any? %>" data-question-id="<%= answer.question.id %>">
<% answer.question.display_conditions.each do |display_condition| %>
<%= content_tag :div, nil, class: "display-condition", data: display_condition.to_html_data %>
<% end %>

<%= fields_for "questionnaire[responses][#{answer_idx}]", answer do |answer_form| %>
<%= render(
"decidim/forms/questionnaires/answer",
answer_form: answer_form,
answer: answer,
answer_idx: answer_idx,
cleaned_answer_idx: cleaned_answer_idx,
disabled: !current_participatory_space.can_participate?(current_user)
) %>
<% end %>
</div>
<% if !(answer.question.separator? || answer.question.title_and_description?) %>
<% cleaned_answer_idx += 1 %>
<% end %>
<% answer_idx += 1 %>
<% end %>

<% if step_index + 1 == @form.total_steps %>
<% if show_represent_user_group? %>
<div class="row column represent-user-group">
<%= cell("decidim/represent_user_group", form) %>
</div>
<% end %>

<% if show_public_participation? %>
<div class="row column public-participation">
<%= cell("decidim/public_participation", form) %>
</div>
<% end %>

<div class="row column tos-agreement">
<%= form.check_box :tos_agreement, label: t(".tos_agreement"), id: "questionnaire_tos_agreement", disabled: !current_participatory_space.can_participate?(current_user) %>
<div class="help-text">
<%= decidim_sanitize_editor translated_attribute questionnaire.tos %>
</div>
</div>
<% end %>

<%= cell(
"decidim/forms/step_navigation",
step_index,
total_steps: @form.total_steps,
button_disabled: !current_participatory_space.can_participate?(current_user),
form: form,
errors: invalid?(@form.responses)
) %>
</div>
<% end %>
<% end %>
</div>
<% end %>
<% else %>
<div class="answer-questionnaire">
<h3 class="section-heading"><%= t(".answer_questionnaire.title") %></h3>
<p>
<%= t(".answer_questionnaire.anonymous_user_message", sign_in_link: decidim.new_user_session_path, sign_up_link: decidim.new_user_registration_path).html_safe %>
</p>

<ol>
<%= cell("decidim/forms/question_readonly", collection: @questionnaire.questions.not_conditioned) %>
</ol>
</div>
<% end %>
<% else %>
<div class="section">
<div class="callout warning">
<h3 class="heading4"><%= t(".questionnaire_closed.title") %></h3>
<p><%= t(".questionnaire_closed.body") %></p>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>

<%= javascript_pack_tag "decidim_forms" %>
2 changes: 2 additions & 0 deletions config/i18n-tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ ignore_missing:
- layouts.decidim.header.user_menu
- decidim.devise.shared.omniauth_buttons.or
- devise.shared.links.sign_in_with_provider
- decidim.forms.step_navigation.*
- decidim.forms.questionnaires.show.*
# Consider these keys used:
ignore_unused:
- faker.*
Expand Down
16 changes: 14 additions & 2 deletions config/initializers/decidim_verifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
# end
# end

Decidim::Verifications.register_workflow(:osp_authorization_handler) do |auth|
auth.form = "Decidim::OspAuthorizationHandler"
if Rails.env.test?
Decidim::Verifications.register_workflow(:dummy_authorization_handler) do |workflow|
workflow.form = "DummyAuthorizationHandler"
workflow.action_authorizer = "DummyAuthorizationHandler::DummyActionAuthorizer"
workflow.expires_in = 1.hour

workflow.options do |options|
options.attribute :postal_code, type: :string, default: "08001", required: false
end
end
else
Decidim::Verifications.register_workflow(:osp_authorization_handler) do |auth|
auth.form = "Decidim::OspAuthorizationHandler"
end
end
Loading