From b9b6c853bb35a508d2a7ade997f5f2f4bb1aa27c Mon Sep 17 00:00:00 2001
From: Jon Dufresne
Date: Sat, 26 Sep 2020 15:16:40 -0700
Subject: [PATCH] Simplify fetch() calls in JavaScript
The fetch() init object does not have a property "url", so remove it.
The element being either a button or a element is mutually exclusive, so
use an "else if".
When a button is pressed, an enclosing form element already exist, so
remove the guard.
Assign the object properties in the object literal instead of after.
---
.../static/debug_toolbar/js/toolbar.js | 44 +++++++------------
.../debug_toolbar/panels/history.html | 4 +-
.../debug_toolbar/panels/history_tr.html | 4 +-
3 files changed, 21 insertions(+), 31 deletions(-)
diff --git a/debug_toolbar/static/debug_toolbar/js/toolbar.js b/debug_toolbar/static/debug_toolbar/js/toolbar.js
index 6628a1271..641121aff 100644
--- a/debug_toolbar/static/debug_toolbar/js/toolbar.js
+++ b/debug_toolbar/static/debug_toolbar/js/toolbar.js
@@ -105,23 +105,19 @@ const djdt = {
$$.on(djDebug, 'click', '.remoteCall', function(event) {
event.preventDefault();
+ let url;
const ajax_data = {};
if (this.tagName === 'BUTTON') {
const form = this.closest('form');
- ajax_data.url = this.formAction;
-
- if (form) {
- ajax_data.body = new FormData(form);
- ajax_data.method = form.method.toUpperCase();
- }
- }
-
- if (this.tagName === 'A') {
- ajax_data.url = this.href;
+ url = this.formAction;
+ ajax_data.method = form.method.toUpperCase();
+ ajax_data.body = new FormData(form);
+ } else if (this.tagName === 'A') {
+ url = this.href;
}
- ajax(ajax_data.url, ajax_data).then(function(data) {
+ ajax(url, ajax_data).then(function(data) {
const win = djDebug.querySelector('#djDebugWindow');
win.innerHTML = data.content;
$$.show(win);
@@ -131,22 +127,19 @@ const djdt = {
// Used by the history panel
$$.on(djDebug, 'click', '.switchHistory', function(event) {
event.preventDefault();
- const ajax_data = {};
const newStoreId = this.dataset.storeId;
const form = this.closest('form');
const tbody = this.closest('tbody');
- ajax_data.url = this.getAttribute('formaction');
-
- if (form) {
- ajax_data.body = new FormData(form);
- ajax_data.method = form.getAttribute('method') || 'POST';
- }
+ const ajax_data = {
+ method: form.method.toUpperCase(),
+ body: new FormData(form),
+ };
tbody.querySelector('.djdt-highlighted').classList.remove('djdt-highlighted');
this.closest('tr').classList.add('djdt-highlighted');
- ajax(ajax_data.url, ajax_data).then(function(data) {
+ ajax(form.action, ajax_data).then(function(data) {
djDebug.setAttribute('data-store-id', newStoreId);
Object.keys(data).map(function (panelId) {
if (djDebug.querySelector('#'+panelId)) {
@@ -160,18 +153,15 @@ const djdt = {
// Used by the history panel
$$.on(djDebug, 'click', '.refreshHistory', function(event) {
event.preventDefault();
- const ajax_data = {};
const form = this.closest('form');
const container = djDebug.querySelector('#djdtHistoryRequests');
- ajax_data.url = this.getAttribute('formaction');
-
- if (form) {
- ajax_data.body = new FormData(form);
- ajax_data.method = form.getAttribute('method') || 'POST';
- }
+ const ajax_data = {
+ method: form.method.toUpperCase(),
+ body: new FormData(form),
+ };
- ajax(ajax_data.url, ajax_data).then(function(data) {
+ ajax(form.action, ajax_data).then(function(data) {
if (data.requests.constructor === Array) {
data.requests.map(function(request) {
if (!container.querySelector('[data-store-id="'+request.id+'"]')) {
diff --git a/debug_toolbar/templates/debug_toolbar/panels/history.html b/debug_toolbar/templates/debug_toolbar/panels/history.html
index dc404fa65..873c43d73 100644
--- a/debug_toolbar/templates/debug_toolbar/panels/history.html
+++ b/debug_toolbar/templates/debug_toolbar/panels/history.html
@@ -1,7 +1,7 @@
{% load i18n %}{% load static %}
-
diff --git a/debug_toolbar/templates/debug_toolbar/panels/history_tr.html b/debug_toolbar/templates/debug_toolbar/panels/history_tr.html
index 8e446a9b6..338e96154 100644
--- a/debug_toolbar/templates/debug_toolbar/panels/history_tr.html
+++ b/debug_toolbar/templates/debug_toolbar/panels/history_tr.html
@@ -39,9 +39,9 @@
|
-
|