FIX: attempts to use params from addDiscoveryQueryParam (#8007)

This commit will for example allow this:

```
api.addDiscoveryQueryParam("my_param", { persist: true });
```

If you page is forum.foo.bar/?my_param=1, when clicking on an "unread" link for example this query string will be kept.
This commit is contained in:
Joffrey JAFFEUX 2019-08-14 19:56:02 +02:00 committed by GitHub
parent 88359b0f16
commit a5542768ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 2 deletions

View File

@ -1,6 +1,9 @@
import computed from "ember-addons/ember-computed-decorators"; import computed from "ember-addons/ember-computed-decorators";
export default Ember.Component.extend({ export default Ember.Component.extend({
router: Ember.inject.service(),
persistedQueryParams: null,
tagName: "", tagName: "",
@computed("category") @computed("category")
@ -27,9 +30,25 @@ export default Ember.Component.extend({
if (filterMode.indexOf("top/") === 0) { if (filterMode.indexOf("top/") === 0) {
filterMode = filterMode.replace("top/", ""); filterMode = filterMode.replace("top/", "");
} }
let params;
const currentRouteQueryParams = this.get("router.currentRoute.queryParams");
if (this.persistedQueryParams && currentRouteQueryParams) {
const currentKeys = Object.keys(currentRouteQueryParams);
const discoveryKeys = Object.keys(this.persistedQueryParams);
const supportedKeys = currentKeys.filter(
i => discoveryKeys.indexOf(i) > 0
);
params = supportedKeys.reduce((object, key) => {
object[key] = currentRouteQueryParams[key];
return object;
}, {});
}
return Discourse.NavItem.buildList(category, { return Discourse.NavItem.buildList(category, {
filterMode, filterMode,
noSubcategories noSubcategories,
persistedQueryParams: params
}); });
} }
}); });

View File

@ -26,15 +26,28 @@ export default Ember.Component.extend(
const content = this.content; const content = this.content;
let href = content.get("href"); let href = content.get("href");
let queryParams = [];
// Include the category id if the option is present // Include the category id if the option is present
if (content.get("includeCategoryId")) { if (content.get("includeCategoryId")) {
let categoryId = this.get("category.id"); let categoryId = this.get("category.id");
if (categoryId) { if (categoryId) {
href += `?category_id=${categoryId}`; queryParams.push(`category_id=${categoryId}`);
} }
} }
// ensures we keep discovery query params added through plugin api
if (content.persistedQueryParams) {
Object.keys(content.persistedQueryParams).forEach(key => {
const value = content.persistedQueryParams[key];
queryParams.push(`${key}=${value}`);
});
}
if (queryParams.length) {
href += `?${queryParams.join("&")}`;
}
if ( if (
!this.active && !this.active &&
this.currentUser && this.currentUser &&

View File

@ -1,3 +1,5 @@
import DiscourseNavigation from "discourse/components/d-navigation";
// Just add query params here to have them automatically passed to topic list filters. // Just add query params here to have them automatically passed to topic list filters.
export const queryParams = { export const queryParams = {
order: { replace: true, refreshModel: true }, order: { replace: true, refreshModel: true },
@ -31,6 +33,12 @@ export const addDiscoveryQueryParam = function(p, opts) {
cOpts[p] = Ember.computed.alias(`discoveryTopics.${p}`); cOpts[p] = Ember.computed.alias(`discoveryTopics.${p}`);
cOpts["queryParams"] = Object.keys(queryParams); cOpts["queryParams"] = Object.keys(queryParams);
Controller.reopen(cOpts); Controller.reopen(cOpts);
if (opts && opts.persisted) {
DiscourseNavigation.reopen({
persistedQueryParams: queryParams
});
}
}; };
export default Controller; export default Controller;

View File

@ -136,6 +136,9 @@ NavItem.reopenClass({
if (opts.category) { if (opts.category) {
args.category = opts.category; args.category = opts.category;
} }
if (opts.persistedQueryParams) {
args.persistedQueryParams = opts.persistedQueryParams;
}
if (opts.noSubcategories) { if (opts.noSubcategories) {
args.noSubcategories = true; args.noSubcategories = true;
} }