REFACTOR: Invite modal panels init, actions, and styling (#10242)

REFACTOR: Invite modal panels init, actions, and styling (#10242)

  • Import @action rather than using actions: {}
  • Set default values in functions outside of init, so the functions can be modified by modifyClass (plugin api).
  • Move padding from .choices div to the input in group selector.
diff --git a/app/assets/javascripts/discourse/app/components/invite-link-panel.js b/app/assets/javascripts/discourse/app/components/invite-link-panel.js
index b0d2393..b3baf81 100644
--- a/app/assets/javascripts/discourse/app/components/invite-link-panel.js
+++ b/app/assets/javascripts/discourse/app/components/invite-link-panel.js
@@ -21,16 +21,12 @@ export default Component.extend({
 
   init() {
     this._super(...arguments);
-
-    this.set("groupIds", []);
-    Group.findAll().then(groups => {
-      this.set("allGroups", groups.filterBy("automatic", false));
-    });
+    this.setDefaultSelectedGroups();
+    this.setGroupOptions();
   },
 
   willDestroyElement() {
     this._super(...arguments);
-
     this.reset();
   },
 
@@ -112,5 +108,15 @@ export default Component.extend({
         }
         model.setProperties({ saving: false, error: true });
       });
+  },
+
+  setDefaultSelectedGroups() {
+    this.set("groupIds", []);
+  },
+
+  setGroupOptions() {
+    Group.findAll().then(groups => {
+      this.set("allGroups", groups.filterBy("automatic", false));
+    });
   }
 });
diff --git a/app/assets/javascripts/discourse/app/components/invite-panel.js b/app/assets/javascripts/discourse/app/components/invite-panel.js
index 7f11433..0866fbf 100644
--- a/app/assets/javascripts/discourse/app/components/invite-panel.js
+++ b/app/assets/javascripts/discourse/app/components/invite-panel.js
@@ -1,7 +1,7 @@
 import I18n from "I18n";
 import discourseComputed from "discourse-common/utils/decorators";
 import { isEmpty } from "@ember/utils";
-import EmberObject, { computed } from "@ember/object";
+import EmberObject, { action, computed } from "@ember/object";
 import { alias, and, equal } from "@ember/object/computed";
 import Component from "@ember/component";
 import { emailValid } from "discourse/lib/utilities";
