Skip to content

Commit 56b1489

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

9 files changed

Lines changed: 184 additions & 56 deletions

File tree

scripts/js/groups-clients.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ function addClient() {
370370
"warning",
371371
"",
372372
"Warning",
373-
"Input is neither a valid IP or MAC address nor a valid host name!"
373+
"Input is neither a valid IP or MAC address nor a valid host name!",
374+
null
374375
);
375376
return;
376377
}
@@ -380,11 +381,13 @@ function addClient() {
380381

381382
if (ips.length === 0) {
382383
utils.enableAll();
383-
utils.showAlert("warning", "", "Warning", "Please specify a client IP or MAC address");
384+
utils.showAlert("warning", "", "Warning", "Please specify a client IP or MAC address", null);
384385
return;
385386
}
386387

387-
utils.showAlert("info", "", "Adding client(s)...", ipStr);
388+
const toasts = {};
389+
//eslint-disable-next-line unicorn/no-immediate-mutation
390+
toasts[ips] = utils.showAlert("info", "", "Adding client(s)...", ipStr, null);
388391

389392
$.ajax({
390393
url: document.body.dataset.apiurl + "/clients",
@@ -395,7 +398,7 @@ function addClient() {
395398
data: JSON.stringify({ client: ips, comment, groups: group }),
396399
success(data) {
397400
utils.enableAll();
398-
utils.listsAlert("client", ips, data);
401+
utils.listsAlert("client", ips, data, toasts[ips]);
399402
reloadClientSuggestions();
400403
$("#new_comment").val("");
401404
table.ajax.reload(null, false);
@@ -407,7 +410,7 @@ function addClient() {
407410
error(data, exception) {
408411
apiFailure(data);
409412
utils.enableAll();
410-
utils.showAlert("error", "", "Error while adding new client", data.responseText);
413+
utils.showAlert("error", "", "Error while adding new client", data.responseText, toasts[ips]);
411414
console.log(exception); // eslint-disable-line no-console
412415
},
413416
});
@@ -440,9 +443,11 @@ function editClient() {
440443
return;
441444
}
442445

446+
const toasts = {};
447+
443448
utils.disableAll();
444449
const clientDecoded = utils.hexDecode(client);
445-
utils.showAlert("info", "", "Editing client...", clientDecoded);
450+
toasts[client] = utils.showAlert("info", "", "Editing client...", clientDecoded, null);
446451
$.ajax({
447452
url: document.body.dataset.apiurl + "/clients/" + encodeURIComponent(clientDecoded),
448453
method: "put",
@@ -455,7 +460,7 @@ function editClient() {
455460
}),
456461
success(data) {
457462
utils.enableAll();
458-
processGroupResult(data, "client", done, notDone);
463+
processGroupResult(data, "client", done, notDone, toasts[client]);
459464
table.ajax.reload(null, false);
460465
},
461466
error(data, exception) {
@@ -465,7 +470,8 @@ function editClient() {
465470
"error",
466471
"",
467472
"Error while " + notDone + " client " + clientDecoded,
468-
data.responseText
473+
data.responseText,
474+
toasts[client]
469475
);
470476
console.log(exception); // eslint-disable-line no-console
471477
},

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: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,13 @@ function addDomain() {
475475

476476
if (domains.length === 0) {
477477
utils.enableAll();
478-
utils.showAlert("warning", "", "Warning", "Please specify at least one domain");
478+
utils.showAlert("warning", "", "Warning", "Please specify at least one domain", null);
479479
return;
480480
}
481481

482-
utils.showAlert("info", "", "Adding domain(s)...", domainStr);
482+
const toast = {};
483+
//eslint-disable-next-line unicorn/no-immediate-mutation
484+
toast[domains] = utils.showAlert("info", "", "Adding domain(s)...", domainStr, null);
483485

484486
// Check if the wildcard checkbox was marked and transform the domains into regex
485487
if (kind === "exact" && wildcardChecked) {
@@ -514,7 +516,7 @@ function addDomain() {
514516
}),
515517
success(data) {
516518
utils.enableAll();
517-
utils.listsAlert("domain", domains, data);
519+
utils.listsAlert("domain", domains, data, toast[domains]);
518520
$("#new_domain").val("");
519521
$("#new_domain_comment").val("");
520522
$("#new_regex").val("");
@@ -528,7 +530,13 @@ function addDomain() {
528530
error(data, exception) {
529531
apiFailure(data);
530532
utils.enableAll();
531-
utils.showAlert("error", "", "Error while adding new domain", data.responseText);
533+
utils.showAlert(
534+
"error",
535+
"",
536+
"Error while adding new domain",
537+
data.responseText,
538+
toast[domains]
539+
);
532540
console.log(exception); // eslint-disable-line no-console
533541
},
534542
});
@@ -587,7 +595,9 @@ function editDomain() {
587595

588596
utils.disableAll();
589597
const domainDecoded = utils.hexDecode(domain.split("_", 1)[0]);
590-
utils.showAlert("info", "", "Editing domain...", domainDecoded);
598+
const toast = {};
599+
//eslint-disable-next-line unicorn/no-immediate-mutation
600+
toast[domainDecoded] = utils.showAlert("info", "", "Editing domain...", domainDecoded, null);
591601
$.ajax({
592602
url:
593603
document.body.dataset.apiurl +
@@ -608,7 +618,7 @@ function editDomain() {
608618
}),
609619
success(data) {
610620
utils.enableAll();
611-
processGroupResult(data, "domain", done, notDone);
621+
processGroupResult(data, "domain", done, notDone, toast[domainDecoded]);
612622
table.ajax.reload(null, false);
613623
},
614624
error(data, exception) {
@@ -618,7 +628,8 @@ function editDomain() {
618628
"error",
619629
"",
620630
"Error while " + notDone + " domain " + domainDecoded,
621-
data.responseText
631+
data.responseText,
632+
toast[domainDecoded]
622633
);
623634
console.log(exception); // eslint-disable-line no-console
624635
},

scripts/js/groups-lists.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,19 @@ function addList(event) {
490490
if (addresses.length === 0) {
491491
// enable the ui elements again
492492
utils.enableAll();
493-
utils.showAlert("warning", "", "Warning", "Please specify " + type + "list address");
493+
utils.showAlert("warning", "", "Warning", "Please specify " + type + "list address", null);
494494
return;
495495
}
496496

497-
utils.showAlert("info", "", "Adding subscribed " + type + "list(s)...", addressestr);
497+
const toast = {};
498+
//eslint-disable-next-line unicorn/no-immediate-mutation
499+
toast[addressestr] = utils.showAlert(
500+
"info",
501+
"",
502+
"Adding subscribed " + type + "list(s)...",
503+
addressestr,
504+
null
505+
);
498506

499507
$.ajax({
500508
url: document.body.dataset.apiurl + "/lists?type=" + encodeURIComponent(type),
@@ -505,7 +513,7 @@ function addList(event) {
505513
data: JSON.stringify({ address: addresses, comment, groups: group }),
506514
success(data) {
507515
utils.enableAll();
508-
utils.listsAlert(type + "list", addresses, data);
516+
utils.listsAlert(type + "list", addresses, data, toast[addressestr]);
509517
$("#new_address").val("");
510518
$("#new_comment").val("");
511519
table.ajax.reload(null, false);
@@ -517,7 +525,13 @@ function addList(event) {
517525
error(data, exception) {
518526
apiFailure(data);
519527
utils.enableAll();
520-
utils.showAlert("error", "", "Error while adding new " + type + "list", data.responseText);
528+
utils.showAlert(
529+
"error",
530+
"",
531+
"Error while adding new " + type + "list",
532+
data.responseText,
533+
toast[addressestr]
534+
);
521535
console.log(exception); // eslint-disable-line no-console
522536
},
523537
});
@@ -564,7 +578,9 @@ function editList() {
564578
}
565579

566580
utils.disableAll();
567-
utils.showAlert("info", "", "Editing address...", address);
581+
const toasts = {};
582+
//eslint-disable-next-line unicorn/no-immediate-mutation
583+
toasts[address] = utils.showAlert("info", "", "Editing address...", address, null);
568584
$.ajax({
569585
url: document.body.dataset.apiurl + "/lists/" + encodeURIComponent(address) + "?type=" + type,
570586
method: "put",
@@ -579,7 +595,7 @@ function editList() {
579595
}),
580596
success(data) {
581597
utils.enableAll();
582-
processGroupResult(data, type + "list", done, notDone);
598+
processGroupResult(data, type + "list", done, notDone, toasts[address]);
583599
table.ajax.reload(null, false);
584600
},
585601
error(data, exception) {
@@ -589,7 +605,8 @@ function editList() {
589605
"error",
590606
"",
591607
"Error while " + notDone + type + "list " + address,
592-
data.responseText
608+
data.responseText,
609+
toasts[address]
593610
);
594611
console.log(exception); // eslint-disable-line no-console
595612
},

scripts/js/groups.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,13 @@ function addGroup() {
254254
if (names.length === 0) {
255255
// enable the ui elements again
256256
utils.enableAll();
257-
utils.showAlert("warning", "", "Warning", "Please specify a group name");
257+
utils.showAlert("warning", "", "Warning", "Please specify a group name", null);
258258
return;
259259
}
260260

261-
utils.showAlert("info", "", "Adding group(s)...", groupStr);
261+
const toast = {};
262+
//eslint-disable-next-line unicorn/no-immediate-mutation
263+
toast[groupStr] = utils.showAlert("info", "", "Adding group(s)...", groupStr, null);
262264

263265
$.ajax({
264266
url: document.body.dataset.apiurl + "/groups",
@@ -273,7 +275,7 @@ function addGroup() {
273275
}),
274276
success(data) {
275277
utils.enableAll();
276-
utils.listsAlert("group", names, data);
278+
utils.listsAlert("group", names, data, toast[groupStr]);
277279
$("#new_name").val("");
278280
$("#new_comment").val("");
279281
table.ajax.reload();
@@ -285,7 +287,13 @@ function addGroup() {
285287
error(data, exception) {
286288
apiFailure(data);
287289
utils.enableAll();
288-
utils.showAlert("error", "", "Error while adding new group", data.responseText);
290+
utils.showAlert(
291+
"error",
292+
"",
293+
"Error while adding new group",
294+
data.responseText,
295+
toast[groupStr]
296+
);
289297
console.log(exception); // eslint-disable-line no-console
290298
},
291299
});
@@ -300,6 +308,7 @@ function editGroup() {
300308
const enabled = tr.find("#enabled_" + id).is(":checked");
301309
const comment = tr.find("#comment_" + id).val();
302310

311+
const toasts = {};
303312
let done = "edited";
304313
let notDone = "editing";
305314
switch (elem) {
@@ -327,7 +336,7 @@ function editGroup() {
327336
}
328337

329338
utils.disableAll();
330-
utils.showAlert("info", "", "Editing group...", oldName);
339+
toasts[id] = utils.showAlert("info", "", "Editing group...", oldName, null);
331340
$.ajax({
332341
url: document.body.dataset.apiurl + "/groups/" + oldName,
333342
method: "put",
@@ -341,7 +350,7 @@ function editGroup() {
341350
}),
342351
success(data) {
343352
utils.enableAll();
344-
processGroupResult(data, "group", done, notDone);
353+
processGroupResult(data, "group", done, notDone, toasts[id]);
345354
table.ajax.reload(null, false);
346355
},
347356
error(data, exception) {
@@ -351,7 +360,8 @@ function editGroup() {
351360
"error",
352361
"",
353362
"Error while " + notDone + " group with name " + oldName,
354-
data.responseText
363+
data.responseText,
364+
toasts[id]
355365
);
356366
console.log(exception); // eslint-disable-line no-console
357367
},

0 commit comments

Comments
 (0)