FIX: Pass category param on /tags/intersection (#23352)

This commit is contained in:
Jarek Radosz 2023-10-13 02:42:41 +02:00 committed by GitHub
parent f33b60ba17
commit 1df3ccc903
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 13 deletions

View File

@ -0,0 +1,13 @@
import { queryParams } from "discourse/controllers/discovery-sortable";
import TagShowController from "discourse/controllers/tag-show";
export default class TagsIntersectionController extends TagShowController {
constructor() {
super(...arguments);
this.set("queryParams", [
...Object.keys(queryParams),
{ categoryParam: "category" },
]);
}
}

View File

@ -41,7 +41,7 @@ export default class TagShowRoute extends DiscourseRoute {
}
beforeModel() {
const controller = this.controllerFor("tag.show");
const controller = this.controllerFor(this.controllerName);
controller.setProperties({
loading: true,
showInfo: false,
@ -74,7 +74,7 @@ export default class TagShowRoute extends DiscourseRoute {
);
}
const category = params.category_slug_path_with_id
let category = params.category_slug_path_with_id
? Category.findBySlugPathWithID(params.category_slug_path_with_id)
: null;
const filteredQueryParams = filterQueryParams(
@ -96,6 +96,13 @@ export default class TagShowRoute extends DiscourseRoute {
filter += `/${tagId}/l/${topicFilter}`;
} else if (additionalTags) {
filter = `tags/intersection/${tagId}/${additionalTags.join("/")}`;
if (transition.to.queryParams["category"]) {
filteredQueryParams["category"] = transition.to.queryParams["category"];
category = Category.findBySlugPathWithID(
transition.to.queryParams["category"]
);
}
} else {
filter = `tag/${tagId}/l/${topicFilter}`;
}
@ -150,7 +157,7 @@ export default class TagShowRoute extends DiscourseRoute {
setupController(controller, model) {
const noSubcategories = this.noSubcategories;
this.controllerFor("tag.show").setProperties({
controller.setProperties({
model: model.tag,
...model,
period: model.list.for_period,
@ -179,7 +186,7 @@ export default class TagShowRoute extends DiscourseRoute {
const filterText = I18n.t(
`filters.${this.navMode.replace("/", ".")}.title`
);
const controller = this.controllerFor("tag.show");
const controller = this.controllerFor(this.controllerName);
if (controller.tag?.id) {
if (controller.category) {
@ -223,7 +230,7 @@ export default class TagShowRoute extends DiscourseRoute {
if (this.currentUser?.has_topic_draft) {
this.openTopicDraft();
} else {
const controller = this.controllerFor("tag.show");
const controller = this.controllerFor(this.controllerName);
this.composer
.open({
categoryId: controller.category?.id,
@ -248,7 +255,7 @@ export default class TagShowRoute extends DiscourseRoute {
@action
dismissRead(operationType) {
const controller = this.controllerFor("tag-show");
const controller = this.controllerFor(this.controllerName);
let options = {
tagName: controller.tag?.id,
};

View File

@ -1,9 +1,20 @@
import { queryParams } from "discourse/controllers/discovery-sortable";
import { buildTagRoute } from "discourse/routes/tag-show";
export default buildTagRoute();
// The tags-intersection route is exactly the same as the tags-show route, but the wildcard at the
// end of the route (*additional_tags) will cause a match when query parameters are present,
// breaking all other tags-show routes. Ember thinks the query params are addition tags and should
// be handled by the intersection logic. Defining tags-intersection as something separate avoids
// that confusion.
export default buildTagRoute().extend({
controllerName: "tags.intersection",
init() {
this._super(...arguments);
// The only difference is support for `category` query param.
// Other routes include category in the route path.
this.set("queryParams", { ...queryParams });
this.queryParams["categoryParam"] = { replace: true, refreshModel: true };
},
});

View File

@ -1,5 +1,6 @@
import { click, visit } from "@ember/test-helpers";
import { test } from "qunit";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
import {
acceptance,
exists,
@ -11,9 +12,9 @@ acceptance("Tags intersection", function (needs) {
needs.site({ can_tag_topics: true });
needs.settings({ tagging_enabled: true });
needs.pretender((server, helper) => {
server.get("/tags/intersection/first/second.json", () => {
return helper.response({
test("Populate tags when creating new topic", async function (assert) {
pretender.get("/tags/intersection/first/second.json", () => {
return response({
users: [],
primary_groups: [],
topic_list: {
@ -27,9 +28,7 @@ acceptance("Tags intersection", function (needs) {
},
});
});
});
test("Populate tags when creating new topic", async function (assert) {
await visit("/tags/intersection/first/second");
await click("#create-topic");
@ -40,4 +39,28 @@ acceptance("Tags intersection", function (needs) {
"populates the tags when clicking 'New topic'"
);
});
test("correctly passes the category filter", async function (assert) {
pretender.get("/tags/intersection/sour/tangy.json", (request) => {
assert.deepEqual(request.queryParams, { category: "fruits" });
assert.step("request");
return response({
users: [],
primary_groups: [],
topic_list: {
can_create_topic: true,
draft_key: "new_topic",
topics: [{ id: 16, posters: [] }],
tags: [
{ id: 1, name: "second", topic_count: 1 },
{ id: 2, name: "first", topic_count: 1 },
],
},
});
});
await visit("/tags/intersection/sour/tangy?category=fruits");
assert.verifySteps(["request"]);
});
});