From e00142876a36cf3af690919ab388a45c993aa56a Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Sun, 13 Oct 2024 22:02:08 +0200 Subject: [PATCH] Use safe navigation operator for Bootstrap Modals There are too many edge cases where the modal might not be present any longer, and we just want to avoid unnecessary errors. --- app/assets/javascripts/editor/editor.js.erb | 2 +- app/assets/javascripts/editor/participantsupport.js.erb | 2 +- app/assets/javascripts/exercise_collections.js | 2 +- app/assets/javascripts/exercises.js | 4 ++-- app/assets/javascripts/request_for_comments.js | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/editor/editor.js.erb b/app/assets/javascripts/editor/editor.js.erb index dca11943e..dcdfad849 100644 --- a/app/assets/javascripts/editor/editor.js.erb +++ b/app/assets/javascripts/editor/editor.js.erb @@ -501,7 +501,7 @@ var CodeOceanEditor = { $('#askForCommentsButton').one('click', this.requestComments.bind(this)); $('#closeAskForCommentsButton').on('click', function () { - bootstrap.Modal.getInstance($('#comment-modal')).hide(); + bootstrap.Modal.getInstance($('#comment-modal'))?.hide(); }); setTimeout(function () { diff --git a/app/assets/javascripts/editor/participantsupport.js.erb b/app/assets/javascripts/editor/participantsupport.js.erb index c3a0589e8..af16b9d76 100644 --- a/app/assets/javascripts/editor/participantsupport.js.erb +++ b/app/assets/javascripts/editor/participantsupport.js.erb @@ -140,7 +140,7 @@ CodeOceanEditorRequestForComments = { } }).catch(this.ajaxError.bind(this)); - bootstrap.Modal.getInstance(commentModal).hide(); + bootstrap.Modal.getInstance(commentModal)?.hide(); this.hideSpinner(); questionElement.prop("disabled", false).val(''); closeAskForCommentsButton.removeClass('d-none'); diff --git a/app/assets/javascripts/exercise_collections.js b/app/assets/javascripts/exercise_collections.js index 5c8f4b913..d89bcd271 100644 --- a/app/assets/javascripts/exercise_collections.js +++ b/app/assets/javascripts/exercise_collections.js @@ -187,7 +187,7 @@ $(document).on('turbolinks:load', function() { for (var i = 0; i < selectedExercises.length; i++) { addExercise(selectedExercises[i].value, selectedExercises[i].label); } - bootstrap.Modal.getInstance($('#add-exercise-modal')).hide(); + bootstrap.Modal.getInstance($('#add-exercise-modal'))?.hide(); updateExerciseList(); addExercisesForm.find('select').val('').trigger("chosen:updated"); }); diff --git a/app/assets/javascripts/exercises.js b/app/assets/javascripts/exercises.js index c18f49496..ba5225829 100644 --- a/app/assets/javascripts/exercises.js +++ b/app/assets/javascripts/exercises.js @@ -254,7 +254,7 @@ $(document).on('turbolinks:load', function () { for (let i = 0; i < selectedTips.length; i++) { addTip(selectedTips[i].value, selectedTips[i].label); } - bootstrap.Modal.getInstance($('#add-tips-modal')).hide(); + bootstrap.Modal.getInstance($('#add-tips-modal'))?.hide(); updateTipsJSON(); chosenInputTips.val('').trigger("chosen:updated"); }); @@ -395,7 +395,7 @@ $(document).on('turbolinks:load', function () { if (response.status === 'success') { $messageDiv.addClass('export-success'); setTimeout((function () { - bootstrap.Modal.getInstance($('#export-modal')).hide(); + bootstrap.Modal.getInstance($('#export-modal'))?.hide(); $messageDiv.html('').removeClass('export-success'); }), 3000); } else { diff --git a/app/assets/javascripts/request_for_comments.js b/app/assets/javascripts/request_for_comments.js index 632e51738..9d4ca20be 100644 --- a/app/assets/javascripts/request_for_comments.js +++ b/app/assets/javascripts/request_for_comments.js @@ -377,7 +377,7 @@ $(document).on('turbolinks:load', function () { if (commenttext !== "") { createComment(fileid, row, editor, commenttext); commentTextarea.val(''); - bootstrap.Modal.getInstance(commentModal).hide(); + bootstrap.Modal.getInstance(commentModal)?.hide(); } });