WebUI: migrate to fetch API

This is the final part of it.

PR #22072.
This commit is contained in:
Chocobo1
2024-12-29 15:44:28 +08:00
committed by GitHub
parent e740a42366
commit 9c0475ebfa
15 changed files with 626 additions and 551 deletions

View File

@@ -58,19 +58,23 @@ window.qBittorrent.PropTrackers ??= (() => {
torrentTrackersTable.clear();
current_hash = new_hash;
}
const url = new URI("api/v2/torrents/trackers?hash=" + current_hash);
new Request.JSON({
url: url,
method: "get",
noCache: true,
onComplete: () => {
clearTimeout(loadTrackersDataTimer);
loadTrackersDataTimer = loadTrackersData.delay(10000);
},
onSuccess: (trackers) => {
const url = new URL("api/v2/torrents/trackers", window.location);
url.search = new URLSearchParams({
hash: current_hash
});
fetch(url, {
method: "GET",
cache: "no-store"
})
.then(async (response) => {
if (!response.ok)
return;
const selectedTrackers = torrentTrackersTable.selectedRowsIds();
torrentTrackersTable.clear();
const trackers = await response.json();
if (trackers) {
trackers.each((tracker) => {
let status;
@@ -113,8 +117,11 @@ window.qBittorrent.PropTrackers ??= (() => {
if (selectedTrackers.length > 0)
torrentTrackersTable.reselectRows(selectedTrackers);
}
}
}).send();
})
.finally(() => {
clearTimeout(loadTrackersDataTimer);
loadTrackersDataTimer = loadTrackersData.delay(10000);
});
};
const updateData = () => {
@@ -214,18 +221,19 @@ window.qBittorrent.PropTrackers ??= (() => {
if (current_hash.length === 0)
return;
const selectedTrackers = torrentTrackersTable.selectedRowsIds();
new Request({
url: "api/v2/torrents/removeTrackers",
method: "post",
data: {
hash: current_hash,
urls: selectedTrackers.map(encodeURIComponent).join("|")
},
onSuccess: () => {
fetch("api/v2/torrents/removeTrackers", {
method: "POST",
body: new URLSearchParams({
hash: current_hash,
urls: torrentTrackersTable.selectedRowsIds().map(encodeURIComponent).join("|")
})
})
.then((response) => {
if (!response.ok)
return;
updateData();
}
}).send();
});
};
const clear = () => {