From 8f2c959cddda7d648e3363d614f6f6d7abed9013 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Fri, 18 Oct 2019 22:48:59 +0200 Subject: [PATCH] Introduce a refresh() function to simplify code / make it more semantic --- src/js/yunohost/controllers/apps.js | 10 ++---- src/js/yunohost/controllers/domains.js | 23 +++---------- src/js/yunohost/controllers/firewall.js | 46 ++++++++++--------------- src/js/yunohost/controllers/services.js | 12 ++----- src/js/yunohost/controllers/tools.js | 43 +++++++++++------------ src/js/yunohost/helpers.js | 6 ++++ 6 files changed, 56 insertions(+), 84 deletions(-) diff --git a/src/js/yunohost/controllers/apps.js b/src/js/yunohost/controllers/apps.js index f7fc2edb..e3bf8432 100644 --- a/src/js/yunohost/controllers/apps.js +++ b/src/js/yunohost/controllers/apps.js @@ -223,11 +223,7 @@ c.confirm( y18n.t('applications'), y18n.t('confirm_app_default'), - function() { - c.api('PUT', '/apps/'+app+'/default', {}, function() { - c.redirect_to('#/apps/'+ app, {slide:false}); - }); - } + function() { c.api('PUT', '/apps/'+app+'/default', {}, function() { c.refresh() }); } ); }); @@ -584,7 +580,7 @@ } else { c.flash('warning', y18n.t('app_install_cancel')); - c.redirect_to('#/apps/install', {slide: false}); + c.refresh(); } }); @@ -624,7 +620,7 @@ }) .fail(function(xhr) { c.flash('fail', y18n.t('app_install_custom_no_manifest')); - c.redirect_to('#/apps/install', {slide:false}); + c.refresh(); }); } ); diff --git a/src/js/yunohost/controllers/domains.js b/src/js/yunohost/controllers/domains.js index d1ab6922..c6e5281c 100644 --- a/src/js/yunohost/controllers/domains.js +++ b/src/js/yunohost/controllers/domains.js @@ -100,21 +100,9 @@ y18n.t('domains'), y18n.t('confirm_change_maindomain'), function() { - var params = { - new_domain: c.params['domain'] - }; - c.api('PUT', '/domains/main', params, function(data) { - c.redirect_to('#/domains'); - }); - - // WTF ... why is this hack needed v_v - - // Wait 15s and refresh the page - var refreshDomain = window.setTimeout(function(){ - c.redirect_to('#/domains'); - }, 15000); + c.api('PUT', '/domains/main', {new_domain: domain}, function() { c.refresh() }); } - ); + ) }); // Configure delete button @@ -257,11 +245,8 @@ c.confirm( y18n.t('certificate'), y18n.t(confirm_key, [domain]), - function(){ - c.api('POST', api_url, {}, function(data) { - c.redirect_to('#/domains/'+domain+'/cert-management', {slide:false}); - }); - }); + function(){ c.api('POST', api_url, {}, function() { c.refresh() }); } + ); }); }); }); diff --git a/src/js/yunohost/controllers/firewall.js b/src/js/yunohost/controllers/firewall.js index f1055638..013d2511 100644 --- a/src/js/yunohost/controllers/firewall.js +++ b/src/js/yunohost/controllers/firewall.js @@ -54,11 +54,7 @@ y18n.t('firewall'), // confirm_upnp_enable and confirm_upnp_disable y18n.t('confirm_upnp_' + action), - function(){ - c.api('GET', '/firewall/upnp', {action: action}, function(data) { - c.redirect_to('#/tools/firewall'); - }); - } + function(){ c.api('GET', '/firewall/upnp', {action: action}, function() { c.refresh() }); } ); }); }); @@ -90,7 +86,7 @@ if (port != parseInt(port) || port < 0 || port > 65535) { c.flash('fail', y18n.t('unknown_argument', [port])); - c.redirect_to('#/tools/firewall', {slide: false}); + c.refresh(); } switch (connection) { @@ -122,29 +118,25 @@ break; default: c.flash('fail', y18n.t('unknown_action', [action])); - c.redirect_to('#/tools/firewall', {slide: false}); + c.refresh(); } - if (method !== null && protocol !== null && port !== null) { - // port: - // protocol: - // - UDP - // - TCP - // - Both - // --ipv4-only: - // --ipv6-only: - // --no-upnp: - var params = { - port : port, - protocol : protocol - }; - c.api(method, '/firewall/port?'+endurl, params, function(data) { - c.redirect_to('#/tools/firewall'); - }); - } - else { - c.redirect_to('#/tools/firewall'); - } + // port: + // protocol: + // - UDP + // - TCP + // - Both + // --ipv4-only: + // --ipv6-only: + // --no-upnp: + var params = { + port : port, + protocol : protocol + }; + + c.api(method, '/firewall/port?'+endurl, params, function() { c.refresh() }); + + return; }); diff --git a/src/js/yunohost/controllers/services.js b/src/js/yunohost/controllers/services.js index c81c43d4..113c34fc 100644 --- a/src/js/yunohost/controllers/services.js +++ b/src/js/yunohost/controllers/services.js @@ -94,17 +94,11 @@ break; default: c.flash('fail', y18n.t('unknown_action', [action])); - c.redirect_to('#/services/'+ service, {slide: false}); + c.refresh(); + return; } - if (method && endurl) { - c.api(method, '/services/'+ endurl, {}, function(data) { - c.redirect_to('#/services/'+ service, {slide: false}); - }); - } - else { - c.redirect_to('#/services/'+ service, {slide: false}); - } + c.api(method, '/services/'+ endurl, {}, function() { c.refresh(); }); }); }); }); diff --git a/src/js/yunohost/controllers/tools.js b/src/js/yunohost/controllers/tools.js index 1daf691e..52c69133 100644 --- a/src/js/yunohost/controllers/tools.js +++ b/src/js/yunohost/controllers/tools.js @@ -26,22 +26,25 @@ }); if ($.isEmptyObject(params)) { c.flash('fail', y18n.t('error_modify_something')); - c.redirect_to('#/tools/adminpw', {slide: false}); - } else if (params['new_password'] !== params['confirm_new_password']) { - c.flash('fail', y18n.t('passwords_dont_match')); - c.redirect_to('#/tools/adminpw', {slide: false}); - } else { - c.api('POST', '/login', { 'password': params['old_password'] }, function(data) { - // Remove useless variable - delete params['old_password']; - delete params['confirm_new_password']; - - // Update password and redirect to the home - c.api('PUT', '/adminpw', params, function(data) { - c.redirect_to('#/logout'); - }); - }, undefined, false); + c.refresh(); + return; } + if (params['new_password'] !== params['confirm_new_password']) { + c.flash('fail', y18n.t('passwords_dont_match')); + c.refresh(); + return; + } + + c.api('POST', '/login', { 'password': params['old_password'] }, function(data) { + // Remove useless variable + delete params['old_password']; + delete params['confirm_new_password']; + + // Update password and redirect to the home + c.api('PUT', '/adminpw', params, function(data) { + c.redirect_to('#/logout'); + }); + }, undefined, false); }); // System update & upgrade @@ -298,14 +301,12 @@ { // FIXME / TODO i18n c.flash('fail', "Some of these migrations require you to acknowledge a disclaimer before running them."); - c.redirect_to('#/tools/migrations', {slide: false}); + c.refresh(); return; } }; - c.api('POST', '/migrations/migrate?accept_disclaimer', {}, function (data) { - c.redirect_to('#/tools/migrations', {slide: false}); - }); + c.api('POST', '/migrations/migrate?accept_disclaimer', {}, function() { c.refresh(); }); }); // Configure buttons 'Skip' @@ -315,9 +316,7 @@ y18n.t('migrations'), y18n.t('confirm_migrations_skip'), function(){ - c.api('POST', '/migrations/migrate?skip&targets=' + migration_id, {}, function(data) { - c.redirect_to('#/tools/migrations', {slide: false}); - }); + c.api('POST', '/migrations/migrate?skip&targets=' + migration_id, {}, function() { c.refresh() }); } ); }); diff --git a/src/js/yunohost/helpers.js b/src/js/yunohost/helpers.js index e5756f72..a43aa396 100644 --- a/src/js/yunohost/helpers.js +++ b/src/js/yunohost/helpers.js @@ -588,7 +588,13 @@ c.app.last_location = c.path; c.app.setLocation(destination); c.app.trigger('location-changed'); + }, + + refresh: function() { + c = this; + c.redirect_to(c.path, {slide: false}); } + }); })();