From dcd1d422d1251f104abdecb72d151ea7c4357dfb Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 26 Mar 2018 17:33:03 +0800 Subject: [PATCH] UX: Allow admins to set users as owners while adding users. https://meta.discourse.org/t/adding-owners-members-ux-is-inconsistent-and-misleading/58084/9 --- .../controllers/group-membership.js.es6 | 33 ++++++++++++------- .../javascripts/discourse/models/group.js.es6 | 22 ++++++++++--- .../templates/modal/group-membership.hbs | 18 ++++++++-- .../stylesheets/common/base/discourse.scss | 4 +++ app/assets/stylesheets/common/base/group.scss | 15 +++++++++ app/controllers/admin/groups_controller.rb | 2 +- config/locales/client.en.yml | 2 ++ spec/requests/admin/groups_controller_spec.rb | 6 +++- .../acceptance/group-index-test.js.es6 | 19 +++++++++++ 9 files changed, 99 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/group-membership.js.es6 b/app/assets/javascripts/discourse/controllers/group-membership.js.es6 index 76dafb878a3..8c22d2765e2 100644 --- a/app/assets/javascripts/discourse/controllers/group-membership.js.es6 +++ b/app/assets/javascripts/discourse/controllers/group-membership.js.es6 @@ -4,6 +4,7 @@ import { extractError } from 'discourse/lib/ajax-error'; export default Ember.Controller.extend(ModalFunctionality, { loading: false, + setAsOwner: false, @computed('model.usernames') disableAddButton(usernames) { @@ -12,19 +13,27 @@ export default Ember.Controller.extend(ModalFunctionality, { actions: { addMembers() { - if (Em.isEmpty(this.get("model.usernames"))) { return; } + const model = this.get('model'); + const usernames = model.get('usernames'); + if (Em.isEmpty(usernames)) { return; } + let promise; - this.get("model").addMembers(this.get("model.usernames")) - .then(() => { - this.transitionToRoute( - "group.members", - this.get('model.name'), - { queryParams: { filter: this.get('model.usernames') } } - ); - this.set("model.usernames", null); - this.send('closeModal'); - }) - .catch(error => this.flash(extractError(error), 'error')); + if (this.get('setAsOwner')) { + promise = model.addOwners(usernames, true); + } else { + promise = model.addMembers(usernames, true); + } + + promise.then(() => { + this.transitionToRoute( + "group.members", + this.get('model.name'), + { queryParams: { filter: usernames } } + ); + model.set("usernames", null); + this.send('closeModal'); + }) + .catch(error => this.flash(extractError(error), 'error')); }, }, }); diff --git a/app/assets/javascripts/discourse/models/group.js.es6 b/app/assets/javascripts/discourse/models/group.js.es6 index 14844e58ec1..4ad96c34fb6 100644 --- a/app/assets/javascripts/discourse/models/group.js.es6 +++ b/app/assets/javascripts/discourse/models/group.js.es6 @@ -74,24 +74,36 @@ const Group = RestModel.extend({ }); }, - addMembers(usernames) { + addMembers(usernames, filter) { return ajax('/groups/' + this.get('id') + '/members.json', { type: "PUT", data: { usernames: usernames } }).then(response => { - this.findMembers({ filter: response.usernames.join(',') }); + if (filter) { + this._filterMembers(response); + } else { + this.findMembers(); + } }); }, - addOwners(usernames) { + addOwners(usernames, filter) { return ajax(`/admin/groups/${this.get('id')}/owners.json`, { type: "PUT", data: { group: { usernames: usernames } } - }).then(() => { - this.findMembers(); + }).then(response => { + if (filter) { + this._filterMembers(response); + } else { + this.findMembers(); + } }); }, + _filterMembers(response) { + return this.findMembers({ filter: response.usernames.join(",") }); + }, + @computed("display_name", "name") displayName(groupDisplayName, name) { return groupDisplayName || name; diff --git a/app/assets/javascripts/discourse/templates/modal/group-membership.hbs b/app/assets/javascripts/discourse/templates/modal/group-membership.hbs index 7c5b07bfc66..92c35ab9f0a 100644 --- a/app/assets/javascripts/discourse/templates/modal/group-membership.hbs +++ b/app/assets/javascripts/discourse/templates/modal/group-membership.hbs @@ -1,7 +1,19 @@ {{#d-modal-body class='group-membership' title="groups.add_members.title"}} - {{user-selector usernames=model.usernames - placeholderKey="groups.selector_placeholder" - id="group-membership-user-selector"}} +
+ + + {{user-selector + usernames=model.usernames + placeholderKey="groups.selector_placeholder" + id="group-membership-user-selector"}} +
+ + {{#if this.currentUser.admin}} +
+ + {{input type="checkbox" class="inline" checked=setAsOwner}} +
+ {{/if}} {{/d-modal-body}}