DEV: Convert ajax functions to async (#273)

This commit is contained in:
Isaac Janzen 2024-01-02 12:38:22 -07:00 committed by GitHub
parent 2a596e3fd9
commit 70458df7cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 94 additions and 92 deletions

View File

@ -104,20 +104,19 @@ export default class PluginsExplorerController extends Controller {
} }
@action @action
save() { async save() {
this.loading = true; try {
this.loading = true;
await this.selectedItem.save();
return this.selectedItem this.dirty = false;
.save() this.editingName = false;
.then(() => { } catch (error) {
this.dirty = false; popupAjaxError(error);
this.editingName = false; throw error;
}) } finally {
.catch((x) => { this.loading = false;
popupAjaxError(x); }
throw x;
})
.finally(() => (this.loading = false));
} }
@action @action
@ -212,28 +211,28 @@ export default class PluginsExplorerController extends Controller {
} }
@action @action
import(files) { async import(files) {
this.loading = true; try {
const file = files[0]; this.loading = true;
this._importQuery(file) const file = files[0];
.then((record) => this.addCreatedRecord(record)) const record = await this._importQuery(file);
.catch((e) => { this.addCreatedRecord(record);
if (e.jqXHR) { } catch (e) {
popupAjaxError(e); if (e.jqXHR) {
} else if (e instanceof SyntaxError) { popupAjaxError(e);
this.dialog.alert(I18n.t("explorer.import.unparseable_json")); } else if (e instanceof SyntaxError) {
} else if (e instanceof TypeError) { this.dialog.alert(I18n.t("explorer.import.unparseable_json"));
this.dialog.alert(I18n.t("explorer.import.wrong_json")); } else if (e instanceof TypeError) {
} else { this.dialog.alert(I18n.t("explorer.import.wrong_json"));
this.dialog.alert(I18n.t("errors.desc.unknown")); } else {
// eslint-disable-next-line no-console this.dialog.alert(I18n.t("errors.desc.unknown"));
console.error(e); // eslint-disable-next-line no-console
} console.error(e);
}) }
.finally(() => { } finally {
this.loading = false; this.loading = false;
this.dirty = true; this.dirty = true;
}); }
} }
@action @action
@ -258,14 +257,12 @@ export default class PluginsExplorerController extends Controller {
@action @action
goHome() { goHome() {
this.setProperties({ this.order = null;
order: null, this.showResults = false;
showResults: false, this.selectedQueryId = null;
selectedQueryId: null, this.params = null;
params: null, this.sortByProperty = "last_run_at";
sortByProperty: "last_run_at", this.sortDescending = true;
sortDescending: true,
});
this.router.transitionTo({ queryParams: { id: null, params: null } }); this.router.transitionTo({ queryParams: { id: null, params: null } });
} }
@ -290,64 +287,69 @@ export default class PluginsExplorerController extends Controller {
} }
@action @action
create() { async create() {
const name = this.newQueryName.trim(); try {
this.setProperties({ const name = this.newQueryName.trim();
loading: true, this.loading = true;
showCreate: false, this.showCreate = false;
}); const result = await this.store.createRecord("query", { name }).save();
this.store this.addCreatedRecord(result.target);
.createRecord("query", { name }) } catch (error) {
.save() popupAjaxError(error);
.then((result) => this.addCreatedRecord(result.target)) } finally {
.catch(popupAjaxError) this.loading = false;
.finally(() => { this.dirty = true;
this.loading = false; }
this.dirty = true;
});
} }
@action @action
discard() { async discard() {
this.loading = true; try {
this.store this.loading = true;
.find("query", this.selectedItem.id) const result = await this.store.find("query", this.selectedItem.id);
.then((result) => { this.selectedItem.setProperties(
this.selectedItem.setProperties( result.getProperties(Query.updatePropertyNames)
result.getProperties(Query.updatePropertyNames) );
); if (
if ( !this.selectedItem.group_ids ||
!this.selectedItem.group_ids || !Array.isArray(this.selectedItem.group_ids)
!Array.isArray(this.selectedItem.group_ids) ) {
) { this.selectedItem.set("group_ids", []);
this.selectedItem.set("group_ids", []); }
} this.dirty = false;
this.dirty = false; } catch (error) {
}) popupAjaxError(error);
.catch(popupAjaxError) } finally {
.finally(() => (this.loading = false)); this.loading = false;
}
} }
@action @action
destroyQuery() { async destroyQuery() {
this.loading = true; try {
this.showResults = false; this.loading = true;
this.store this.showResults = false;
.destroyRecord("query", this.selectedItem) await this.store.destroyRecord("query", this.selectedItem);
.then(() => this.selectedItem.set("destroyed", true)) this.selectedItem.set("destroyed", true);
.catch(popupAjaxError) } catch (error) {
.finally(() => (this.loading = false)); popupAjaxError(error);
} finally {
this.loading = false;
}
} }
@action @action
recover() { async recover() {
this.loading = true; try {
this.showResults = true; this.loading = true;
this.selectedItem this.showResults = true;
.save() await this.selectedItem.save();
.then(() => this.selectedItem.set("destroyed", false)) this.selectedItem.set("destroyed", false);
.catch(popupAjaxError) } catch (error) {
.finally(() => (this.loading = false)); popupAjaxError(error);
} finally {
this.loading = false;
}
} }
@action @action