From 7362aff34538ca3abeea42dd88fb1fbb3094f7d9 Mon Sep 17 00:00:00 2001
From: Toby Zerner <toby.zerner@gmail.com>
Date: Mon, 1 Jun 2015 10:29:01 +0930
Subject: [PATCH] Make replyAction into a promise. closes #100

---
 js/forum/src/components/discussion-page.js    |  2 +-
 js/forum/src/components/login-modal.js        |  2 +-
 .../src/initializers/discussion-controls.js   | 50 ++++++++++++-------
 3 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/js/forum/src/components/discussion-page.js b/js/forum/src/components/discussion-page.js
index dcb60d951..f4356e3fd 100644
--- a/js/forum/src/components/discussion-page.js
+++ b/js/forum/src/components/discussion-page.js
@@ -88,7 +88,7 @@ export default class DiscussionPage extends mixin(Component, evented) {
     this.stream.on('positionChanged', this.positionChanged.bind(this));
     this.stream.goToNumber(m.route.param('near') || 1, true);
 
-    this.trigger('loaded');
+    this.trigger('loaded', discussion);
   }
 
   onload(element, isInitialized, context) {
diff --git a/js/forum/src/components/login-modal.js b/js/forum/src/components/login-modal.js
index cbecd24d1..3e0897100 100644
--- a/js/forum/src/components/login-modal.js
+++ b/js/forum/src/components/login-modal.js
@@ -60,7 +60,7 @@ export default class LoginModal extends FormModal {
 
     app.session.login(email, password).then(() => {
       this.hide();
-      this.props.callback && this.props.callback();
+      this.props.onlogin && this.props.onlogin();
     }, response => {
       this.loading(false);
       if (response && response.code === 'confirm_email') {
diff --git a/js/forum/src/initializers/discussion-controls.js b/js/forum/src/initializers/discussion-controls.js
index 755bbc44d..1b1a031fd 100644
--- a/js/forum/src/initializers/discussion-controls.js
+++ b/js/forum/src/initializers/discussion-controls.js
@@ -8,25 +8,41 @@ import ItemList from 'flarum/utils/item-list';
 
 export default function(app) {
   Discussion.prototype.replyAction = function(goToLast, forceRefresh) {
-    if (app.session.user() && this.canReply()) {
-      if (goToLast && app.current.discussion && app.current.discussion().id() === this.id()) {
-        app.current.stream.goToLast();
+    var deferred = m.deferred();
+
+    var reply = () => {
+      if (this.canReply()) {
+        if (goToLast && app.viewingDiscussion(this)) {
+          app.current.stream.goToLast();
+        }
+
+        var component = app.composer.component;
+        if (!app.composingReplyTo(this) || forceRefresh) {
+          component = new ReplyComposer({
+            user: app.session.user(),
+            discussion: this
+          });
+          app.composer.load(component);
+        }
+        app.composer.show(goToLast);
+
+        deferred.resolve(component);
+      } else {
+        deferred.reject();
       }
-      var component = app.composer.component;
-      if (!(component instanceof ReplyComposer) || component.props.discussion !== this || component.props.user !== app.session.user() || forceRefresh) {
-        component = new ReplyComposer({
-          user: app.session.user(),
-          discussion: this
-        });
-        app.composer.load(component);
-      }
-      app.composer.show(goToLast);
-      return component;
-    } else if (!app.session.user()) {
-      app.modal.show(new LoginModal({
-        callback: () => app.current.one('loaded', this.replyAction.bind(this, goToLast, forceRefresh))
-      }));
+    };
+
+    if (app.session.user()) {
+      reply();
+    } else {
+      app.modal.show(
+        new LoginModal({
+          onlogin: () => app.current.one('loaded', reply)
+        })
+      );
     }
+
+    return deferred.promise;
   }
 
   Discussion.prototype.deleteAction = function() {