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