From dd3046327684ce98f0c3a7430f5d22fd35f1d9ca Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Fri, 5 Jul 2024 22:25:42 +0200 Subject: [PATCH] DEV: Use `async/await` in admin route `model` fns (#27745) --- .../admin/addon/routes/admin-backups-index.js | 7 ++-- .../admin/addon/routes/admin-backups.js | 16 +++---- .../admin-customize-themes-show-schema.js | 14 +++---- .../admin/addon/routes/admin-emojis.js | 9 ++-- .../routes/admin-plugins-show-settings.js | 8 ++-- .../admin/addon/routes/admin-plugins-show.js | 7 ++-- .../admin/addon/routes/admin-plugins.js | 7 ++-- .../addon/routes/admin-search-logs-index.js | 8 ++-- .../addon/routes/admin-search-logs-term.js | 42 +++++++++---------- .../addon/routes/admin-site-text-edit.js | 10 ++--- 10 files changed, 61 insertions(+), 67 deletions(-) diff --git a/app/assets/javascripts/admin/addon/routes/admin-backups-index.js b/app/assets/javascripts/admin/addon/routes/admin-backups-index.js index 545a2ba592e..fafbab34437 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-backups-index.js +++ b/app/assets/javascripts/admin/addon/routes/admin-backups-index.js @@ -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 diff --git a/app/assets/javascripts/admin/addon/routes/admin-backups.js b/app/assets/javascripts/admin/addon/routes/admin-backups.js index 0ceb00e01a9..e625de8d25c 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-backups.js +++ b/app/assets/javascripts/admin/addon/routes/admin-backups.js @@ -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 diff --git a/app/assets/javascripts/admin/addon/routes/admin-customize-themes-show-schema.js b/app/assets/javascripts/admin/addon/routes/admin-customize-themes-show-schema.js index 2bfe91c525f..5cff32ce875 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-customize-themes-show-schema.js +++ b/app/assets/javascripts/admin/addon/routes/admin-customize-themes-show-schema.js @@ -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() { diff --git a/app/assets/javascripts/admin/addon/routes/admin-emojis.js b/app/assets/javascripts/admin/addon/routes/admin-emojis.js index f1f6432e716..0fa0095cb4d 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-emojis.js +++ b/app/assets/javascripts/admin/addon/routes/admin-emojis.js @@ -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)); } } diff --git a/app/assets/javascripts/admin/addon/routes/admin-plugins-show-settings.js b/app/assets/javascripts/admin/addon/routes/admin-plugins-show-settings.js index 6fe418e06f5..8c76769729c 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-plugins-show-settings.js +++ b/app/assets/javascripts/admin/addon/routes/admin-plugins-show-settings.js @@ -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 }; } } diff --git a/app/assets/javascripts/admin/addon/routes/admin-plugins-show.js b/app/assets/javascripts/admin/addon/routes/admin-plugins-show.js index 4411592acee..d9f57f1c0eb 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-plugins-show.js +++ b/app/assets/javascripts/admin/addon/routes/admin-plugins-show.js @@ -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) { diff --git a/app/assets/javascripts/admin/addon/routes/admin-plugins.js b/app/assets/javascripts/admin/addon/routes/admin-plugins.js index a46f59460c9..289231101cb 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-plugins.js +++ b/app/assets/javascripts/admin/addon/routes/admin-plugins.js @@ -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)); } } diff --git a/app/assets/javascripts/admin/addon/routes/admin-search-logs-index.js b/app/assets/javascripts/admin/addon/routes/admin-search-logs-index.js index 03de7453f4a..3701dcfa3f7 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-search-logs-index.js +++ b/app/assets/javascripts/admin/addon/routes/admin-search-logs-index.js @@ -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) { diff --git a/app/assets/javascripts/admin/addon/routes/admin-search-logs-term.js b/app/assets/javascripts/admin/addon/routes/admin-search-logs-term.js index 25bf963898c..74311140869 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-search-logs-term.js +++ b/app/assets/javascripts/admin/addon/routes/admin-search-logs-term.js @@ -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) { diff --git a/app/assets/javascripts/admin/addon/routes/admin-site-text-edit.js b/app/assets/javascripts/admin/addon/routes/admin-site-text-edit.js index 4fae723f898..817f91f389d 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-site-text-edit.js +++ b/app/assets/javascripts/admin/addon/routes/admin-site-text-edit.js @@ -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) {