A platform for community discussion. Free, open, simple.
FIX: Pass category param on /tags/intersection (#23352)
Jarek Radosz1df3ccc903c6
diff --git a/app/assets/javascripts/discourse/app/controllers/tags-intersection.js b/app/assets/javascripts/discourse/app/controllers/tags-intersection.jsnew file mode 100644index 00000000000..9f076ccb317--- /dev/null+++ b/app/assets/javascripts/discourse/app/controllers/tags-intersection.js@@ -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" },+ ]);+ }+}diff --git a/app/assets/javascripts/discourse/app/routes/tag-show.js b/app/assets/javascripts/discourse/app/routes/tag-show.jsindex 676f8682aac..6aac7d60c50 100644--- a/app/assets/javascripts/discourse/app/routes/tag-show.js+++ b/app/assets/javascripts/discourse/app/routes/tag-show.js@@ -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 {@actiondismissRead(operationType) {- const controller = this.controllerFor("tag-show");+ const controller = this.controllerFor(this.controllerName);let options = {tagName: controller.tag?.id,};diff --git a/app/assets/javascripts/discourse/app/routes/tags-intersection.js b/app/assets/javascripts/discourse/app/routes/tags-intersection.jsindex 4d9653d50cf..fdaeb9897ae 100644--- a/app/assets/javascripts/discourse/app/routes/tags-intersection.js+++ b/app/assets/javascripts/discourse/app/routes/tags-intersection.js@@ -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 };+ },+});diff --git a/app/assets/javascripts/discourse/tests/acceptance/tags-intersection-test.js b/app/assets/javascripts/discourse/tests/acceptance/tags-intersection-test.jsindex b12f5321750..73f8505d97d 100644--- a/app/assets/javascripts/discourse/tests/acceptance/tags-intersection-test.js+++ b/app/assets/javascripts/discourse/tests/acceptance/tags-intersection-test.js@@ -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"]);+ });});