REFACTOR: new-topic route (#7639)

This commit is contained in:
Joffrey JAFFEUX 2019-05-29 16:46:58 +02:00 committed by GitHub
parent ba66d6fd82
commit c77bc525cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 41 deletions

View File

@ -1,21 +1,22 @@
import Category from "discourse/models/category"; import Category from "discourse/models/category";
export default Discourse.Route.extend({ export default Discourse.Route.extend({
beforeModel: function(transition) { beforeModel(transition) {
const self = this; if (this.currentUser) {
if (Discourse.User.current()) { let category, categoryId;
let category, category_id;
if (transition.to.queryParams.category_id) { if (transition.to.queryParams.category_id) {
category_id = transition.to.queryParams.category_id; categoryId = transition.to.queryParams.category_id;
category = Category.findById(category_id); category = Category.findById(categoryId);
} else if (transition.to.queryParams.category) { } else if (transition.to.queryParams.category) {
const splitCategory = transition.to.queryParams.category.split("/"); const splitCategory = transition.to.queryParams.category.split("/");
category = this._getCategory( category = this._getCategory(
splitCategory[0], splitCategory[0],
splitCategory[1], splitCategory[1],
"nameLower" "nameLower"
); );
if (!category) { if (!category) {
category = this._getCategory( category = this._getCategory(
splitCategory[0], splitCategory[0],
@ -25,76 +26,71 @@ export default Discourse.Route.extend({
} }
if (category) { if (category) {
category_id = category.get("id"); categoryId = category.id;
} }
} }
if (Boolean(category)) { if (Boolean(category)) {
let route = "discovery.parentCategory"; let route = "discovery.parentCategory";
let params = { category, slug: category.get("slug") }; let params = { category, slug: category.slug };
if (category.get("parentCategory")) { if (category.parentCategory) {
route = "discovery.category"; route = "discovery.category";
params = { params = {
category, category,
parentSlug: category.get("parentCategory.slug"), parentSlug: category.parentCategory.slug,
slug: category.get("slug") slug: category.slug
}; };
} }
self.replaceWith(route, params).then(function(e) { this.replaceWith(route, params).then(e => {
if (self.controllerFor("navigation/category").get("canCreateTopic")) { if (this.controllerFor("navigation/category").canCreateTopic) {
Ember.run.next(function() { this._sendTransition(e, transition, categoryId);
e.send(
"createNewTopicViaParams",
transition.to.queryParams.title,
transition.to.queryParams.body,
category_id,
transition.to.queryParams.tags
);
});
} }
}); });
} else { } else {
self.replaceWith("discovery.latest").then(function(e) { this.replaceWith("discovery.latest").then(e => {
if (self.controllerFor("navigation/default").get("canCreateTopic")) { if (this.controllerFor("navigation/default").canCreateTopic) {
Ember.run.next(function() { this._sendTransition(e, transition);
e.send(
"createNewTopicViaParams",
transition.to.queryParams.title,
transition.to.queryParams.body,
null,
transition.to.queryParams.tags
);
});
} }
}); });
} }
} else { } else {
// User is not logged in // User is not logged in
$.cookie("destination_url", window.location.href); $.cookie("destination_url", window.location.href);
self.replaceWith("login"); this.replaceWith("login");
} }
}, },
_sendTransition(event, transition, categoryId) {
Ember.run.next(() => {
event.send(
"createNewTopicViaParams",
transition.to.queryParams.title,
transition.to.queryParams.body,
categoryId,
transition.to.queryParams.tags
);
});
},
_getCategory(mainCategory, subCategory, type) { _getCategory(mainCategory, subCategory, type) {
let category; let category;
if (!subCategory) { if (!subCategory) {
category = this.site category = this.site.categories.findBy(type, mainCategory.toLowerCase());
.get("categories")
.findBy(type, mainCategory.toLowerCase());
} else { } else {
const categories = this.site.get("categories"); const categories = this.site.categories;
const main = categories.findBy(type, mainCategory.toLowerCase()); const main = categories.findBy(type, mainCategory.toLowerCase());
if (main) { if (main) {
category = categories.find(function(item) { category = categories.find(item => {
return ( return (
item && item &&
item.get(type) === subCategory.toLowerCase() && item.type === subCategory.toLowerCase() &&
item.get("parent_category_id") === main.id item.parent_category_id === main.id
); );
}); });
} }
} }
return category; return category;
} }
}); });