REFACTOR: admin-badge-preview (#7017)

This commit is contained in:
Joffrey JAFFEUX 2019-02-19 09:30:52 +01:00 committed by GitHub
parent 3bf61fca66
commit 2c6bf184bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 39 deletions

View File

@ -1,3 +1,4 @@
import { default as computed } from "ember-addons/ember-computed-decorators";
import { escapeExpression } from "discourse/lib/utilities"; import { escapeExpression } from "discourse/lib/utilities";
export default Ember.Controller.extend({ export default Ember.Controller.extend({
@ -5,43 +6,42 @@ export default Ember.Controller.extend({
errors: Ember.computed.alias("model.errors"), errors: Ember.computed.alias("model.errors"),
count: Ember.computed.alias("model.grant_count"), count: Ember.computed.alias("model.grant_count"),
count_warning: function() { @computed("count", "sample.length")
if (this.get("count") <= 10) { countWarning(count, sampleLength) {
return this.get("sample.length") !== this.get("count"); if (count <= 10) {
return sampleLength !== count;
} else { } else {
return this.get("sample.length") !== 10; return sampleLength !== 10;
} }
}.property("count", "sample.length"), },
has_query_plan: function() { @computed("model.query_plan")
return !!this.get("model.query_plan"); hasQueryPlan(queryPlan) {
}.property("model.query_plan"), return !!queryPlan;
},
query_plan_html: function() { @computed("model.query_plan")
var raw = this.get("model.query_plan"), queryPlanHtml(queryPlan) {
returned = "<pre class='badge-query-plan'>"; let output = `<pre class="badge-query-plan">`;
raw.forEach(linehash => { queryPlan.forEach(linehash => {
returned += escapeExpression(linehash["QUERY PLAN"]); output += escapeExpression(linehash["QUERY PLAN"]);
returned += "<br>"; output += "<br>";
}); });
returned += "</pre>"; output += "</pre>";
return returned; return output;
}.property("model.query_plan"), },
processed_sample: Ember.computed.map("model.sample", function(grant) { processedSample: Ember.computed.map("model.sample", grant => {
var i18nKey = "admin.badges.preview.grant.with", let i18nKey = "admin.badges.preview.grant.with";
i18nParams = { username: escapeExpression(grant.username) }; const i18nParams = { username: escapeExpression(grant.username) };
if (grant.post_id) { if (grant.post_id) {
i18nKey += "_post"; i18nKey += "_post";
i18nParams.link = i18nParams.link = `<a href="/p/${grant.post_id}" data-auto-route="true">
"<a href='/p/" + ${Handlebars.Utils.escapeExpression(grant.title)}
grant.post_id + </a>`;
"' data-auto-route='true'>" +
Handlebars.Utils.escapeExpression(grant.title) +
"</a>";
} }
if (grant.granted_at) { if (grant.granted_at) {

View File

@ -1,49 +1,55 @@
{{#d-modal-body title="admin.badges.preview.modal_title" class="badge-query-preview"}} {{#d-modal-body title="admin.badges.preview.modal_title" class="badge-query-preview"}}
{{#if errors}} {{#if errors}}
<p class="error-header">{{i18n 'admin.badges.preview.sql_error_header'}}</p> <p class="error-header">{{i18n "admin.badges.preview.sql_error_header"}}</p>
<pre class="badge-errors">{{errors}}</pre> <pre class="badge-errors">{{errors}}</pre>
<!-- <!--
TODO we want some help pages for this, link to those instead TODO we want some help pages for this, link to those instead
<p> <p>
{{i18n 'admin.badges.preview.error_help'}} {{i18n "admin.badges.preview.error_help"}}
</p> </p>
<ul> <ul>
<li><a href="https://meta.discourse.org/t/triggered-custom-badge-queries/19336">https://meta.discourse.org/t/triggered-custom-badge-queries/19336</a></li> <li><a href="https://meta.discourse.org/t/triggered-custom-badge-queries/19336">https://meta.discourse.org/t/triggered-custom-badge-queries/19336</a></li>
</ul> </ul>
--> -->
{{else}} {{else}}
<p class="grant-count"> <p class="grant-count">
{{#if count}} {{#if count}}
{{{i18n 'admin.badges.preview.grant_count' count=count}}} {{{i18n "admin.badges.preview.grant_count" count=count}}}
{{else}} {{else}}
{{{i18n 'admin.badges.preview.no_grant_count'}}} {{{i18n "admin.badges.preview.no_grant_count"}}}
{{/if}} {{/if}}
</p> </p>
{{#if count_warning}} {{#if countWarning}}
<div class="count-warning"> <div class="count-warning">
<p class="heading">{{d-icon "warning"}} {{i18n 'admin.badges.preview.bad_count_warning.header'}}</p> <p class="heading">
<p class="body">{{i18n 'admin.badges.preview.bad_count_warning.text'}}</p> {{d-icon "warning"}}
{{i18n "admin.badges.preview.bad_count_warning.header"}}
</p>
<p class="body">
{{i18n "admin.badges.preview.bad_count_warning.text"}}
</p>
</div> </div>
{{/if}} {{/if}}
{{#if sample}} {{#if sample}}
<p class="sample"> <p class="sample">
{{i18n 'admin.badges.preview.sample'}} {{i18n "admin.badges.preview.sample"}}
</p> </p>
<ul> <ul>
{{#each processed_sample as |html|}} {{#each processedSample as |html|}}
<li>{{{html}}}</li> <li>{{{html}}}</li>
{{/each}} {{/each}}
</ul> </ul>
{{/if}} {{/if}}
{{#if has_query_plan}} {{#if hasQueryPlan}}
<div class="badge-query-plan"> <div class="badge-query-plan">
{{{query_plan_html}}} {{{queryPlanHtml}}}
</div> </div>
{{/if}} {{/if}}
{{/if}} {{/if}}
{{/d-modal-body}} {{/d-modal-body}}