Skip to content

Commit

Permalink
Fix discard behavior in quiz editor -> edit answer
Browse files Browse the repository at this point in the history
  • Loading branch information
Splines committed Mar 20, 2024
1 parent d80680c commit 955581d
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 82 deletions.
4 changes: 1 addition & 3 deletions app/abilities/answer_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ class AnswerAbility
def initialize(user)
clear_aliased_actions

can [:new, :create, :update, :destroy], Answer do |answer|
can [:new, :create, :update, :destroy, :cancel_edit], Answer do |answer|
answer.question.present? && user.can_edit?(answer.question)
end

can :update_answer_box, Answer
end
end
69 changes: 0 additions & 69 deletions app/assets/javascripts/answers.coffee

This file was deleted.

95 changes: 95 additions & 0 deletions app/assets/javascripts/answers.coffee.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# https://stackoverflow.com/a/8133832/
<% environment.context_class.instance_eval { include ApplicationHelper } %>

# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

# change button 'Bearbeiten' to 'verwerfen' after answer body is revealed

# the "target button" is either the "discard" button or the "edit" button
# what its purpose is is stored in this object with a mapping
# id -> boolean
targetButtonIsDiscardButton = {}
window.registeredDiscardListeners = new Set(); # set of answer ids

$(document).on 'turbolinks:load', ->

$(document).on 'shown.bs.collapse', '[id^="collapse-answer-"]', ->
# Answer is now shown to the user and can be edited
answerId = $(this).data('id');
registerDiscardListeners();
targetButtonIsDiscardButton[answerId] = true;
$target = $('#targets-answer-' + answerId)
$target.empty().append($target.data('discard'))
.removeClass('btn-primary').addClass('btn-secondary')

$(document).on 'hidden.bs.collapse', '[id^="collapse-answer-"]', ->
# Answer is now hidden from the user
answerId = $(this).data('id')
targetButtonIsDiscardButton[answerId] = false;
$target = $('#targets-answer-' + answerId)
$target.empty().append($target.data('edit'))
.removeClass('btn-secondary').addClass('btn-primary')

# change color of box depending on whether answer is marked
# as correct or incorrect
$(document).on 'change', '[id^="answer-value-"]', ->
id = $(this).data('id')
isCorrectAnswer = $('#answer-true-' + id).is(':checked')

# Set background color
if isCorrectAnswer
newClass = "<%= bgcolor(true) %>";
else
newClass = "<%= bgcolor(false) %>";
$('#answer-header-' + id)
.removeClass('bg-correct')
.removeClass('bg-incorrect')
.addClass(newClass)

# Set ballot box
answerBox = $('#answer-box-' + id)
answerBox.empty()
if isCorrectAnswer
answerBox.append '<%= ballot_box(true) %>'
else
answerBox.append '<%= ballot_box(false) %>'

# Cancel new answer creation
$(document).on 'click', '#new-answer-cancel', ->
$('#new-answer').show()
$('#new-answer-field').empty()

# clean up everything before turbolinks caches
$(document).on 'turbolinks:before-cache', ->
$(document).off 'shown.bs.collapse', '[id^="collapse-answer-"]'
$(document).off 'hidden.bs.collapse', '[id^="collapse-answer-"]'
$(document).off 'change', '[id^="answer-value-"]'
$(document).off 'click', '#new-answer-cancel'


registerDiscardListeners = () ->
buttons = $('[id^=targets-answer-]');
$.each(buttons, (i,btn) ->
btn = $(btn);
answerId = btn.attr('id').split('-')[2];

# Don't register listeners multiple times
if answerId in window.registeredDiscardListeners
return;

window.registeredDiscardListeners.add(answerId);
$(this).on('click', (evt) =>
isDiscardButton = targetButtonIsDiscardButton[answerId];
if not isDiscardButton
return;

# On discard
$.ajax Routes.cancel_edit_answer_path(answerId),
type: 'GET'
dataType: 'script'
error: (jqXHR, textStatus, errorThrown) ->
console.log("AJAX Error: #{textStatus}")
);
);
7 changes: 3 additions & 4 deletions app/controllers/answers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AnswersController
class AnswersController < ApplicationController
before_action :set_answer, except: [:new, :create, :update_answer_box]
before_action :set_answer, except: [:new, :create]
authorize_resource except: [:new, :create]

def current_ability
Expand Down Expand Up @@ -34,9 +34,8 @@ def destroy
@success = true
end

def update_answer_box
@answer_id = params[:answer_id].to_i
@value = params[:value] == "true"
def cancel_edit
I18n.locale = @answer.question&.locale_with_inheritance
end

private
Expand Down
39 changes: 39 additions & 0 deletions app/views/answers/cancel_edit.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var answerId = <%= @answer.id %>;
var answerCard = $(`#answers-accordion > #answer-card-${answerId}`);
window.registeredDiscardListeners.delete(answerId);

// eslint-disable-next-line @stylistic/quotes
var newAnswerCardElements = $(`<%= j render partial: 'answers/card', locals: { answer: @answer } %>`).children();

// re-render possible MathJax content
// eslint-disable-next-line no-undef
renderMathInElement(newAnswerCardElements.get(0), {
delimiters: [
{
left: "$$",
right: "$$",
display: true,
},
{
left: "$",
right: "$",
display: false,
},
{
left: "\\(",
right: "\\)",
display: false,
},
{
left: "\\[",
right: "\\]",
display: true,
},
],
throwOnError: false,
},
);

setTimeout(() => {
answerCard.empty().append(newAnswerCardElements);
}, 100);
3 changes: 0 additions & 3 deletions app/views/answers/update_answer_box.coffee

This file was deleted.

6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
resources :announcements, only: [:index, :new, :create]

# answers routes

get "answers/update_answer_box",
as: "update_answer_box"
get "answers/:id/cancel_edit",
to: "answers#cancel_edit",
as: "cancel_edit_answer"

resources :answers, except: [:index, :show, :edit]

Expand Down

0 comments on commit 955581d

Please sign in to comment.