Skip to content

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

FIX: Show only the filtered user's data on a filtered badge page

Régis Hanol06cb61e0cf45
diff --git a/app/assets/stylesheets/common/base/user-badges.scss b/app/assets/stylesheets/common/base/user-badges.scss
index 0204fb3b6ce..570e2018bb1 100644
--- a/app/assets/stylesheets/common/base/user-badges.scss
+++ b/app/assets/stylesheets/common/base/user-badges.scss
@@ -316,12 +316,6 @@
margin-top: 1em;
gap: 1em;
- .badge-grant-info {
- &.hidden {
- display: none;
- }
- }
-
.grant-info-item {
margin-bottom: 1em;
color: var(--primary-med-or-secondary-med);
diff --git a/frontend/discourse/app/controllers/badges/show.js b/frontend/discourse/app/controllers/badges/show.js
index 95b35c9d7eb..f9783b64cc8 100644
--- a/frontend/discourse/app/controllers/badges/show.js
+++ b/frontend/discourse/app/controllers/badges/show.js
@@ -1,6 +1,6 @@
import { tracked } from "@glimmer/tracking";
-import Controller, { inject as controller } from "@ember/controller";
-import EmberObject, { action, computed } from "@ember/object";
+import Controller from "@ember/controller";
+import EmberObject, { action } from "@ember/object";
import { uniqueItemsFromArray } from "discourse/lib/array-tools";
import { autoTrackedArray } from "discourse/lib/tracked-tools";
import Badge from "discourse/models/badge";
@@ -8,21 +8,16 @@ import UserBadge from "discourse/models/user-badge";
import { i18n } from "discourse-i18n";
export default class ShowController extends Controller {
- @controller application;
-
+ @tracked model;
+ @tracked username = null;
@tracked loadingMore = false;
@tracked noMoreBadges = false;
-
+ @tracked showTitlePicker = false;
+ @tracked userBadgesAll;
@tracked userBadgesGrantCount;
@autoTrackedArray userBadges = null;
queryParams = ["username"];
- hiddenSetTitle = true;
-
- @computed("userBadgesAll")
- get filteredList() {
- return this.userBadgesAll.filter((item) => item.badge.allow_title);
- }
get selectableUserBadges() {
return [
@@ -30,39 +25,39 @@ export default class ShowController extends Controller {
id: 0,
badge: Badge.create({ name: i18n("badges.none") }),
}),
- ...uniqueItemsFromArray(this.filteredList, "badge.name"),
+ ...uniqueItemsFromArray(
+ this.userBadgesAll.filter((ub) => ub.badge.allow_title),
+ "badge.name"
+ ),
];
}
- @computed("username")
- get user() {
- if (this.username) {
- return this.userBadges[0].get("user");
- }
+ get othersCount() {
+ return this.model.grant_count - this.userBadgesGrantCount;
}
- get grantCount() {
- return this.username ? this.userBadgesGrantCount : this.model.grant_count;
+ get currentUserHasBadge() {
+ return !!this.userBadgesAll?.some((ub) => ub.badge?.id === this.model.id);
}
- @computed("model.grant_count", "userBadgesGrantCount")
- get othersCount() {
- return this.model?.grant_count - this.userBadgesGrantCount;
+ get isFilteredToOtherUser() {
+ return (
+ !!this.username && this.userBadges?.[0]?.user?.id !== this.currentUser?.id
+ );
}
- @computed("model.id", "userBadgesAll")
- get currentUserHasBadge() {
- return !!this.userBadgesAll?.some(
- (userBadge) => userBadge.badge?.id === this.model?.id
- );
+ get hasBadge() {
+ return this.username
+ ? this.userBadgesGrantCount > 0
+ : this.currentUserHasBadge;
}
- @computed("model.allow_title", "currentUserHasBadge")
get canSelectTitle() {
return (
this.siteSettings.enable_badges &&
- this.model?.allow_title &&
- this.currentUserHasBadge
+ this.model.allow_title &&
+ this.hasBadge &&
+ !this.isFilteredToOtherUser
);
}
@@ -70,12 +65,12 @@ export default class ShowController extends Controller {
if (this.noMoreBadges) {
return false;
}
- return this.grantCount > (this.userBadges?.length || 0);
+ const total = this.userBadgesGrantCount ?? this.model.grant_count;
+ return total > (this.userBadges?.length || 0);
}
- @computed("user", "model.grant_count")
get canShowOthers() {
- return !!this.user && this.model?.grant_count > 1;
+ return !!this.username && this.othersCount > 0;
}
@action
@@ -88,7 +83,7 @@ export default class ShowController extends Controller {
try {
const userBadges = this.userBadges;
- const result = await UserBadge.findByBadgeId(this.get("model.id"), {
+ const result = await UserBadge.findByBadgeId(this.model.id, {
offset: userBadges.length,
username: this.username,
});
@@ -103,7 +98,7 @@ export default class ShowController extends Controller {
}
@action
- toggleSetUserTitle() {
- return this.toggleProperty("hiddenSetTitle");
+ toggleTitlePicker() {
+ this.showTitlePicker = !this.showTitlePicker;
}
}
diff --git a/frontend/discourse/app/routes/badges/show.js b/frontend/discourse/app/routes/badges/show.js
index 5e1ac69b540..4dcf2faa952 100644
--- a/frontend/discourse/app/routes/badges/show.js
+++ b/frontend/discourse/app/routes/badges/show.js
@@ -64,4 +64,8 @@ export default class BadgesShow extends DiscourseRoute {
controller.set("userBadgesGrantCount", this.userBadgesGrant.grant_count);
controller.set("userBadgesAll", this.userBadgesAll);
}
+
+ resetController(controller) {
+ controller.showTitlePicker = false;
+ }
}
diff --git a/frontend/discourse/app/templates/badges/show.gjs b/frontend/discourse/app/templates/badges/show.gjs
index 4ed8c0e2082..42cf7b61db6 100644
--- a/frontend/discourse/app/templates/badges/show.gjs
+++ b/frontend/discourse/app/templates/badges/show.gjs
@@ -4,6 +4,7 @@ import BadgeTitle from "discourse/components/badge-title";
import PluginOutlet from "discourse/components/plugin-outlet";
import hideApplicationFooter from "discourse/helpers/hide-application-footer";
import lazyHash from "discourse/helpers/lazy-hash";
+import { and } from "discourse/truth-helpers";
import DBadgeCard from "discourse/ui-kit/d-badge-card";
import DButton from "discourse/ui-kit/d-button";
import DConditionalLoadingSpinner from "discourse/ui-kit/d-conditional-loading-spinner";
@@ -28,19 +29,32 @@ export default <template>
<DBadgeCard
@badge={{@controller.model}}
@count={{@controller.userBadgesGrantCount}}
- @granted={{@controller.currentUserHasBadge}}
+ @granted={{@controller.hasBadge}}
@size="large"
/>
- <div
- class="badge-grant-info {{if @controller.hiddenSetTitle '' 'hidden'}}"
- >
- <div>
+ {{#if (and @controller.canSelectTitle @controller.showTitlePicker)}}
+ <div class="badge-set-title">
+ <PluginOutlet
+ @name="selectable-user-badges"
+ @outletArgs={{lazyHash
+ }}
+ >
+ <BadgeTitle
+ @closeAction={{@controller.toggleTitlePicker}}
+ @selectableUserBadges={{@controller.selectableUserBadges}}
+ />
+ </PluginOutlet>
+ </div>
+ {{else}}
+ <div class="badge-grant-info">
{{#if @controller.canSelectTitle}}
<div class="grant-info-item">
{{i18n "badges.allow_title"}}
<DButton
class="btn-default pad-left"
- @action={{@controller.toggleSetUserTitle}}
+ @action={{@controller.toggleTitlePicker}}
@icon="pencil"
/>
</div>
@@ -51,25 +65,6 @@ export default <template>
</div>
{{/if}}
</div>
- </div>
-
- {{#if @controller.canSelectTitle}}
- <div
- class="badge-set-title {{if @controller.hiddenSetTitle 'hidden' ''}}"
- >
- <PluginOutlet
- @name="selectable-user-badges"
- @outletArgs={{lazyHash
- }}
- >
- <BadgeTitle
- @closeAction={{@controller.toggleSetUserTitle}}
- @selectableUserBadges={{@controller.selectableUserBadges}}
- />
- </PluginOutlet>
- </div>
{{/if}}
</div>
diff --git a/frontend/discourse/tests/acceptance/badges-test.js b/frontend/discourse/tests/acceptance/badges-test.js
index 318725d2573..841ae744b07 100644
--- a/frontend/discourse/tests/acceptance/badges-test.js
+++ b/frontend/discourse/tests/acceptance/badges-test.js
@@ -22,12 +22,20 @@ acceptance("Badges", function (needs) {
await visit("/badges/9/autobiographer");
assert.dom(".badge-card").exists("has the badge in the listing");
+ assert
+ .dom(".show-badge-details .badge-granted")
+ .includesText("awarded", "shows the site-wide grant count");
assert.dom(".user-info").exists("has the list of users with that badge");
assert.dom(".badge-card:nth-of-type(1) script").doesNotExist();
});
- test("shows correct badge titles to choose from", async function (assert) {
+ test("marks a held badge as granted and offers it as a title", async function (assert) {
await visit("/badges/50/custombadge");
+
+ assert
+ .dom(".show-badge-details .check-display")
+ .exists("the badge is marked as granted");
+
await click(".badge-grant-info .btn-default");
const availableBadgeTitles = selectKit(".badge-set-title .select-kit");
@@ -38,16 +46,48 @@ acceptance("Badges", function (needs) {
"CustomBadge"
);
});
+});
- test("marks a badge the current user holds as granted", async function (assert) {
- await visit("/badges/50/custombadge");
+acceptance("Badges - filtered by username", function (needs) {
+ needs.user();
+
+ needs.pretender((server, helper) => {
+ server.get("/user_badges.json", ({ queryParams: { username } }) => {
+ const { user_badges, ...payload } = cloneJSON(
+ badgesFixtures["/user_badges.json"]
+ );
+ const user = payload.users.find((u) => u.username === username);
+ const userBadges = user_badges.filter((ub) => ub.user_id === user.id);
+ payload.user_badge_info = {
+ grant_count: userBadges.length,
+ username,
+ user_badges: userBadges,
+ };
+ return helper.response(payload);
+ });
+ });
+
+ test("shows another user's data, not the current user's", async function (assert) {
+ await visit("/badges/9/autobiographer?username=allard");
assert
.dom(".show-badge-details .check-display")
- .exists("the badge is marked as granted");
+ .exists("the filtered user holds the badge, even if the viewer doesn't");
+ assert
+ .dom("#show-others-with-badge-link")
+ .exists("links to everyone else with the badge");
+
+ await visit("/badges/50/custombadge?username=allard");
+
assert
.dom(".badge-grant-info .btn-default")
- .exists("the badge title can be selected");
+ .doesNotExist("the viewer's title picker is hidden");
+ });
+
+ test("keeps the title picker when filtered to the current user", async function (assert) {
+ await visit("/badges/50/custombadge?username=eviltrout");
+
+ assert.dom(".badge-grant-info .btn-default").exists();
});
});