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) {
const redirectURL = location.origin;
try {
let resp = await PBS.Async.api2({
let resp = await Proxmox.Async.api2({
url: '/api2/extjs/access/openid/auth-url',
params: {
realm: creds.realm,
@ -44,12 +44,12 @@ Ext.define('PBS.LoginView', {
method: 'POST',
});
window.location = resp.result.data;
} catch (error) {
} catch (response) {
Proxmox.Utils.authClear();
loginForm.unmask();
Ext.MessageBox.alert(
gettext('Error'),
gettext('OpenID redirect failed, please try again') + `<br>${error}`,
gettext('OpenID redirect failed, please try again') + `<br>${response.result.message}`,
);
}
return;
@ -72,7 +72,7 @@ Ext.define('PBS.LoginView', {
sp.set(saveunField.getStateId(), saveunField.getValue());
try {
let resp = await PBS.Async.api2({
let resp = await Proxmox.Async.api2({
url: '/api2/extjs/access/ticket',
params: creds,
method: 'POST',
@ -566,7 +566,7 @@ Ext.define('PBS.login.TfaWindow', {
let reject = view.onReject;
view.close();
return PBS.Async.api2({
return Proxmox.Async.api2({
url: '/api2/extjs/access/ticket',
method: 'POST',
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 {
me.getView().mask(gettext('Please wait...'), 'x-mask-loading');
await PBS.Async.api2({
await Proxmox.Async.api2({
url: `/api2/extjs/access/tfa/${record.id}`,
method: 'DELETE',
params,
});
me.reload();
} catch (error) {
Ext.Msg.alert(gettext('Error'), error);
} catch (response) {
Ext.Msg.alert(gettext('Error'), response.result.message);
} finally {
me.getView().unmask();
}

View File

@ -37,7 +37,7 @@ Ext.define('PBS.TapeManagement.BackupOverview', {
loadContent: async function() {
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',
});
let data = {};
@ -122,7 +122,7 @@ Ext.define('PBS.TapeManagement.BackupOverview', {
const media_set = node.data.text;
try {
let list = await PBS.Async.api2({
let list = await Proxmox.Async.api2({
method: 'GET',
url: `/api2/extjs/tape/media/content`,
params: {
@ -224,9 +224,9 @@ Ext.define('PBS.TapeManagement.BackupOverview', {
node.set('datastores', storeNameList);
Proxmox.Utils.setErrorMask(view, false);
node.expand();
} catch (error) {
} catch (response) {
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 drive = record.data.name;
try {
await PBS.Async.api2({
await Proxmox.Async.api2({
method: 'POST',
timeout: 5*60*1000,
url: `/api2/extjs/tape/drive/${encodeURIComponent(drive)}/unload`,
});
} catch (error) {
Ext.Msg.alert(gettext('Error'), error);
} catch (response) {
Ext.Msg.alert(gettext('Error'), response.result.message);
}
me.reload();
},
@ -478,7 +478,7 @@ Ext.define('PBS.TapeManagement.ChangerStatus', {
Proxmox.Utils.setErrorMask(view, true);
Proxmox.Utils.setErrorMask(me.lookup('content'));
}
let status_fut = PBS.Async.api2({
let status_fut = Proxmox.Async.api2({
timeout: 5*60*1000,
method: 'GET',
url: `/api2/extjs/tape/changer/${encodeURIComponent(changer)}/status`,
@ -486,12 +486,12 @@ Ext.define('PBS.TapeManagement.ChangerStatus', {
cache: use_cache,
},
});
let drives_fut = PBS.Async.api2({
let drives_fut = Proxmox.Async.api2({
timeout: 5*60*1000,
url: `/api2/extjs/tape/drive?changer=${encodeURIComponent(changer)}`,
});
let tapes_fut = PBS.Async.api2({
let tapes_fut = Proxmox.Async.api2({
timeout: 5*60*1000,
url: '/api2/extjs/tape/media/list',
method: 'GET',
@ -589,7 +589,7 @@ Ext.define('PBS.TapeManagement.ChangerStatus', {
Proxmox.Utils.setErrorMask(view);
}
Proxmox.Utils.setErrorMask(me.lookup('content'));
} catch (err) {
} catch (response) {
if (!view || view.isDestroyed) {
return;
}
@ -597,7 +597,7 @@ Ext.define('PBS.TapeManagement.ChangerStatus', {
if (!use_cache) {
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);

View File

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

View File

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