From 2c7948903d58166962124507f8ae01bb0c7c382f Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Wed, 10 Aug 2016 10:38:20 -0500 Subject: [PATCH] fixes #50 - summary page progress on saves --- app/assets/javascripts/live-coder.js.erb | 2 +- app/assets/javascripts/summary-edit.js | 11 ++++++++++ app/assets/stylesheets/molecules/_forms.scss | 4 ++++ app/controllers/quiz_controller.rb | 22 ++++++++++++-------- app/views/quiz/summary.html.erb | 9 ++++---- config/routes.rb | 2 +- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/live-coder.js.erb b/app/assets/javascripts/live-coder.js.erb index d88b9e2..c645a5b 100644 --- a/app/assets/javascripts/live-coder.js.erb +++ b/app/assets/javascripts/live-coder.js.erb @@ -1,5 +1,5 @@ function updateResults(elem) { - if ($(elem).length ===0){return false;}; + if ($(elem).length ===0){return false;} var resultsContainer = $(elem).find('[data-id="results"]')[0]; var codeHtml = $(elem).find('.code-html')[0].value.trim(); diff --git a/app/assets/javascripts/summary-edit.js b/app/assets/javascripts/summary-edit.js index 5b2dc34..3dc0011 100644 --- a/app/assets/javascripts/summary-edit.js +++ b/app/assets/javascripts/summary-edit.js @@ -32,11 +32,22 @@ function updateLocalValues($form){ }); } +function updateProgress(data) { + $(".progress-bar").attr('aria-valuenow', data.progress) + .attr('style','width: '+ data.progress +'%;') + .find('span').text(data.progress + '%'); + if(data.can_submit === true){ + $('#summary-submit').find('.error').remove(); + $('#summary-submit').find('.submit-button').prop('disabled', false); + } +} + function prepareAjax($form) { $form.on("ajax:success", function(e, data){ $form.prepend('
' + data.message + '
'); disableForm($form); updateLocalValues($form); + updateProgress(data); }).on("ajax:error", function(e, xhr) { if (xhr.status === 400){ $form.prepend('
' + xhr.responseJSON.join('
') + '
'); diff --git a/app/assets/stylesheets/molecules/_forms.scss b/app/assets/stylesheets/molecules/_forms.scss index 6a0ed63..f48ee86 100644 --- a/app/assets/stylesheets/molecules/_forms.scss +++ b/app/assets/stylesheets/molecules/_forms.scss @@ -29,6 +29,10 @@ label { font-weight: 300; } +form.btn-center { + text-align: center; +} + #{$all-text-inputs} { display: block; font-size: $base-font-size; diff --git a/app/controllers/quiz_controller.rb b/app/controllers/quiz_controller.rb index 1b50e63..ded3f47 100644 --- a/app/controllers/quiz_controller.rb +++ b/app/controllers/quiz_controller.rb @@ -11,9 +11,10 @@ class QuizController < ApplicationController def update_answer @answer = prep_answer answer_params[:question_id] + prep_status send "process_#{prep_question(answer_params[:question_id]).input_type}" - route_remote and return if request.xhr? - route_answer + route_answer_xhr and return if request.xhr? + route_answer_html end def summary @@ -21,11 +22,10 @@ class QuizController < ApplicationController redirect_to :question and return unless prep_status.current_question_id.nil? end - def update_summary - prep_status + def submit_summary not_completed_error = 'You must complete all questions to submit your test.' record_error = 'There was a problem with your submission. Please try again later.' - redirect_to :summary, flash: { error: not_completed_error } and return unless @status.can_submit + redirect_to :summary, flash: { error: not_completed_error } and return unless prep_status.can_submit redirect_to :thankyou and return if current_candidate.complete! redirect_to :summary, flash: { error: record_error } end @@ -62,9 +62,8 @@ class QuizController < ApplicationController answer end - def route_answer + def route_answer_html if @answer.errors.present? - prep_status prep_question answer_params[:question_id] flash[:error] = answer_params[:question_id].to_i render :question @@ -75,11 +74,16 @@ class QuizController < ApplicationController end end - def route_remote + def route_answer_xhr if @answer.errors.present? render json: @answer.errors["answer"].to_json, status: 400 else - render json: { message: "Your answer has been updated successfully!" }.to_json + results = { + message: "Your answer has been updated successfully!", + can_submit: prep_status.can_submit, + progress: prep_status.progress + } + render json: results.to_json end end diff --git a/app/views/quiz/summary.html.erb b/app/views/quiz/summary.html.erb index a6d3f02..dedf86c 100644 --- a/app/views/quiz/summary.html.erb +++ b/app/views/quiz/summary.html.erb @@ -43,11 +43,10 @@ <% end %> <% end %> - <% if @status.can_submit %> - <%= form_tag post_summary_path, class: "btn-container-right" do %> - + <%= form_tag post_summary_path, id: 'summary-submit', class: "btn-center" do %> + <% unless @status.can_submit %> +
Sorry, you must answer all questions before you can submit.
<% end %> - <% else %> -
Sorry, you must answer all questions before you can submit.
+ <%= submit_tag "Submit all answers", {class: 'submit-button', disabled: !@status.can_submit } %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 847837f..b245523 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,7 +9,7 @@ Rails.application.routes.draw do post "/question(/:answer_id)", to: "quiz#update_answer", as: :post_answer get "/question(/:question_id)", to: "quiz#question", as: :question - post "/summary", to: "quiz#update_summary", as: :post_summary + post "/summary", to: "quiz#submit_summary", as: :post_summary get "/summary", to: "quiz#summary", as: :summary get "/review/logout", to: "review#logout", as: :review_logout