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\/[^\/]+?$/); handle('pages.quiz', /^quiz\/[^\/]+?$/);
} }
app.script('api');
app.template.loadMany([ app.template.loadMany([
'toast.confirm', 'toast.info', 'toast.confirm', 'toast.info',
'toast.warn', 'toast.error', 'toast.warn', 'toast.error',

View File

@ -181,15 +181,7 @@ app.pages.meet = {
const current = app.pages.meet.dataCurrent(); const current = app.pages.meet.dataCurrent();
if (current === undefined || current === null) if (current === undefined || current === null)
return app.toast.error(s('app.error.unknown_error')); return app.toast.error(s('app.error.unknown_error'));
http.post(`/api/v1/accounts/block?url=${current.account.url}`, {}, function(json) { api.account.block(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() { setTimeout(function() {
for (var i = app.pages.meet.data.length-1; i >= 0; i--) for (var i = app.pages.meet.data.length-1; i >= 0; i--)
if (app.pages.meet.data[i].account.url === current.account.url) if (app.pages.meet.data[i].account.url === current.account.url)
@ -200,7 +192,6 @@ app.pages.meet = {
app.pages.meet.carousel._current]; app.pages.meet.carousel._current];
animateTimeout(elem, 'slide-from-right', 100); animateTimeout(elem, 'slide-from-right', 100);
}, app.toast.DEFAULT_MILLIS); }, app.toast.DEFAULT_MILLIS);
}
}); });
}, },
quiz: function() { quiz: function() {