DEV: Use `async/await` in admin route `model` fns (#27745)

This commit is contained in:
Jarek Radosz 2024-07-05 22:25:42 +02:00 committed by GitHub
parent 005f623c42
commit dd30463276
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 61 additions and 67 deletions

View File

@ -11,10 +11,9 @@ export default class AdminBackupsIndexRoute extends Route {
this.messageBus.unsubscribe("/admin/backups", this.onMessage);
}
model() {
return Backup.find().then((backups) =>
backups.map((backup) => Backup.create(backup))
);
async model() {
const backups = await Backup.find();
return backups.map((backup) => Backup.create(backup));
}
@bind

View File

@ -28,16 +28,16 @@ export default class AdminBackupsRoute extends DiscourseRoute {
this.messageBus.unsubscribe(LOG_CHANNEL, this.onMessage);
}
model() {
return PreloadStore.getAndRemove("operations_status", () =>
async model() {
const status = await PreloadStore.getAndRemove("operations_status", () =>
ajax("/admin/backups/status.json")
).then((status) =>
BackupStatus.create({
isOperationRunning: status.is_operation_running,
canRollback: status.can_rollback,
allowRestore: status.allow_restore,
})
);
BackupStatus.create({
isOperationRunning: status.is_operation_running,
canRollback: status.can_rollback,
allowRestore: status.allow_restore,
});
}
@bind

View File

@ -1,16 +1,16 @@
import Route from "@ember/routing/route";
export default class AdminCustomizeThemesShowSchemaRoute extends Route {
model(params) {
async model(params) {
const theme = this.modelFor("adminCustomizeThemesShow");
const setting = theme.settings.findBy("setting", params.setting_name);
return setting.loadMetadata(theme.id).then(() => {
return {
theme,
setting,
};
});
await setting.loadMetadata(theme.id);
return {
theme,
setting,
};
}
setupController() {

View File

@ -3,11 +3,8 @@ import { ajax } from "discourse/lib/ajax";
import DiscourseRoute from "discourse/routes/discourse";
export default class AdminEmojisRoute extends DiscourseRoute {
model() {
return ajax("/admin/customize/emojis.json").then(function (emojis) {
return emojis.map(function (emoji) {
return EmberObject.create(emoji);
});
});
async model() {
const emojis = await ajax("/admin/customize/emojis.json");
return emojis.map((emoji) => EmberObject.create(emoji));
}
}

View File

@ -9,10 +9,10 @@ export default class AdminPluginsShowSettingsRoute extends Route {
filter: { replace: true },
};
model(params) {
async model(params) {
const plugin = this.modelFor("adminPlugins.show");
return SiteSetting.findAll({ plugin: plugin.name }).then((settings) => {
return { plugin, settings, initialFilter: params.filter };
});
const settings = await SiteSetting.findAll({ plugin: plugin.name });
return { plugin, settings, initialFilter: params.filter };
}
}

View File

@ -8,11 +8,10 @@ export default class AdminPluginsShowRoute extends Route {
@service router;
@service adminPluginNavManager;
model(params) {
async model(params) {
const pluginId = sanitize(params.plugin_id).substring(0, 100);
return ajax(`/admin/plugins/${pluginId}.json`).then((plugin) => {
return AdminPlugin.create(plugin);
});
const pluginAttrs = await ajax(`/admin/plugins/${pluginId}.json`);
return AdminPlugin.create(pluginAttrs);
}
afterModel(model) {

View File

@ -5,9 +5,8 @@ import AdminPlugin from "admin/models/admin-plugin";
export default class AdminPluginsRoute extends Route {
@service router;
model() {
return this.store
.findAll("plugin")
.then((plugins) => plugins.map((plugin) => AdminPlugin.create(plugin)));
async model() {
const plugins = await this.store.findAll("plugin");
return plugins.map((plugin) => AdminPlugin.create(plugin));
}
}

View File

@ -8,13 +8,13 @@ export default class AdminSearchLogsIndexRoute extends DiscourseRoute {
searchType: { refreshModel: true },
};
model(params) {
async model(params) {
this._params = params;
return ajax("/admin/logs/search_logs.json", {
const searchLogs = await ajax("/admin/logs/search_logs.json", {
data: { period: params.period, search_type: params.searchType },
}).then((search_logs) => {
return search_logs.map((sl) => EmberObject.create(sl));
});
return searchLogs.map((log) => EmberObject.create(log));
}
setupController(controller, model) {

View File

@ -11,35 +11,35 @@ export default class AdminSearchLogsTermRoute extends DiscourseRoute {
searchType: { refreshModel: true },
};
model(params) {
async model(params) {
this._params = params;
return ajax(`/admin/logs/search_logs/term.json`, {
const json = await ajax(`/admin/logs/search_logs/term.json`, {
data: {
period: params.period,
search_type: params.searchType,
term: params.term,
},
}).then(async (json) => {
// Add zero values for missing dates
if (json.term.data.length > 0) {
const startDate =
json.term.period === "all"
? moment(json.term.data[0].x).format("YYYY-MM-DD")
: moment(json.term.start_date).format("YYYY-MM-DD");
const endDate = moment(json.term.end_date).format("YYYY-MM-DD");
json.term.data = fillMissingDates(json.term.data, startDate, endDate);
}
if (json.term.search_result) {
json.term.search_result = await translateResults(
json.term.search_result
);
}
const model = EmberObject.create({ type: "search_log_term" });
model.setProperties(json.term);
return model;
});
// Add zero values for missing dates
if (json.term.data.length > 0) {
const startDate =
json.term.period === "all"
? moment(json.term.data[0].x).format("YYYY-MM-DD")
: moment(json.term.start_date).format("YYYY-MM-DD");
const endDate = moment(json.term.end_date).format("YYYY-MM-DD");
json.term.data = fillMissingDates(json.term.data, startDate, endDate);
}
if (json.term.search_result) {
json.term.search_result = await translateResults(json.term.search_result);
}
const model = EmberObject.create({ type: "search_log_term" });
model.setProperties(json.term);
return model;
}
setupController(controller, model) {

View File

@ -6,12 +6,12 @@ export default class AdminSiteTextEditRoute extends Route {
locale: { replace: true },
};
model(params) {
return ajax(
async model(params) {
const result = await ajax(
`/admin/customize/site_texts/${params.id}?locale=${params.locale}`
).then((result) => {
return this.store.createRecord("site-text", result.site_text);
});
);
return this.store.createRecord("site-text", result.site_text);
}
setupController(controller, siteText) {