From 5da755baeefa052577e4c378f7bf4849ad9cf445 Mon Sep 17 00:00:00 2001 From: Dante Soares Date: Tue, 23 Feb 2021 17:12:07 -0600 Subject: [PATCH 1/3] Set X-Requested-With XMLHttpRequest when querying collaborators --- app/assets/javascripts/admin.js | 15 +++++++++++---- config/secrets.yml | 1 - 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/admin.js b/app/assets/javascripts/admin.js index b87e7273..09c0aad9 100644 --- a/app/assets/javascripts/admin.js +++ b/app/assets/javascripts/admin.js @@ -18,15 +18,22 @@ $(document).on('turbolinks:load', function() { $("#collaborators-query").on("input", function() { const xhr = new XMLHttpRequest(); + + xhr.open( + "GET", + "/admin/publications/collaborators?collaborators_query=" + $("#collaborators-query").val(), + true + ); + + // required, otherwise Rails fails with a CORS error even if the request is from the same origin + xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); + xhr.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { eval(xhr.responseText); } }; - xhr.open( - "GET", - "/admin/publications/collaborators?collaborators_query=" + $("#collaborators-query").val(), true - ); + xhr.send(); }); diff --git a/config/secrets.yml b/config/secrets.yml index 78d5753d..fe52fc8b 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -86,7 +86,6 @@ production: sentry: dsn: <%= ENV['SENTRY_DSN'] %> release: <%= ENV['RELEASE_VERSION'] %> - aws: aws: s3: region: <%= ENV['AWS_S3_REGION'] %> From c4723a9e44146191496c975928cb7b44bfc8cb55 Mon Sep 17 00:00:00 2001 From: Dante Soares Date: Wed, 24 Feb 2021 11:14:08 -0600 Subject: [PATCH 2/3] Use fetch instead of xhr --- app/assets/javascripts/admin.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/admin.js b/app/assets/javascripts/admin.js index 09c0aad9..b9bc6645 100644 --- a/app/assets/javascripts/admin.js +++ b/app/assets/javascripts/admin.js @@ -17,24 +17,19 @@ $(document).on('turbolinks:load', function() { }); $("#collaborators-query").on("input", function() { - const xhr = new XMLHttpRequest(); - - xhr.open( - "GET", - "/admin/publications/collaborators?collaborators_query=" + $("#collaborators-query").val(), - true - ); - - // required, otherwise Rails fails with a CORS error even if the request is from the same origin - xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); - - xhr.onreadystatechange = function() { - if (this.readyState == 4 && this.status == 200) { - eval(xhr.responseText); + req = `/admin/publications/collaborators?collaborators_query=${ + $("#collaborators-query").val() + }` + + fetch(req, { headers: { "X-Requested-With": "XMLHttpRequest" } }).then(response => { + if (response.status == 200) { + return response.text(); + } else { + return Promise.reject( + new Error(`Invalid response status code ${response.status} for request ${req}`) + ); } - }; - - xhr.send(); + }, error => console.log(error)).then(text => eval(text), error => console.log(error)); }); $("#commit").click(function() { From 0079616a97c734718fc6d03e0cba82d47dc913e5 Mon Sep 17 00:00:00 2001 From: Dante Soares Date: Wed, 24 Feb 2021 11:16:54 -0600 Subject: [PATCH 3/3] Removed error handling since the default behavior is to throw which is good enough --- app/assets/javascripts/admin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/admin.js b/app/assets/javascripts/admin.js index b9bc6645..32eeeb2c 100644 --- a/app/assets/javascripts/admin.js +++ b/app/assets/javascripts/admin.js @@ -29,7 +29,7 @@ $(document).on('turbolinks:load', function() { new Error(`Invalid response status code ${response.status} for request ${req}`) ); } - }, error => console.log(error)).then(text => eval(text), error => console.log(error)); + }).then(text => eval(text)); }); $("#commit").click(function() {