Skip to content

A platform for community discussion. Free, open, simple.

UX: Say whose count a badge card shows

Régis Hanol78312dc7e38b
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 5d3a264bc29..a7d9c3040bb 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -6161,9 +6161,6 @@ en:
search_full_page: "Open full page admin search"
badges:
- earned_n_times:
- one: "Earned this badge %{count} time"
- other: "Earned this badge %{count} times"
granted_on: "Granted %{date}"
others_count:
one: "Granted to others %{count} time"
@@ -6180,6 +6177,9 @@ en:
awarded:
one: "%{number} awarded"
other: "%{number} awarded"
+ earned:
+ one: "Earned %{number} time"
+ other: "Earned %{number} times"
select_badge_for_title: Select a badge to use as your title
none: "(none)"
successfully_granted: "Successfully granted %{badge} to %{username}"
diff --git a/frontend/discourse/app/ui-kit/d-badge-card.gjs b/frontend/discourse/app/ui-kit/d-badge-card.gjs
index 6fccdf10b5c..a5889b31872 100644
--- a/frontend/discourse/app/ui-kit/d-badge-card.gjs
+++ b/frontend/discourse/app/ui-kit/d-badge-card.gjs
@@ -4,7 +4,7 @@ import { isEmpty } from "@ember/utils";
import PluginOutlet from "discourse/components/plugin-outlet";
import lazyHash from "discourse/helpers/lazy-hash";
import { emojiUnescape, sanitize } from "discourse/lib/text";
-import { eq, not } from "discourse/truth-helpers";
+import { and, eq, not } from "discourse/truth-helpers";
import DButton from "discourse/ui-kit/d-button";
import dIcon from "discourse/ui-kit/helpers/d-icon";
import dIconOrImage from "discourse/ui-kit/helpers/d-icon-or-image";
@@ -21,13 +21,16 @@ export default class DBadgeCard extends Component {
return filterUser ? `${badge.url}?username=${username}` : badge.url;
}
- get displayCount() {
- const { count, badge } = this.args;
+ get grantedLabel() {
+ const { badge, count } = this.args;
+
if (count == null) {
- return badge.grant_count;
- }
- if (count > 1) {
- return count;
+ const total = badge.grant_count;
+ if (total) {
+ return i18n("badges.awarded", { count: total, number: dNumber(total) });
+ }
+ } else if (count > 1) {
+ return i18n("badges.earned", { count, number: dNumber(count) });
}
}
@@ -40,16 +43,11 @@ export default class DBadgeCard extends Component {
return sanitize(badge.description);
}
- get showFavorite() {
- const { badge } = this.args;
- return ![1, 2, 3, 4].includes(badge.id);
- }
-
get ariaDescribedBy() {
const { badge } = this.args;
const ids = [`badge-summary-${badge.slug}`];
- if (this.displayCount) {
+ if (this.grantedLabel) {
ids.push(`badge-granted-${badge.slug}`);
}
@@ -97,15 +95,9 @@ export default class DBadgeCard extends Component {
<div class="badge-summary" id="badge-summary-{{@badge.slug}}">
{{trustHTML this.summary}}
</div>
- {{#if this.displayCount}}
+ {{#if this.grantedLabel}}
<div class="badge-granted" id="badge-granted-{{@badge.slug}}">
- {{trustHTML
- (i18n
- "badges.awarded"
- count=this.displayCount
- number=(dNumber this.displayCount)
- )
- }}
+ {{trustHTML this.grantedLabel}}
</div>
{{/if}}
</div>
@@ -123,25 +115,20 @@ export default class DBadgeCard extends Component {
{{/if}}
{{#if @canFavorite}}
- {{#if @isFavorite}}
- <DButton
- class="btn-default favorite-btn btn-small"
- @action={{@onFavoriteClick}}
- @icon="star"
- />
- {{else}}
- <DButton
- class="btn-default favorite-btn btn-small"
- @action={{@onFavoriteClick}}
- @disabled={{not @canFavoriteMoreBadges}}
- @icon="far-star"
- @title={{if
+ <DButton
+ class="btn-default favorite-btn btn-small"
+ @action={{@onFavoriteClick}}
+ @disabled={{and (not @isFavorite) (not @canFavoriteMoreBadges)}}
+ @icon={{if @isFavorite "star" "far-star"}}
+ @title={{unless
+ @isFavorite
+ (if
@canFavoriteMoreBadges
"badges.favorite_max_not_reached"
"badges.favorite_max_reached"
- }}
- />
- {{/if}}
+ )
+ }}
+ />
{{/if}}
</div>
</template>
diff --git a/frontend/discourse/tests/integration/ui-kit/d-badge-card-test.gjs b/frontend/discourse/tests/integration/ui-kit/d-badge-card-test.gjs
index 1d7bfe3964b..869e1446ad0 100644
--- a/frontend/discourse/tests/integration/ui-kit/d-badge-card-test.gjs
+++ b/frontend/discourse/tests/integration/ui-kit/d-badge-card-test.gjs
@@ -22,26 +22,69 @@ module("Integration | ui-kit | DBadgeCard", function (hooks) {
);
});
- test("not granted", async function (assert) {
- const badge = { slug: "custombadge" };
+ test("ignores has_badge on the badge itself", async function (assert) {
+ const badge = { slug: "custombadge", has_badge: true };
await render(<template><DBadgeCard @badge={{badge}} /></template>);
- assert.dom(".check-display").doesNotExist();
+ assert
+ .dom(".check-display")
+ .doesNotExist(
+ "the caller says who holds the badge, not the shared record"
+ );
assert
.dom(".badge-link")
.hasAttribute("aria-describedby", "badge-summary-custombadge");
});
- test("ignores has_badge on the badge itself", async function (assert) {
- const badge = { slug: "custombadge", has_badge: true };
+ test("shows the site-wide grant count without @count", async function (assert) {
+ const badge = { slug: "custombadge", grant_count: 1234 };
await render(<template><DBadgeCard @badge={{badge}} /></template>);
+ assert.dom(".badge-granted").hasText("1.2k awarded");
assert
- .dom(".check-display")
- .doesNotExist(
- "the caller says who holds the badge, not the shared record"
+ .dom(".badge-link")
+ .hasAttribute(
+ "aria-describedby",
+ "badge-summary-custombadge badge-granted-custombadge"
);
});
+
+ test("shows the user's own grant count with @count", async function (assert) {
+ const badge = { slug: "custombadge", grant_count: 5 };
+
+ await render(
+ <template><DBadgeCard @badge={{badge}} @count={{1234}} /></template>
+ );
+
+ assert.dom(".badge-granted").hasText("Earned 1.2k times");
+ });
+
+ test("hides the grant count when @count is 1", async function (assert) {
+ const badge = { slug: "custombadge", grant_count: 5 };
+
+ await render(
+ <template><DBadgeCard @badge={{badge}} @count={{1}} /></template>
+ );
+
+ assert.dom(".badge-granted").doesNotExist();
+ assert
+ .dom(".badge-link")
+ .hasAttribute("aria-describedby", "badge-summary-custombadge");
+ });
+
+ test("links to the user-filtered badge page with @filterUser", async function (assert) {
+ const badge = { slug: "custombadge", url: "/badges/1/custombadge" };
+
+ await render(
+ <template>
+ <DBadgeCard @badge={{badge}} @filterUser={{true}} @username="bob" />
+ </template>
+ );
+
+ assert
+ .dom(".badge-link")
+ .hasAttribute("href", "/badges/1/custombadge?username=bob");
+ });
});