Skip to content

Commit 97bed99

Browse files
committed
Re-Implement updating toasts
Signed-off-by: yubiuser <github@yubiuser.dev>
1 parent da15e51 commit 97bed99

12 files changed

Lines changed: 271 additions & 79 deletions

scripts/js/groups-clients.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"use strict";
1212

1313
let table;
14+
const toasts = {};
1415

1516
function reloadClientSuggestions() {
1617
$.ajax({
@@ -370,7 +371,8 @@ function addClient() {
370371
"warning",
371372
"",
372373
"Warning",
373-
"Input is neither a valid IP or MAC address nor a valid host name!"
374+
"Input is neither a valid IP or MAC address nor a valid host name!",
375+
null
374376
);
375377
return;
376378
}
@@ -380,11 +382,11 @@ function addClient() {
380382

381383
if (ips.length === 0) {
382384
utils.enableAll();
383-
utils.showAlert("warning", "", "Warning", "Please specify a client IP or MAC address");
385+
utils.showAlert("warning", "", "Warning", "Please specify a client IP or MAC address", null);
384386
return;
385387
}
386388

387-
utils.showAlert("info", "", "Adding client(s)...", ipStr);
389+
toasts[ips] = utils.showAlert("info", "", "Adding client(s)...", ipStr, null);
388390

389391
$.ajax({
390392
url: document.body.dataset.apiurl + "/clients",
@@ -395,7 +397,7 @@ function addClient() {
395397
data: JSON.stringify({ client: ips, comment, groups: group }),
396398
success(data) {
397399
utils.enableAll();
398-
utils.listsAlert("client", ips, data);
400+
utils.listsAlert("client", ips, data, toasts[ips]);
399401
reloadClientSuggestions();
400402
$("#new_comment").val("");
401403
table.ajax.reload(null, false);
@@ -407,7 +409,7 @@ function addClient() {
407409
error(data, exception) {
408410
apiFailure(data);
409411
utils.enableAll();
410-
utils.showAlert("error", "", "Error while adding new client", data.responseText);
412+
utils.showAlert("error", "", "Error while adding new client", data.responseText, toasts[ips]);
411413
console.log(exception); // eslint-disable-line no-console
412414
},
413415
});
@@ -442,7 +444,7 @@ function editClient() {
442444

443445
utils.disableAll();
444446
const clientDecoded = utils.hexDecode(client);
445-
utils.showAlert("info", "", "Editing client...", clientDecoded);
447+
toasts[client] = utils.showAlert("info", "", "Editing client...", clientDecoded, null);
446448
$.ajax({
447449
url: document.body.dataset.apiurl + "/clients/" + encodeURIComponent(clientDecoded),
448450
method: "put",
@@ -455,7 +457,7 @@ function editClient() {
455457
}),
456458
success(data) {
457459
utils.enableAll();
458-
processGroupResult(data, "client", done, notDone);
460+
processGroupResult(data, "client", done, notDone, toasts[client]);
459461
table.ajax.reload(null, false);
460462
},
461463
error(data, exception) {
@@ -465,7 +467,8 @@ function editClient() {
465467
"error",
466468
"",
467469
"Error while " + notDone + " client " + clientDecoded,
468-
data.responseText
470+
data.responseText,
471+
toasts[client]
469472
);
470473
console.log(exception); // eslint-disable-line no-console
471474
},

scripts/js/groups-common.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function getGroups() {
5656
}
5757

5858
// eslint-disable-next-line no-unused-vars
59-
function processGroupResult(data, type, done, notDone) {
59+
function processGroupResult(data, type, done, notDone, oldToastInstance) {
6060
// Helper function to get the label of an item
6161
const itemLabel = entry => {
6262
if (typeof entry === "string") {
@@ -76,7 +76,8 @@ function processGroupResult(data, type, done, notDone) {
7676
"success",
7777
"fas fa-pencil-alt",
7878
`Successfully ${done} ${type}`,
79-
itemLabel(item)
79+
itemLabel(item),
80+
oldToastInstance
8081
);
8182
}
8283

@@ -87,7 +88,8 @@ function processGroupResult(data, type, done, notDone) {
8788
"error",
8889
"",
8990
`Error while ${notDone} ${type} ${itemLabel(error)}`,
90-
typeof error.error === "string" ? error.error : String(error.error)
91+
typeof error.error === "string" ? error.error : String(error.error),
92+
oldToastInstance
9193
);
9294
}
9395
}
@@ -119,8 +121,15 @@ function delGroupItems(type, ids, table, listType = undefined) {
119121
type = listType + " " + type;
120122
}
121123

124+
const toast = {};
122125
utils.disableAll();
123-
utils.showAlert("info", "", "Deleting " + ids.length + " " + type + "...", idstring);
126+
toast[ids] = utils.showAlert(
127+
"info",
128+
"",
129+
"Deleting " + ids.length + " " + type + "...",
130+
idstring,
131+
null
132+
);
124133

125134
$.ajax({
126135
url,
@@ -130,7 +139,13 @@ function delGroupItems(type, ids, table, listType = undefined) {
130139
})
131140
.done(() => {
132141
utils.enableAll();
133-
utils.showAlert("success", "far fa-trash-alt", "Successfully deleted " + type, idstring);
142+
utils.showAlert(
143+
"success",
144+
"far fa-trash-alt",
145+
"Successfully deleted " + type,
146+
idstring,
147+
toast[ids]
148+
);
134149
table.ajax.reload(null, false);
135150

136151
// Clear selection after deletion
@@ -143,7 +158,7 @@ function delGroupItems(type, ids, table, listType = undefined) {
143158
.fail((data, exception) => {
144159
apiFailure(data);
145160
utils.enableAll();
146-
utils.showAlert("error", "", "Error while deleting " + type, data.responseText);
161+
utils.showAlert("error", "", "Error while deleting " + type, data.responseText, toast[ids]);
147162
console.log(exception); // eslint-disable-line no-console
148163
});
149164
}

scripts/js/groups-domains.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
let table;
1414
let GETDict = {};
15+
const toasts = {};
1516

1617
$(() => {
1718
GETDict = utils.parseQueryString();
@@ -475,11 +476,11 @@ function addDomain() {
475476

476477
if (domains.length === 0) {
477478
utils.enableAll();
478-
utils.showAlert("warning", "", "Warning", "Please specify at least one domain");
479+
utils.showAlert("warning", "", "Warning", "Please specify at least one domain", null);
479480
return;
480481
}
481482

482-
utils.showAlert("info", "", "Adding domain(s)...", domainStr);
483+
toasts[domains] = utils.showAlert("info", "", "Adding domain(s)...", domainStr, null);
483484

484485
// Check if the wildcard checkbox was marked and transform the domains into regex
485486
if (kind === "exact" && wildcardChecked) {
@@ -514,7 +515,7 @@ function addDomain() {
514515
}),
515516
success(data) {
516517
utils.enableAll();
517-
utils.listsAlert("domain", domains, data);
518+
utils.listsAlert("domain", domains, data, toasts[domains]);
518519
$("#new_domain").val("");
519520
$("#new_domain_comment").val("");
520521
$("#new_regex").val("");
@@ -528,7 +529,13 @@ function addDomain() {
528529
error(data, exception) {
529530
apiFailure(data);
530531
utils.enableAll();
531-
utils.showAlert("error", "", "Error while adding new domain", data.responseText);
532+
utils.showAlert(
533+
"error",
534+
"",
535+
"Error while adding new domain",
536+
data.responseText,
537+
toasts[domains]
538+
);
532539
console.log(exception); // eslint-disable-line no-console
533540
},
534541
});
@@ -587,7 +594,8 @@ function editDomain() {
587594

588595
utils.disableAll();
589596
const domainDecoded = utils.hexDecode(domain.split("_", 1)[0]);
590-
utils.showAlert("info", "", "Editing domain...", domainDecoded);
597+
598+
toasts[domainDecoded] = utils.showAlert("info", "", "Editing domain...", domainDecoded, null);
591599
$.ajax({
592600
url:
593601
document.body.dataset.apiurl +
@@ -608,7 +616,7 @@ function editDomain() {
608616
}),
609617
success(data) {
610618
utils.enableAll();
611-
processGroupResult(data, "domain", done, notDone);
619+
processGroupResult(data, "domain", done, notDone, toasts[domainDecoded]);
612620
table.ajax.reload(null, false);
613621
},
614622
error(data, exception) {
@@ -618,7 +626,8 @@ function editDomain() {
618626
"error",
619627
"",
620628
"Error while " + notDone + " domain " + domainDecoded,
621-
data.responseText
629+
data.responseText,
630+
toasts[domainDecoded]
622631
);
623632
console.log(exception); // eslint-disable-line no-console
624633
},

scripts/js/groups-lists.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
let table;
1414
let GETDict = {};
15+
const toasts = {};
1516

1617
$(() => {
1718
GETDict = utils.parseQueryString();
@@ -490,11 +491,17 @@ function addList(event) {
490491
if (addresses.length === 0) {
491492
// enable the ui elements again
492493
utils.enableAll();
493-
utils.showAlert("warning", "", "Warning", "Please specify " + type + "list address");
494+
utils.showAlert("warning", "", "Warning", "Please specify " + type + "list address", null);
494495
return;
495496
}
496497

497-
utils.showAlert("info", "", "Adding subscribed " + type + "list(s)...", addressestr);
498+
toasts[addressestr] = utils.showAlert(
499+
"info",
500+
"",
501+
"Adding subscribed " + type + "list(s)...",
502+
addressestr,
503+
null
504+
);
498505

499506
$.ajax({
500507
url: document.body.dataset.apiurl + "/lists?type=" + encodeURIComponent(type),
@@ -505,7 +512,7 @@ function addList(event) {
505512
data: JSON.stringify({ address: addresses, comment, groups: group }),
506513
success(data) {
507514
utils.enableAll();
508-
utils.listsAlert(type + "list", addresses, data);
515+
utils.listsAlert(type + "list", addresses, data, toasts[addressestr]);
509516
$("#new_address").val("");
510517
$("#new_comment").val("");
511518
table.ajax.reload(null, false);
@@ -517,7 +524,13 @@ function addList(event) {
517524
error(data, exception) {
518525
apiFailure(data);
519526
utils.enableAll();
520-
utils.showAlert("error", "", "Error while adding new " + type + "list", data.responseText);
527+
utils.showAlert(
528+
"error",
529+
"",
530+
"Error while adding new " + type + "list",
531+
data.responseText,
532+
toasts[addressestr]
533+
);
521534
console.log(exception); // eslint-disable-line no-console
522535
},
523536
});
@@ -564,7 +577,7 @@ function editList() {
564577
}
565578

566579
utils.disableAll();
567-
utils.showAlert("info", "", "Editing address...", address);
580+
toasts[address] = utils.showAlert("info", "", "Editing address...", address, null);
568581
$.ajax({
569582
url: document.body.dataset.apiurl + "/lists/" + encodeURIComponent(address) + "?type=" + type,
570583
method: "put",
@@ -579,7 +592,7 @@ function editList() {
579592
}),
580593
success(data) {
581594
utils.enableAll();
582-
processGroupResult(data, type + "list", done, notDone);
595+
processGroupResult(data, type + "list", done, notDone, toasts[address]);
583596
table.ajax.reload(null, false);
584597
},
585598
error(data, exception) {
@@ -589,7 +602,8 @@ function editList() {
589602
"error",
590603
"",
591604
"Error while " + notDone + type + "list " + address,
592-
data.responseText
605+
data.responseText,
606+
toasts[address]
593607
);
594608
console.log(exception); // eslint-disable-line no-console
595609
},

scripts/js/groups.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"use strict";
1111

1212
let table;
13+
const toasts = {};
1314

1415
function handleAjaxError(xhr, textStatus) {
1516
if (textStatus === "timeout") {
@@ -254,11 +255,11 @@ function addGroup() {
254255
if (names.length === 0) {
255256
// enable the ui elements again
256257
utils.enableAll();
257-
utils.showAlert("warning", "", "Warning", "Please specify a group name");
258+
utils.showAlert("warning", "", "Warning", "Please specify a group name", null);
258259
return;
259260
}
260261

261-
utils.showAlert("info", "", "Adding group(s)...", groupStr);
262+
toasts[groupStr] = utils.showAlert("info", "", "Adding group(s)...", groupStr, null);
262263

263264
$.ajax({
264265
url: document.body.dataset.apiurl + "/groups",
@@ -273,7 +274,7 @@ function addGroup() {
273274
}),
274275
success(data) {
275276
utils.enableAll();
276-
utils.listsAlert("group", names, data);
277+
utils.listsAlert("group", names, data, toasts[groupStr]);
277278
$("#new_name").val("");
278279
$("#new_comment").val("");
279280
table.ajax.reload();
@@ -285,7 +286,13 @@ function addGroup() {
285286
error(data, exception) {
286287
apiFailure(data);
287288
utils.enableAll();
288-
utils.showAlert("error", "", "Error while adding new group", data.responseText);
289+
utils.showAlert(
290+
"error",
291+
"",
292+
"Error while adding new group",
293+
data.responseText,
294+
toasts[groupStr]
295+
);
289296
console.log(exception); // eslint-disable-line no-console
290297
},
291298
});
@@ -327,7 +334,7 @@ function editGroup() {
327334
}
328335

329336
utils.disableAll();
330-
utils.showAlert("info", "", "Editing group...", oldName);
337+
toasts[id] = utils.showAlert("info", "", "Editing group...", oldName, null);
331338
$.ajax({
332339
url: document.body.dataset.apiurl + "/groups/" + oldName,
333340
method: "put",
@@ -341,7 +348,7 @@ function editGroup() {
341348
}),
342349
success(data) {
343350
utils.enableAll();
344-
processGroupResult(data, "group", done, notDone);
351+
processGroupResult(data, "group", done, notDone, toasts[id]);
345352
table.ajax.reload(null, false);
346353
},
347354
error(data, exception) {
@@ -351,7 +358,8 @@ function editGroup() {
351358
"error",
352359
"",
353360
"Error while " + notDone + " group with name " + oldName,
354-
data.responseText
361+
data.responseText,
362+
toasts[id]
355363
);
356364
console.log(exception); // eslint-disable-line no-console
357365
},

0 commit comments

Comments
 (0)