@@ -30,16 +30,12 @@ export default Component.extend({
 
   init() {
     this._super(...arguments);
-
-    this.set("groupIds", []);
-    Group.findAll().then(groups => {
-      this.set("allGroups", groups.filterBy("automatic", false));
-    });
+    this.setDefaultSelectedGroups();
+    this.setGroupOptions();
   },
 
   willDestroyElement() {
     this._super(...arguments);
-
     this.reset();
   },
 
@@ -304,105 +300,59 @@ export default Component.extend({
     });
   },
 
-  actions: {
-    createInvite() {
-      if (this.disabled) {
-        return;
-      }
-
-      const groupIds = this.groupIds;
-      const userInvitedController = this.userInvitedShow;
+  setDefaultSelectedGroups() {
+    this.set("groupIds", []);
+  },
 
-      const model = this.inviteModel;
-      model.setProperties({ saving: true, error: false });
+  setGroupOptions() {
+    Group.findAll().then(groups => {
+      this.set("allGroups", groups.filterBy("automatic", false));
+    });
+  },
 
-      const onerror = e => {
-        if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
-          this.set("errorMessage", e.jqXHR.responseJSON.errors[0]);
-        } else {
-          this.set(
-            "errorMessage",
-            this.isPM
-              ? I18n.t("topic.invite_private.error")
-              : I18n.t("topic.invite_reply.error")
-          );
-        }
-        model.setProperties({ saving: false, error: true });
-      };
-
-      if (this.hasGroups) {
-        return this.inviteModel
-          .createGroupInvite(this.emailOrUsername.trim())
-          .then(data => {
-            model.setProperties({ saving: false, finished: true });
-            this.get("inviteModel.details.allowed_groups").pushObject(
-              EmberObject.create(data.group)
-            );
-            this.appEvents.trigger("post-stream:refresh");
-          })
-          .catch(onerror);
-      } else {
-        return this.inviteModel
-          .createInvite(
-            this.emailOrUsername.trim(),
-            groupIds,
-            this.customMessage
-          )
-          .then(result => {
-            model.setProperties({ saving: false, finished: true });
-            if (!this.invitingToTopic && userInvitedController) {
-              Invite.findInvitedBy(
-                this.currentUser,
-                userInvitedController.get("filter")
-              ).then(inviteModel => {
-                userInvitedController.setProperties({
-                  model: inviteModel,
-                  totalInvites: inviteModel.invites.length
-                });
-              });
-            } else if (this.isPM && result && result.user) {
-              this.get("inviteModel.details.allowed_users").pushObject(
-                EmberObject.create(result.user)
-              );
-              this.appEvents.trigger("post-stream:refresh");
-            } else if (
-              this.invitingToTopic &&
-              emailValid(this.emailOrUsername.trim()) &&
-              result &&
-              result.user
-            ) {
-              this.set("invitingExistingUserToTopic", true);
-            }
-          })
-          .catch(onerror);
-      }
-    },
+  @action
+  createInvite() {
+    if (this.disabled) {
+      return;
+    }
 
-    generateInvitelink() {
-      if (this.disabled) {
-        return;
-      }
+    const groupIds = this.groupIds;
+    const userInvitedController = this.userInvitedShow;
 
-      const groupIds = this.groupIds;
-      const userInvitedController = this.userInvitedShow;
-      const model = this.inviteModel;
-      model.setProperties({ saving: true, error: false });
+    const model = this.inviteModel;
+    model.setProperties({ saving: true, error: false });
 
-      let topicId;
-      if (this.invitingToTopic) {
-        topicId = this.get("inviteModel.id");
+    const onerror = e => {
+      if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
+        this.set("errorMessage", e.jqXHR.responseJSON.errors[0]);
+      } else {
+        this.set(
+          "errorMessage",
+          this.isPM
+            ? I18n.t("topic.invite_private.error")
+            : I18n.t("topic.invite_reply.error")
+        );
       }
+      model.setProperties({ saving: false, error: true });
+    };
 
-      return model
-        .generateInviteLink(this.emailOrUsername.trim(), groupIds, topicId)
+    if (this.hasGroups) {
+      return this.inviteModel
+        .createGroupInvite(this.emailOrUsername.trim())
+        .then(data => {
+          model.setProperties({ saving: false, finished: true });
+          this.get("inviteModel.details.allowed_groups").pushObject(
+            EmberObject.create(data.group)
+          );
+          this.appEvents.trigger("post-stream:refresh");
+        })
+        .catch(onerror);
+    } else {
+      return this.inviteModel
+        .createInvite(this.emailOrUsername.trim(), groupIds, this.customMessage)
         .then(result => {
-          model.setProperties({
-            saving: false,
-            finished: true,
-            inviteLink: result
-          });
-
-          if (userInvitedController) {
+          model.setProperties({ saving: false, finished: true });
+          if (!this.invitingToTopic && userInvitedController) {
             Invite.findInvitedBy(
               this.currentUser,
               userInvitedController.get("filter")
@@ -412,46 +362,100 @@ export default Component.extend({
                 totalInvites: inviteModel.invites.length
               });
             });
-          }
-        })
-        .catch(e => {
-          if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
-            this.set("errorMessage", e.jqXHR.responseJSON.errors[0]);
-          } else {
-            this.set(
-              "errorMessage",
-              this.isPM
-                ? I18n.t("topic.invite_private.error")
-                : I18n.t("topic.invite_reply.error")
+          } else if (this.isPM && result && result.user) {
+            this.get("inviteModel.details.allowed_users").pushObject(
+              EmberObject.create(result.user)
             );

[... diff too long, it was truncated ...]

GitHub sha: a1507b23

This commit appears in #10242 which was approved by eviltrout. It was merged by markvanlan.

no need for get here

guess you could do model.id here no ?