Skip to content

Simplify fetch() calls in JavaScript #1348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 17 additions & 27 deletions debug_toolbar/static/debug_toolbar/js/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)) {
Expand All @@ -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+'"]')) {
Expand Down
4 changes: 2 additions & 2 deletions debug_toolbar/templates/debug_toolbar/panels/history.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% load i18n %}{% load static %}
<form method="post">
<form method="post" action="{% url 'djdt:history_refresh' %}">
{{ refresh_form }}
<button formaction="{% url 'djdt:history_refresh' %}" class="refreshHistory">Refresh</button>
<button class="refreshHistory">Refresh</button>
</form>
<table style="max-height:100%;">
<thead>
Expand Down
4 changes: 2 additions & 2 deletions debug_toolbar/templates/debug_toolbar/panels/history_tr.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
</div>
</td>
<td class="djdt-actions">
<form method="post">
<form method="post" action="{% url 'djdt:history_sidebar' %}">
{{ store_context.form }}
<button formaction="{% url 'djdt:history_sidebar' %}" data-store-id="{{ id }}" class="switchHistory">Switch</button>
<button data-store-id="{{ id }}" class="switchHistory">Switch</button>
</form>
</td>
</tr>