ui: use Async tools from widget toolkit

The api2 one passes the whole response (for more flexibility) on
reject, so we need to adapt to that.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2021-07-09 16:53:11 +02:00
parent 681e096448
commit 3006d70ebe
7 changed files with 26 additions and 50 deletions

View File

@ -35,7 +35,7 @@ Ext.define('PBS.LoginView', {
if (this.getViewModel().data.openid === true) { if (this.getViewModel().data.openid === true) {
const redirectURL = location.origin; const redirectURL = location.origin;
try { try {
let resp = await PBS.Async.api2({ let resp = await Proxmox.Async.api2({
url: '/api2/extjs/access/openid/auth-url', url: '/api2/extjs/access/openid/auth-url',
params: { params: {
realm: creds.realm, realm: creds.realm,
@ -44,12 +44,12 @@ Ext.define('PBS.LoginView', {
method: 'POST', method: 'POST',
}); });
window.location = resp.result.data; window.location = resp.result.data;
} catch (error) { } catch (response) {
Proxmox.Utils.authClear(); Proxmox.Utils.authClear();
loginForm.unmask(); loginForm.unmask();
Ext.MessageBox.alert( Ext.MessageBox.alert(
gettext('Error'), gettext('Error'),
gettext('OpenID redirect failed, please try again') + `<br>${error}`, gettext('OpenID redirect failed, please try again') + `<br>${response.result.message}`,
); );
} }
return; return;
@ -72,7 +72,7 @@ Ext.define('PBS.LoginView', {
sp.set(saveunField.getStateId(), saveunField.getValue()); sp.set(saveunField.getStateId(), saveunField.getValue());
try { try {
let resp = await PBS.Async.api2({ let resp = await Proxmox.Async.api2({
url: '/api2/extjs/access/ticket', url: '/api2/extjs/access/ticket',
params: creds, params: creds,
method: 'POST', method: 'POST',
@ -566,7 +566,7 @@ Ext.define('PBS.login.TfaWindow', {
let reject = view.onReject; let reject = view.onReject;
view.close(); view.close();
return PBS.Async.api2({ return Proxmox.Async.api2({
url: '/api2/extjs/access/ticket', url: '/api2/extjs/access/ticket',
method: 'POST', method: 'POST',
params, params,

View File

@ -641,28 +641,3 @@ Ext.define('PBS.Utils', {
}, },
}); });
Ext.define('PBS.Async', {
singleton: true,
// Returns a Promise resolving to the result of an `API2Request`.
api2: function(reqOpts) {
return new Promise((resolve, reject) => {
delete reqOpts.callback; // not allowed in this api
reqOpts.success = response => resolve(response);
reqOpts.failure = response => {
if (response.result && response.result.message) {
reject(response.result.message);
} else {
reject("api call failed");
}
};
Proxmox.Utils.API2Request(reqOpts);
});
},
// Delay for a number of milliseconds.
sleep: function(millis) {
return new Promise((resolve, _reject) => setTimeout(resolve, millis));
},
});

View File

@ -161,14 +161,14 @@ Ext.define('PBS.config.TfaView', {
try { try {
me.getView().mask(gettext('Please wait...'), 'x-mask-loading'); me.getView().mask(gettext('Please wait...'), 'x-mask-loading');
await PBS.Async.api2({ await Proxmox.Async.api2({
url: `/api2/extjs/access/tfa/${record.id}`, url: `/api2/extjs/access/tfa/${record.id}`,
method: 'DELETE', method: 'DELETE',
params, params,
}); });
me.reload(); me.reload();
} catch (error) { } catch (response) {
Ext.Msg.alert(gettext('Error'), error); Ext.Msg.alert(gettext('Error'), response.result.message);
} finally { } finally {
me.getView().unmask(); me.getView().unmask();
} }

View File

@ -37,7 +37,7 @@ Ext.define('PBS.TapeManagement.BackupOverview', {
loadContent: async function() { loadContent: async function() {
let me = this; let me = this;
let content_response = await PBS.Async.api2({ let content_response = await Proxmox.Async.api2({
url: '/api2/extjs/tape/media/list?update-status=false', url: '/api2/extjs/tape/media/list?update-status=false',
}); });
let data = {}; let data = {};
@ -122,7 +122,7 @@ Ext.define('PBS.TapeManagement.BackupOverview', {
const media_set = node.data.text; const media_set = node.data.text;
try { try {
let list = await PBS.Async.api2({ let list = await Proxmox.Async.api2({
method: 'GET', method: 'GET',
url: `/api2/extjs/tape/media/content`, url: `/api2/extjs/tape/media/content`,
params: { params: {
@ -224,9 +224,9 @@ Ext.define('PBS.TapeManagement.BackupOverview', {
node.set('datastores', storeNameList); node.set('datastores', storeNameList);
Proxmox.Utils.setErrorMask(view, false); Proxmox.Utils.setErrorMask(view, false);
node.expand(); node.expand();
} catch (error) { } catch (response) {
Proxmox.Utils.setErrorMask(view, false); Proxmox.Utils.setErrorMask(view, false);
Ext.Msg.alert('Error', error.toString()); Ext.Msg.alert('Error', response.result.message.toString());
} }
}, },

View File

@ -257,13 +257,13 @@ Ext.define('PBS.TapeManagement.ChangerStatus', {
let me = this; let me = this;
let drive = record.data.name; let drive = record.data.name;
try { try {
await PBS.Async.api2({ await Proxmox.Async.api2({
method: 'POST', method: 'POST',
timeout: 5*60*1000, timeout: 5*60*1000,
url: `/api2/extjs/tape/drive/${encodeURIComponent(drive)}/unload`, url: `/api2/extjs/tape/drive/${encodeURIComponent(drive)}/unload`,
}); });
} catch (error) { } catch (response) {
Ext.Msg.alert(gettext('Error'), error); Ext.Msg.alert(gettext('Error'), response.result.message);
} }
me.reload(); me.reload();
}, },
@ -478,7 +478,7 @@ Ext.define('PBS.TapeManagement.ChangerStatus', {
Proxmox.Utils.setErrorMask(view, true); Proxmox.Utils.setErrorMask(view, true);
Proxmox.Utils.setErrorMask(me.lookup('content')); Proxmox.Utils.setErrorMask(me.lookup('content'));
} }
let status_fut = PBS.Async.api2({ let status_fut = Proxmox.Async.api2({
timeout: 5*60*1000, timeout: 5*60*1000,
method: 'GET', method: 'GET',
url: `/api2/extjs/tape/changer/${encodeURIComponent(changer)}/status`, url: `/api2/extjs/tape/changer/${encodeURIComponent(changer)}/status`,
@ -486,12 +486,12 @@ Ext.define('PBS.TapeManagement.ChangerStatus', {
cache: use_cache, cache: use_cache,
}, },
}); });
let drives_fut = PBS.Async.api2({ let drives_fut = Proxmox.Async.api2({
timeout: 5*60*1000, timeout: 5*60*1000,
url: `/api2/extjs/tape/drive?changer=${encodeURIComponent(changer)}`, url: `/api2/extjs/tape/drive?changer=${encodeURIComponent(changer)}`,
}); });
let tapes_fut = PBS.Async.api2({ let tapes_fut = Proxmox.Async.api2({
timeout: 5*60*1000, timeout: 5*60*1000,
url: '/api2/extjs/tape/media/list', url: '/api2/extjs/tape/media/list',
method: 'GET', method: 'GET',
@ -589,7 +589,7 @@ Ext.define('PBS.TapeManagement.ChangerStatus', {
Proxmox.Utils.setErrorMask(view); Proxmox.Utils.setErrorMask(view);
} }
Proxmox.Utils.setErrorMask(me.lookup('content')); Proxmox.Utils.setErrorMask(me.lookup('content'));
} catch (err) { } catch (response) {
if (!view || view.isDestroyed) { if (!view || view.isDestroyed) {
return; return;
} }
@ -597,7 +597,7 @@ Ext.define('PBS.TapeManagement.ChangerStatus', {
if (!use_cache) { if (!use_cache) {
Proxmox.Utils.setErrorMask(view); Proxmox.Utils.setErrorMask(view);
} }
Proxmox.Utils.setErrorMask(me.lookup('content'), err.toString()); Proxmox.Utils.setErrorMask(me.lookup('content'), response.result.message.toString());
} }
me.scheduleReload(5000); me.scheduleReload(5000);

View File

@ -51,12 +51,12 @@ Ext.define('PBS.window.AddTfaRecovery', {
let view = me.getView(); let view = me.getView();
try { try {
await PBS.Async.api2({ await Proxmox.Async.api2({
url: `${view.url}/${userid}/recovery`, url: `${view.url}/${userid}/recovery`,
method: 'GET', method: 'GET',
}); });
return true; return true;
} catch (_ex) { } catch (_response) {
return false; return false;
} }
}, },

View File

@ -62,7 +62,7 @@ Ext.define('PBS.window.AddWebauthn', {
me.getView().mask(gettext('Please wait...'), 'x-mask-loading'); me.getView().mask(gettext('Please wait...'), 'x-mask-loading');
try { try {
let register_response = await PBS.Async.api2({ let register_response = await Proxmox.Async.api2({
url: `/api2/extjs/access/tfa/${userid}`, url: `/api2/extjs/access/tfa/${userid}`,
method: 'POST', method: 'POST',
params: values, params: values,
@ -135,12 +135,13 @@ Ext.define('PBS.window.AddWebauthn', {
params.password = values.password; params.password = values.password;
} }
await PBS.Async.api2({ await Proxmox.Async.api2({
url: `/api2/extjs/access/tfa/${userid}`, url: `/api2/extjs/access/tfa/${userid}`,
method: 'POST', method: 'POST',
params, params,
}); });
} catch (error) { } catch (response) {
let error = response.result.message;
console.error(error); // for debugging if it's not displayable... console.error(error); // for debugging if it's not displayable...
Ext.Msg.alert(gettext('Error'), error); Ext.Msg.alert(gettext('Error'), error);
} }