From 8e1f2af66242fa5653553129539fae4111bca0f1 Mon Sep 17 00:00:00 2001 From: Niko Date: Sun, 27 Feb 2022 13:12:26 +0100 Subject: [PATCH] Create module "api" for various repeated api method calls inside UI --- web/src/app/js/api.js | 19 +++++++++++++++++++ web/src/app/js/app.js | 1 + web/src/app/js/pages/meet.js | 31 +++++++++++-------------------- 3 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 web/src/app/js/api.js diff --git a/web/src/app/js/api.js b/web/src/app/js/api.js new file mode 100644 index 0000000..8fe5417 --- /dev/null +++ b/web/src/app/js/api.js @@ -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); + } + }); + }, + }, +}; diff --git a/web/src/app/js/app.js b/web/src/app/js/app.js index c3615ef..3c76bcd 100644 --- a/web/src/app/js/app.js +++ b/web/src/app/js/app.js @@ -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', diff --git a/web/src/app/js/pages/meet.js b/web/src/app/js/pages/meet.js index 91ba2a2..3d0b764 100644 --- a/web/src/app/js/pages/meet.js +++ b/web/src/app/js/pages/meet.js @@ -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() {