Create module "api" for various repeated api method calls inside UI

This commit is contained in:
Niko 2022-02-27 13:12:26 +01:00
parent ccf5954091
commit 8e1f2af662
3 changed files with 31 additions and 20 deletions

19
web/src/app/js/api.js Normal file
View File

@ -0,0 +1,19 @@
var api = {
account: {
block: function(url, cback) {
cback = cback || function() {};
http.post(`/api/v1/accounts/block?url=${url}`, {}, function(json) {
if (json === undefined) return;
if (json.warn !== undefined)
app.toast.warn(app.strings.app.warn[json.warn]);
else if (json.error !== undefined)
app.toast.error(app.strings.app.error[json.error]);
else if (json.result !== undefined && json.result === 1) {
const n = randomNum(0, 3);
app.toast.warn(app.strings.app.ok['discarded_success_'+n]);
cback(json);
}
});
},
},
};

View File

@ -558,6 +558,7 @@ window.onload = function(e) {
handle('pages.quiz', /^quiz\/[^\/]+?$/);
}
app.script('api');
app.template.loadMany([
'toast.confirm', 'toast.info',
'toast.warn', 'toast.error',

View File

@ -181,26 +181,17 @@ app.pages.meet = {
const current = app.pages.meet.dataCurrent();
if (current === undefined || current === null)
return app.toast.error(s('app.error.unknown_error'));
http.post(`/api/v1/accounts/block?url=${current.account.url}`, {}, function(json) {
if (json === undefined) return;
if (json.warn !== undefined)
app.toast.warn(app.strings.app.warn[json.warn]);
else if (json.error !== undefined)
app.toast.error(app.strings.app.error[json.error]);
else if (json.result !== undefined && json.result === 1) {
const n = randomNum(0, 3);
app.toast.warn(app.strings.app.ok['discarded_success_'+n]);
setTimeout(function() {
for (var i = app.pages.meet.data.length-1; i >= 0; i--)
if (app.pages.meet.data[i].account.url === current.account.url)
app.pages.meet.data.splice(i, 1);
app.pages.meet.carousel.set(0);
app.pages.meet.paint();
const elem = app.pages.meet.carousel._elements()[
app.pages.meet.carousel._current];
animateTimeout(elem, 'slide-from-right', 100);
}, app.toast.DEFAULT_MILLIS);
}
api.account.block(current.account.url, function(json) {
setTimeout(function() {
for (var i = app.pages.meet.data.length-1; i >= 0; i--)
if (app.pages.meet.data[i].account.url === current.account.url)
app.pages.meet.data.splice(i, 1);
app.pages.meet.carousel.set(0);
app.pages.meet.paint();
const elem = app.pages.meet.carousel._elements()[
app.pages.meet.carousel._current];
animateTimeout(elem, 'slide-from-right', 100);
}, app.toast.DEFAULT_MILLIS);
});
},
quiz: function() {