diff --git a/app/assets/javascripts/admin/addon/components/modal/badge-preview.hbs b/app/assets/javascripts/admin/addon/components/modal/badge-preview.hbs new file mode 100644 index 00000000000..4f0e2e1492a --- /dev/null +++ b/app/assets/javascripts/admin/addon/components/modal/badge-preview.hbs @@ -0,0 +1,53 @@ + + <:body> + {{#if @model.badge.errors}} +

{{i18n + "admin.badges.preview.sql_error_header" + }}

+
{{@model.badge.errors}}
+ {{else}} +

+ {{#if this.count}} + {{html-safe + (i18n "admin.badges.preview.grant_count" count=@model.badge.count) + }} + {{else}} + {{html-safe (i18n "admin.badges.preview.no_grant_count")}} + {{/if}} +

+ + {{#if this.countWarning}} +
+

+ {{d-icon "exclamation-triangle"}} + {{i18n "admin.badges.preview.bad_count_warning.header"}} +

+

+ {{i18n "admin.badges.preview.bad_count_warning.text"}} +

+
+ {{/if}} + + {{#if this.sample}} +

+ {{i18n "admin.badges.preview.sample"}} +

+ + {{/if}} + + {{#if this.hasQueryPlan}} +
+ {{html-safe this.queryPlanHtml}} +
+ {{/if}} + {{/if}} + +
\ No newline at end of file diff --git a/app/assets/javascripts/admin/addon/components/modal/badge-preview.js b/app/assets/javascripts/admin/addon/components/modal/badge-preview.js new file mode 100644 index 00000000000..f46a8611d62 --- /dev/null +++ b/app/assets/javascripts/admin/addon/components/modal/badge-preview.js @@ -0,0 +1,53 @@ +import Component from "@glimmer/component"; +import I18n from "I18n"; +import { escapeExpression } from "discourse/lib/utilities"; + +export default class BadgePreview extends Component { + get processedSample() { + return this.args.model.badge.sample.map((grant) => { + let i18nKey = "admin.badges.preview.grant.with"; + const i18nParams = { username: escapeExpression(grant.username) }; + + if (grant.post_id) { + i18nKey += "_post"; + i18nParams.link = ` + ${escapeExpression(grant.title)} + `; + } + + if (grant.granted_at) { + i18nKey += "_time"; + i18nParams.time = escapeExpression( + moment(grant.granted_at).format(I18n.t("dates.long_with_year")) + ); + } + + return I18n.t(i18nKey, i18nParams); + }); + } + + get countWarning() { + if (this.args.model.badge.grant_count <= 10) { + return ( + this.args.model.badge.sample.length !== + this.args.model.badge.grant_count + ); + } else { + return this.args.model.badge.sample.length !== 10; + } + } + + get hasQueryPlan() { + return !!this.args.model.badge.query_plan; + } + + get queryPlanHtml() { + let output = `
`;
+    this.args.model.badge.query_plan.forEach((linehash) => {
+      output += escapeExpression(linehash["QUERY PLAN"]);
+      output += "
"; + }); + output += "
"; + return output; + } +} diff --git a/app/assets/javascripts/admin/addon/controllers/modals/admin-badge-preview.js b/app/assets/javascripts/admin/addon/controllers/modals/admin-badge-preview.js deleted file mode 100644 index f86c04e56e2..00000000000 --- a/app/assets/javascripts/admin/addon/controllers/modals/admin-badge-preview.js +++ /dev/null @@ -1,60 +0,0 @@ -import { alias, map } from "@ember/object/computed"; -import Controller from "@ember/controller"; -import I18n from "I18n"; -import discourseComputed from "discourse-common/utils/decorators"; -import { escapeExpression } from "discourse/lib/utilities"; - -export default class AdminBadgePreviewController extends Controller { - @alias("model.sample") sample; - @alias("model.errors") errors; - @alias("model.grant_count") count; - - @map("model.sample", (grant) => { - let i18nKey = "admin.badges.preview.grant.with"; - const i18nParams = { username: escapeExpression(grant.username) }; - - if (grant.post_id) { - i18nKey += "_post"; - i18nParams.link = ` - ${escapeExpression(grant.title)} - `; - } - - if (grant.granted_at) { - i18nKey += "_time"; - i18nParams.time = escapeExpression( - moment(grant.granted_at).format(I18n.t("dates.long_with_year")) - ); - } - - return I18n.t(i18nKey, i18nParams); - }) - processedSample; - - @discourseComputed("count", "sample.length") - countWarning(count, sampleLength) { - if (count <= 10) { - return sampleLength !== count; - } else { - return sampleLength !== 10; - } - } - - @discourseComputed("model.query_plan") - hasQueryPlan(queryPlan) { - return !!queryPlan; - } - - @discourseComputed("model.query_plan") - queryPlanHtml(queryPlan) { - let output = `
`;
-
-    queryPlan.forEach((linehash) => {
-      output += escapeExpression(linehash["QUERY PLAN"]);
-      output += "
"; - }); - - output += "
"; - return output; - } -} diff --git a/app/assets/javascripts/admin/addon/routes/admin-badges/show.js b/app/assets/javascripts/admin/addon/routes/admin-badges/show.js index cb7fa0d4ea4..b19daf69faa 100644 --- a/app/assets/javascripts/admin/addon/routes/admin-badges/show.js +++ b/app/assets/javascripts/admin/addon/routes/admin-badges/show.js @@ -3,9 +3,9 @@ import I18n from "I18n"; import Route from "@ember/routing/route"; import { ajax } from "discourse/lib/ajax"; import { action, get } from "@ember/object"; -import showModal from "discourse/lib/show-modal"; import { inject as service } from "@ember/service"; import EditBadgeGroupingsModal from "../../components/modal/edit-badge-groupings"; +import BadgePreviewModal from "../../components/modal/badge-preview"; export default class AdminBadgesShowRoute extends Route { @service dialog; @@ -53,26 +53,25 @@ export default class AdminBadgesShowRoute extends Route { } @action - preview(badge, explain) { - badge.set("preview_loading", true); - ajax("/admin/badges/preview.json", { - type: "POST", - data: { - sql: badge.get("query"), - target_posts: !!badge.get("target_posts"), - trigger: badge.get("trigger"), - explain, - }, - }) - .then(function (model) { - badge.set("preview_loading", false); - showModal("admin-badge-preview", { model, admin: true }); - }) - .catch(function (error) { - badge.set("preview_loading", false); - // eslint-disable-next-line no-console - console.error(error); - this.dialog.alert("Network error"); + async preview(badge, explain) { + try { + badge.set("preview_loading", true); + const model = await ajax("/admin/badges/preview.json", { + type: "POST", + data: { + sql: badge.get("query"), + target_posts: !!badge.get("target_posts"), + trigger: badge.get("trigger"), + explain, + }, }); + badge.set("preview_loading", false); + this.modal.show(BadgePreviewModal, { model: { badge: model } }); + } catch (e) { + badge.set("preview_loading", false); + // eslint-disable-next-line no-console + console.error(e); + this.dialog.alert("Network error"); + } } } diff --git a/app/assets/javascripts/admin/addon/templates/modal/admin-badge-preview.hbs b/app/assets/javascripts/admin/addon/templates/modal/admin-badge-preview.hbs deleted file mode 100644 index 950de88cc92..00000000000 --- a/app/assets/javascripts/admin/addon/templates/modal/admin-badge-preview.hbs +++ /dev/null @@ -1,58 +0,0 @@ - - {{#if this.errors}} -

{{i18n "admin.badges.preview.sql_error_header"}}

- -
{{this.errors}}
- - {{!-- - TODO we want some help pages for this, link to those instead -

- {{i18n "admin.badges.preview.error_help"}} -

- - --}} - - {{else}} -

- {{#if this.count}} - {{html-safe (i18n "admin.badges.preview.grant_count" count=this.count)}} - {{else}} - {{html-safe (i18n "admin.badges.preview.no_grant_count")}} - {{/if}} -

- - {{#if this.countWarning}} -
-

- {{d-icon "exclamation-triangle"}} - {{i18n "admin.badges.preview.bad_count_warning.header"}} -

-

- {{i18n "admin.badges.preview.bad_count_warning.text"}} -

-
- {{/if}} - - {{#if this.sample}} -

- {{i18n "admin.badges.preview.sample"}} -

- - {{/if}} - - {{#if this.hasQueryPlan}} -
- {{html-safe this.queryPlanHtml}} -
- {{/if}} - {{/if}} -
\ No newline at end of file diff --git a/app/assets/javascripts/discourse/app/services/modal.js b/app/assets/javascripts/discourse/app/services/modal.js index f1d5f0e7d51..acd1b175119 100644 --- a/app/assets/javascripts/discourse/app/services/modal.js +++ b/app/assets/javascripts/discourse/app/services/modal.js @@ -46,7 +46,6 @@ const KNOWN_LEGACY_MODALS = [ "topic-summary", "user-status", "admin-penalize-user", - "admin-badge-preview", "admin-reseed", "admin-theme-item", "admin-color-scheme-select-base",