From 7d9c97d66141d35d00258fe544211d9fd7f79a76 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Thu, 19 Jul 2018 15:48:26 +0530 Subject: [PATCH] FEATURE: land on specified category page when creating topic via URL --- .../discourse/controllers/composer.js.es6 | 26 ------ .../discourse/mixins/open-composer.js.es6 | 2 - .../discourse/routes/application.js.es6 | 3 +- .../discourse/routes/new-topic.js.es6 | 84 ++++++++++++++++--- 4 files changed, 72 insertions(+), 43 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/composer.js.es6 b/app/assets/javascripts/discourse/controllers/composer.js.es6 index 1232707b6c4..1e5cb0761dd 100644 --- a/app/assets/javascripts/discourse/controllers/composer.js.es6 +++ b/app/assets/javascripts/discourse/controllers/composer.js.es6 @@ -814,32 +814,6 @@ export default Ember.Controller.extend({ if (opts.topicCategoryId) { this.set("model.categoryId", opts.topicCategoryId); - } else if (opts.topicCategory) { - const splitCategory = opts.topicCategory.split("/"); - let category; - - if (!splitCategory[1]) { - category = this.site - .get("categories") - .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) { - return ( - item && - item.get("nameLower") === splitCategory[1].toLowerCase() && - item.get("parent_category_id") === mainCategory.id - ); - }); - } - - if (category) { - this.set("model.categoryId", category.get("id")); - } } if ( diff --git a/app/assets/javascripts/discourse/mixins/open-composer.js.es6 b/app/assets/javascripts/discourse/mixins/open-composer.js.es6 index a5c7a7f3a99..e3576cda08c 100644 --- a/app/assets/javascripts/discourse/mixins/open-composer.js.es6 +++ b/app/assets/javascripts/discourse/mixins/open-composer.js.es6 @@ -16,7 +16,6 @@ export default Ember.Mixin.create({ topicTitle, topicBody, topicCategoryId, - topicCategory, topicTags ) { this.controllerFor("composer").open({ @@ -24,7 +23,6 @@ export default Ember.Mixin.create({ topicTitle, topicBody, topicCategoryId, - topicCategory, topicTags, draftKey: controller.get("model.draft_key"), draftSequence: controller.get("model.draft_sequence") diff --git a/app/assets/javascripts/discourse/routes/application.js.es6 b/app/assets/javascripts/discourse/routes/application.js.es6 index 88523b01647..a161f09542c 100644 --- a/app/assets/javascripts/discourse/routes/application.js.es6 +++ b/app/assets/javascripts/discourse/routes/application.js.es6 @@ -183,13 +183,12 @@ const ApplicationRoute = Discourse.Route.extend(OpenComposer, { }); }, - createNewTopicViaParams(title, body, category_id, category, tags) { + createNewTopicViaParams(title, body, category_id, tags) { this.openComposerWithTopicParams( this.controllerFor("discovery/topics"), title, body, category_id, - category, tags ); }, diff --git a/app/assets/javascripts/discourse/routes/new-topic.js.es6 b/app/assets/javascripts/discourse/routes/new-topic.js.es6 index b6bc13393b6..b1ee1ae8f7b 100644 --- a/app/assets/javascripts/discourse/routes/new-topic.js.es6 +++ b/app/assets/javascripts/discourse/routes/new-topic.js.es6 @@ -1,23 +1,81 @@ +import Category from "discourse/models/category"; + export default Discourse.Route.extend({ beforeModel: function(transition) { const self = this; if (Discourse.User.current()) { - // User is logged in - self.replaceWith("discovery.latest").then(function(e) { - if (self.controllerFor("navigation/default").get("canCreateTopic")) { - // User can create topic - Ember.run.next(function() { - e.send( - "createNewTopicViaParams", - transition.queryParams.title, - transition.queryParams.body, - transition.queryParams.category_id, - transition.queryParams.category, - transition.queryParams.tags + let category, category_id; + + if (transition.queryParams.category_id) { + category_id = transition.queryParams.category_id; + category = Category.findById(category_id); + } else if (transition.queryParams.category) { + const splitCategory = transition.queryParams.category.split("/"); + + if (!splitCategory[1]) { + category = this.site + .get("categories") + .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) { + return ( + item && + item.get("nameLower") === splitCategory[1].toLowerCase() && + item.get("parent_category_id") === mainCategory.id ); }); } - }); + + if (category) { + category_id = category.get("id"); + } + } + + if (Boolean(category)) { + let route = "discovery.parentCategory"; + let params = { category, slug: category.get("slug") }; + if (category.get("parentCategory")) { + route = "discovery.category"; + params = { + category, + parentSlug: category.get("parentCategory.slug"), + slug: category.get("slug") + }; + } + + self.replaceWith(route, params).then(function(e) { + if (self.controllerFor("navigation/category").get("canCreateTopic")) { + Ember.run.next(function() { + e.send( + "createNewTopicViaParams", + transition.queryParams.title, + transition.queryParams.body, + category_id, + transition.queryParams.tags + ); + }); + } + }); + } else { + self.replaceWith("discovery.latest").then(function(e) { + if (self.controllerFor("navigation/default").get("canCreateTopic")) { + Ember.run.next(function() { + e.send( + "createNewTopicViaParams", + transition.queryParams.title, + transition.queryParams.body, + null, + transition.queryParams.tags + ); + }); + } + }); + } } else { // User is not logged in $.cookie("destination_url", window.location.href);