From 31619071aff05674cfc9d44c7b7d2f59a0528e69 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Fri, 20 Sep 2024 19:10:26 -0700 Subject: [PATCH] Remove ajax_auth.js lib, by merging it in where needed It's only actually used in two JS files, so rather than doing a weird global `$.ajaxSetup` call, let's just inline it into the small handful of AJAX calls that actually care. --- app/assets/javascripts/ajax_auth.js | 20 ------------------- .../javascripts/closet_hangers/index.js | 14 +++++++++++++ app/assets/javascripts/pets/bulk.js | 6 ++++++ app/views/closet_hangers/index.html.haml | 3 +-- app/views/outfits/new.html.haml | 2 +- app/views/pets/bulk.html.haml | 1 - 6 files changed, 22 insertions(+), 24 deletions(-) delete mode 100644 app/assets/javascripts/ajax_auth.js diff --git a/app/assets/javascripts/ajax_auth.js b/app/assets/javascripts/ajax_auth.js deleted file mode 100644 index 81fd79d5..00000000 --- a/app/assets/javascripts/ajax_auth.js +++ /dev/null @@ -1,20 +0,0 @@ -(function () { - var CSRFProtection; - var token = $('meta[name="csrf-token"]').attr("content"); - if (token) { - CSRFProtection = function (xhr, settings) { - var sendToken = - typeof settings.useCSRFProtection === "undefined" || // default to true - settings.useCSRFProtection; - if (sendToken) { - xhr.setRequestHeader("X-CSRF-Token", token); - } - }; - } else { - CSRFProtection = $.noop; - } - - $.ajaxSetup({ - beforeSend: CSRFProtection, - }); -})(); diff --git a/app/assets/javascripts/closet_hangers/index.js b/app/assets/javascripts/closet_hangers/index.js index 429a5c43..1226a5cd 100644 --- a/app/assets/javascripts/closet_hangers/index.js +++ b/app/assets/javascripts/closet_hangers/index.js @@ -1,4 +1,11 @@ (function () { + function addCSRFToken(xhr) { + const token = document + .querySelector('meta[name="csrf-token"]') + ?.getAttribute("content"); + xhr.setRequestHeader("X-CSRF-Token", token); + } + var hangersInitCallbacks = []; function onHangersInit(callback) { @@ -285,6 +292,7 @@ type: "post", data: data, dataType: "json", + beforeSend: addCSRFToken, complete: function (data) { if (quantityEl.val() == 0) { objectRemoved(objectWrapper); @@ -389,6 +397,7 @@ type: "post", data: data, dataType: "json", + beforeSend: addCSRFToken, complete: function () { button.val("Remove"); }, @@ -465,6 +474,7 @@ url: form.attr("action"), type: form.attr("method"), data: data, + beforeSend: addCSRFToken, success: function (html) { var doc = $(html); hangersEl.html(doc.find("#closet-hangers").html()); @@ -501,6 +511,7 @@ url: form.attr("action") + ".json?" + $.param({ ids: hangerIds }), type: "delete", dataType: "json", + beforeSend: addCSRFToken, success: function () { objectRemoved(hangerEls); }, @@ -567,6 +578,7 @@ closet_hanger: closetHanger, return_to: window.location.pathname + window.location.search, }, + beforeSend: addCSRFToken, complete: function () { itemsSearchField.removeClass("loading"); }, @@ -711,6 +723,7 @@ type: "post", data: data, dataType: "json", + beforeSend: addCSRFToken, complete: function () { contactForm.enableForms(); }, @@ -731,6 +744,7 @@ type: "POST", data: { neopets_connection: { neopets_username: newUsername } }, dataType: "json", + beforeSend: addCSRFToken, success: function (connection) { var newOption = $("