From 239d56dd69a00d6ad3781fba83230da2110b8682 Mon Sep 17 00:00:00 2001 From: Isaac Janzen <50783505+janzenisaac@users.noreply.github.com> Date: Wed, 5 Jul 2023 05:04:06 -0500 Subject: [PATCH] DEV: Convert group-add-members modal to component-based API (#22368) --- .../components/modal/group-add-members.hbs | 60 ++++++++++++++ .../app/components/modal/group-add-members.js | 69 ++++++++++++++++ .../app/controllers/group-add-members.js | 82 ------------------- .../discourse/app/routes/group-index.js | 6 +- .../app/templates/modal/group-add-members.hbs | 55 ------------- 5 files changed, 134 insertions(+), 138 deletions(-) create mode 100644 app/assets/javascripts/discourse/app/components/modal/group-add-members.hbs create mode 100644 app/assets/javascripts/discourse/app/components/modal/group-add-members.js delete mode 100644 app/assets/javascripts/discourse/app/controllers/group-add-members.js delete mode 100644 app/assets/javascripts/discourse/app/templates/modal/group-add-members.hbs diff --git a/app/assets/javascripts/discourse/app/components/modal/group-add-members.hbs b/app/assets/javascripts/discourse/app/components/modal/group-add-members.hbs new file mode 100644 index 00000000000..dfa9879d05c --- /dev/null +++ b/app/assets/javascripts/discourse/app/components/modal/group-add-members.hbs @@ -0,0 +1,60 @@ + + <:body> +
+

{{i18n "groups.add_members.description"}}

+
+ +
+ + {{#if @model.can_admin_group}} +
+ +
+ {{/if}} + +
+ +
+
+ + <:footer> + + +
\ No newline at end of file diff --git a/app/assets/javascripts/discourse/app/components/modal/group-add-members.js b/app/assets/javascripts/discourse/app/components/modal/group-add-members.js new file mode 100644 index 00000000000..3e24d54dee4 --- /dev/null +++ b/app/assets/javascripts/discourse/app/components/modal/group-add-members.js @@ -0,0 +1,69 @@ +import Component from "@glimmer/component"; +import { action } from "@ember/object"; +import { isEmpty } from "@ember/utils"; +import { tracked } from "@glimmer/tracking"; +import { emailValid } from "discourse/lib/utilities"; +import I18n from "I18n"; +import { inject as service } from "@ember/service"; + +export default class GroupAddMembers extends Component { + @service currentUser; + @service router; + + @tracked loading = false; + @tracked setOwner = false; + @tracked notifyUsers = false; + @tracked usernamesAndEmails = []; + @tracked flash; + + get title() { + return I18n.t("groups.add_members.title", { + group_name: this.args.model.fullName || this.args.model.name, + }); + } + + get usernames() { + return this.usernamesAndEmails.reject(emailValid).join(","); + } + + get emails() { + return this.usernamesAndEmails.filter(emailValid).join(","); + } + + @action + setUsernamesAndEmails(usernamesAndEmails) { + this.usernamesAndEmails = usernamesAndEmails; + if (this.emails) { + if (!this.usernames) { + this.notifyUsers = false; + } + this.setOwner = false; + } + } + + @action + addMembers() { + if (isEmpty(this.usernamesAndEmails)) { + return; + } + this.loading = true; + const promise = this.setOwner + ? this.args.model.addOwners(this.usernames, true, this.notifyUsers) + : this.args.model.addMembers( + this.usernames, + true, + this.notifyUsers, + this.emails + ); + + promise + .then(() => { + this.router.transitionTo("group.members", this.args.model.name, { + queryParams: { ...(this.usernames && { filter: this.usernames }) }, + }); + this.args.closeModal(); + }) + .catch((e) => (this.flash = e)) + .finally(() => (this.loading = false)); + } +} diff --git a/app/assets/javascripts/discourse/app/controllers/group-add-members.js b/app/assets/javascripts/discourse/app/controllers/group-add-members.js deleted file mode 100644 index 345072b2d57..00000000000 --- a/app/assets/javascripts/discourse/app/controllers/group-add-members.js +++ /dev/null @@ -1,82 +0,0 @@ -import Controller from "@ember/controller"; -import { action } from "@ember/object"; -import { isEmpty } from "@ember/utils"; -import discourseComputed from "discourse-common/utils/decorators"; -import { flashAjaxError } from "discourse/lib/ajax-error"; -import { emailValid } from "discourse/lib/utilities"; -import ModalFunctionality from "discourse/mixins/modal-functionality"; -import I18n from "I18n"; - -export default Controller.extend(ModalFunctionality, { - loading: false, - - usernamesAndEmails: null, - setOwner: false, - notifyUsers: false, - - onShow() { - this.setProperties({ - loading: false, - setOwner: false, - notifyUsers: false, - usernamesAndEmails: [], - }); - }, - - @discourseComputed("model.name", "model.full_name") - rawTitle(name, fullName) { - return I18n.t("groups.add_members.title", { group_name: fullName || name }); - }, - - @discourseComputed("usernamesAndEmails.[]") - usernames(usernamesAndEmails) { - return usernamesAndEmails.reject(emailValid).join(","); - }, - - @discourseComputed("usernamesAndEmails.[]") - emails(usernamesAndEmails) { - return usernamesAndEmails.filter(emailValid).join(","); - }, - - @action - setUsernamesAndEmails(usernamesAndEmails) { - this.set("usernamesAndEmails", usernamesAndEmails); - - if (this.emails) { - if (!this.usernames) { - this.set("notifyUsers", false); - } - - this.set("setOwner", false); - } - }, - - @action - addMembers() { - if (isEmpty(this.usernamesAndEmails)) { - return; - } - - this.set("loading", true); - - const promise = this.setOwner - ? this.model.addOwners(this.usernames, true, this.notifyUsers) - : this.model.addMembers( - this.usernames, - true, - this.notifyUsers, - this.emails - ); - - promise - .then(() => { - this.transitionToRoute("group.members", this.get("model.name"), { - queryParams: this.usernames ? { filter: this.usernames } : {}, - }); - - this.send("closeModal"); - }) - .catch(flashAjaxError(this)) - .finally(() => this.set("loading", false)); - }, -}); diff --git a/app/assets/javascripts/discourse/app/routes/group-index.js b/app/assets/javascripts/discourse/app/routes/group-index.js index c1286780900..a5640ea5f26 100644 --- a/app/assets/javascripts/discourse/app/routes/group-index.js +++ b/app/assets/javascripts/discourse/app/routes/group-index.js @@ -2,8 +2,12 @@ import DiscourseRoute from "discourse/routes/discourse"; import I18n from "I18n"; import { action } from "@ember/object"; import showModal from "discourse/lib/show-modal"; +import { inject as service } from "@ember/service"; +import GroupAddMembersModal from "discourse/components/modal/group-add-members"; export default DiscourseRoute.extend({ + modal: service(), + titleToken() { return I18n.t("groups.members.title"); }, @@ -25,7 +29,7 @@ export default DiscourseRoute.extend({ @action showAddMembersModal() { - showModal("group-add-members", { model: this.modelFor("group") }); + this.modal.show(GroupAddMembersModal, { model: this.modelFor("group") }); }, @action diff --git a/app/assets/javascripts/discourse/app/templates/modal/group-add-members.hbs b/app/assets/javascripts/discourse/app/templates/modal/group-add-members.hbs deleted file mode 100644 index 851f6322361..00000000000 --- a/app/assets/javascripts/discourse/app/templates/modal/group-add-members.hbs +++ /dev/null @@ -1,55 +0,0 @@ - -
-

{{i18n "groups.add_members.description"}}

- -
- -
- - {{#if this.model.can_admin_group}} -
- -
- {{/if}} - -
- -
-
-
- - \ No newline at end of file