From 049561ac49b772aa566f3a44d358a23698f733a8 Mon Sep 17 00:00:00 2001
From: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Date: Fri, 24 May 2019 13:11:33 +0200
Subject: [PATCH] FIX: introduces onSelectAny (regroup onSelect and
 onSelectNone) (#7594)

---
 .../discourse/controllers/topic.js.es6        |  4 +++
 .../javascripts/discourse/templates/topic.hbs |  2 +-
 .../components/single-select.js.es6           |  3 ++
 .../components/single-select-test.js.es6      | 34 +++++++++++++++++++
 4 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/app/assets/javascripts/discourse/controllers/topic.js.es6 b/app/assets/javascripts/discourse/controllers/topic.js.es6
index a340b95293d..cca7caf5d1b 100644
--- a/app/assets/javascripts/discourse/controllers/topic.js.es6
+++ b/app/assets/javascripts/discourse/controllers/topic.js.es6
@@ -223,6 +223,10 @@ export default Ember.Controller.extend(bufferedProperty("model"), {
   },
 
   actions: {
+    topicCategoryChanged(selection) {
+      this.set("buffered.category_id", selection.value);
+    },
+
     deletePending(pending) {
       return ajax(`/review/${pending.id}`, { type: "DELETE" })
         .then(() => {
diff --git a/app/assets/javascripts/discourse/templates/topic.hbs b/app/assets/javascripts/discourse/templates/topic.hbs
index 48bd3d07a6a..b47f7935cc2 100644
--- a/app/assets/javascripts/discourse/templates/topic.hbs
+++ b/app/assets/javascripts/discourse/templates/topic.hbs
@@ -25,7 +25,7 @@
               {{category-chooser
                 class="small"
                 value=(unbound buffered.category_id)
-                onSelect=(action (mut buffered.category_id))}}
+                onSelectAny=(action "topicCategoryChanged")}}
             {{/if}}
 
             {{#if canEditTags}}
diff --git a/app/assets/javascripts/select-kit/components/single-select.js.es6 b/app/assets/javascripts/select-kit/components/single-select.js.es6
index 5ddf7cd36ba..4d8f9b9b273 100644
--- a/app/assets/javascripts/select-kit/components/single-select.js.es6
+++ b/app/assets/javascripts/select-kit/components/single-select.js.es6
@@ -223,6 +223,7 @@ export default SelectKitComponent.extend({
       );
 
       this._boundaryActionHandler("onSelect", computedContentItem.value);
+      this._boundaryActionHandler("onSelectAny", computedContentItem);
       return;
     }
 
@@ -239,6 +240,7 @@ export default SelectKitComponent.extend({
         this
       );
       this._boundaryActionHandler("onSelectNone");
+      this._boundaryActionHandler("onSelectAny", computedContentItem);
       this.clearSelection();
       return;
     }
@@ -291,6 +293,7 @@ export default SelectKitComponent.extend({
         );
 
         this._boundaryActionHandler("onSelect", computedContentItem.value);
+        this._boundaryActionHandler("onSelectAny", computedContentItem);
 
         this.autoHighlight();
       });
diff --git a/test/javascripts/components/single-select-test.js.es6 b/test/javascripts/components/single-select-test.js.es6
index ca6a84e643c..8e247978cd5 100644
--- a/test/javascripts/components/single-select-test.js.es6
+++ b/test/javascripts/components/single-select-test.js.es6
@@ -924,3 +924,37 @@ componentTest("noopRow", {
     assert.equal(this.get("value"), "green");
   }
 });
+
+componentTest("onSelectAny", {
+  template: `<div class='test-external-action'></div>{{single-select none="none" content=content onSelectAny=(action externalAction)}}`,
+
+  beforeEach() {
+    this.set("externalAction", actual => {
+      find(".test-external-action").text(actual.value);
+    });
+
+    this.set("content", ["blue"]);
+  },
+
+  async test(assert) {
+    await this.get("subject").expand();
+    await this.get("subject").selectRowByValue("blue");
+
+    assert.equal(
+      find(".test-external-action")
+        .text()
+        .trim(),
+      "blue"
+    );
+
+    await this.get("subject").expand();
+    await this.get("subject").selectNoneRow();
+
+    assert.equal(
+      find(".test-external-action")
+        .text()
+        .trim(),
+      "__none__"
+    );
+  }
+});