FEATURE: support category slug when creating new topic via URL

This commit is contained in:
Arpit Jalan 2018-10-10 16:50:59 +05:30
parent d35bce96ab
commit c84415b1f2
1 changed files with 32 additions and 17 deletions

View File

@ -11,24 +11,17 @@ export default Discourse.Route.extend({
category = Category.findById(category_id); category = Category.findById(category_id);
} else if (transition.queryParams.category) { } else if (transition.queryParams.category) {
const splitCategory = transition.queryParams.category.split("/"); const splitCategory = transition.queryParams.category.split("/");
category = this._getCategory(
if (!splitCategory[1]) { splitCategory[0],
category = this.site splitCategory[1],
.get("categories") "nameLower"
.findBy("nameLower", splitCategory[0].toLowerCase());
} else {
const categories = this.site.get("categories");
const mainCategory = categories.findBy(
"nameLower",
splitCategory[0].toLowerCase()
); );
category = categories.find(function(item) { if (!category) {
return ( category = this._getCategory(
item && splitCategory[0],
item.get("nameLower") === splitCategory[1].toLowerCase() && splitCategory[1],
item.get("parent_category_id") === mainCategory.id "slug"
); );
});
} }
if (category) { if (category) {
@ -86,5 +79,27 @@ export default Discourse.Route.extend({
self.replaceWith("login"); self.replaceWith("login");
} }
} }
},
_getCategory(mainCategory, subCategory, type) {
let category;
if (!subCategory) {
category = this.site
.get("categories")
.findBy(type, mainCategory.toLowerCase());
} else {
const categories = this.site.get("categories");
const main = categories.findBy(type, mainCategory.toLowerCase());
if (main) {
category = categories.find(function(item) {
return (
item &&
item.get(type) === subCategory.toLowerCase() &&
item.get("parent_category_id") === main.id
);
});
}
}
return category;
} }
}); });