REFACTOR: user-invited-show (#7078)

This commit is contained in:
Joffrey JAFFEUX 2019-02-27 12:59:57 +01:00 committed by GitHub
parent 8ff3fc20a6
commit 5c476f639c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 73 deletions

View File

@ -1,8 +1,11 @@
import Invite from "discourse/models/invite"; import Invite from "discourse/models/invite";
import debounce from "discourse/lib/debounce"; import debounce from "discourse/lib/debounce";
import { popupAjaxError } from "discourse/lib/ajax-error"; import { popupAjaxError } from "discourse/lib/ajax-error";
import {
default as computed,
observes
} from "ember-addons/ember-computed-decorators";
// This controller handles actions related to a user's invitations
export default Ember.Controller.extend({ export default Ember.Controller.extend({
user: null, user: null,
model: null, model: null,
@ -13,84 +16,67 @@ export default Ember.Controller.extend({
invitesLoading: false, invitesLoading: false,
reinvitedAll: false, reinvitedAll: false,
rescindedAll: false, rescindedAll: false,
searchTerm: null,
init: function() { init() {
this._super(...arguments); this._super(...arguments);
this.set("searchTerm", ""); this.set("searchTerm", "");
}, },
/** @observes("searchTearm")
Observe the search term box with a debouncer and change the results.
@observes searchTerm
**/
_searchTermChanged: debounce(function() { _searchTermChanged: debounce(function() {
var self = this;
Invite.findInvitedBy( Invite.findInvitedBy(
self.get("user"), this.get("user"),
this.get("filter"), this.get("filter"),
this.get("searchTerm") this.get("searchTerm")
).then(function(invites) { ).then(invites => this.set("model", invites));
self.set("model", invites); }, 250),
});
}, 250).observes("searchTerm"),
inviteRedeemed: Ember.computed.equal("filter", "redeemed"), inviteRedeemed: Ember.computed.equal("filter", "redeemed"),
showBulkActionButtons: function() { @computed("filter")
showBulkActionButtons(filter) {
return ( return (
this.get("filter") === "pending" && filter === "pending" &&
this.get("model").invites.length > 4 && this.get("model").invites.length > 4 &&
this.currentUser.get("staff") this.currentUser.get("staff")
); );
}.property("filter"), },
/** @computed
Can the currently logged in user invite users to the site canInviteToForum() {
@property canInviteToForum
**/
canInviteToForum: function() {
return Discourse.User.currentProp("can_invite_to_forum"); return Discourse.User.currentProp("can_invite_to_forum");
}.property(), },
/** @computed
Can the currently logged in user bulk invite users to the site (only Admin is allowed to perform this operation) canBulkInvite() {
@property canBulkInvite
**/
canBulkInvite: function() {
return Discourse.User.currentProp("admin"); return Discourse.User.currentProp("admin");
}.property(), },
/** showSearch: Ember.computed.gte("totalInvites", 10),
Should the search filter input box be displayed?
@property showSearch @computed("invitesCount.total", "invitesCount.pending}")
**/ pendingLabel(invitesCountTotal, invitesCountPending) {
showSearch: function() { if (invitesCountTotal > 50) {
return this.get("totalInvites") > 9;
}.property("totalInvites"),
pendingLabel: function() {
if (this.get("invitesCount.total") > 50) {
return I18n.t("user.invited.pending_tab_with_count", { return I18n.t("user.invited.pending_tab_with_count", {
count: this.get("invitesCount.pending") count: invitesCountPending
}); });
} else { } else {
return I18n.t("user.invited.pending_tab"); return I18n.t("user.invited.pending_tab");
} }
}.property("invitesCount"), },
redeemedLabel: function() { @computed("invitesCount.total", "invitesCount.redeemed")
if (this.get("invitesCount.total") > 50) { redeemedLabel(invitesCountTotal, invitesCountRedeemed) {
if (invitesCountTotal > 50) {
return I18n.t("user.invited.redeemed_tab_with_count", { return I18n.t("user.invited.redeemed_tab_with_count", {
count: this.get("invitesCount.redeemed") count: invitesCountRedeemed
}); });
} else { } else {
return I18n.t("user.invited.redeemed_tab"); return I18n.t("user.invited.redeemed_tab");
} }
}.property("invitesCount"), },
actions: { actions: {
rescind(invite) { rescind(invite) {
@ -120,34 +106,31 @@ export default Ember.Controller.extend({
bootbox.confirm(I18n.t("user.invited.reinvite_all_confirm"), confirm => { bootbox.confirm(I18n.t("user.invited.reinvite_all_confirm"), confirm => {
if (confirm) { if (confirm) {
Invite.reinviteAll() Invite.reinviteAll()
.then(() => { .then(() => this.set("reinvitedAll", true))
this.set("reinvitedAll", true);
})
.catch(popupAjaxError); .catch(popupAjaxError);
} }
}); });
}, },
loadMore() { loadMore() {
var self = this; const model = this.get("model");
var model = self.get("model");
if (self.get("canLoadMore") && !self.get("invitesLoading")) { if (this.get("canLoadMore") && !this.get("invitesLoading")) {
self.set("invitesLoading", true); this.set("invitesLoading", true);
Invite.findInvitedBy( Invite.findInvitedBy(
self.get("user"), this.get("user"),
self.get("filter"), this.get("filter"),
self.get("searchTerm"), this.get("searchTerm"),
model.invites.length model.invites.length
).then(function(invite_model) { ).then(invite_model => {
self.set("invitesLoading", false); this.set("invitesLoading", false);
model.invites.pushObjects(invite_model.invites); model.invites.pushObjects(invite_model.invites);
if ( if (
invite_model.invites.length === 0 || invite_model.invites.length === 0 ||
invite_model.invites.length < invite_model.invites.length <
Discourse.SiteSettings.invites_per_page Discourse.SiteSettings.invites_per_page
) { ) {
self.set("canLoadMore", false); this.set("canLoadMore", false);
} }
}); });
} }

View File

@ -3,12 +3,11 @@ import showModal from "discourse/lib/show-modal";
export default Discourse.Route.extend({ export default Discourse.Route.extend({
model(params) { model(params) {
const self = this; Invite.findInvitedCount(this.modelFor("user")).then(result =>
Invite.findInvitedCount(self.modelFor("user")).then(function(result) { this.set("invitesCount", result)
self.set("invitesCount", result); );
}); this.inviteFilter = params.filter;
self.inviteFilter = params.filter; return Invite.findInvitedBy(this.modelFor("user"), params.filter);
return Invite.findInvitedBy(self.modelFor("user"), params.filter);
}, },
afterModel(model) { afterModel(model) {
@ -19,7 +18,7 @@ export default Discourse.Route.extend({
setupController(controller, model) { setupController(controller, model) {
controller.setProperties({ controller.setProperties({
model: model, model,
user: this.controllerFor("user").get("model"), user: this.controllerFor("user").get("model"),
filter: this.inviteFilter, filter: this.inviteFilter,
searchTerm: "", searchTerm: "",

View File

@ -14,7 +14,7 @@
</nav> </nav>
<div class="pull-right"> <div class="pull-right">
{{d-button icon="plus" action=(route-action "showInvite") label="user.invited.create" class="btn"}} {{d-button icon="plus" action=(route-action "showInvite") label="user.invited.create"}}
{{#if canBulkInvite}} {{#if canBulkInvite}}
{{csv-uploader uploading=uploading}} {{csv-uploader uploading=uploading}}
<a href="https://meta.discourse.org/t/sending-bulk-user-invites/16468" target="_blank" style="color:black;">{{d-icon "question-circle"}}</a> <a href="https://meta.discourse.org/t/sending-bulk-user-invites/16468" target="_blank" style="color:black;">{{d-icon "question-circle"}}</a>
@ -23,12 +23,12 @@
{{#if rescindedAll}} {{#if rescindedAll}}
{{i18n 'user.invited.rescinded_all'}} {{i18n 'user.invited.rescinded_all'}}
{{else}} {{else}}
{{d-button icon="times" action=(action "rescindAll") class="btn" label="user.invited.rescind_all"}} {{d-button icon="times" action=(action "rescindAll") label="user.invited.rescind_all"}}
{{/if}} {{/if}}
{{#if reinvitedAll}} {{#if reinvitedAll}}
{{i18n 'user.invited.reinvited_all'}} {{i18n 'user.invited.reinvited_all'}}
{{else}} {{else}}
{{d-button icon="refresh" action=(action "reinviteAll") class="btn" label="user.invited.reinvite_all"}} {{d-button icon="sync" action=(action "reinviteAll") label="user.invited.reinvite_all"}}
{{/if}} {{/if}}
{{/if}} {{/if}}
</div> </div>
@ -72,9 +72,11 @@
<td>{{number invite.user.topics_entered}}</td> <td>{{number invite.user.topics_entered}}</td>
<td>{{number invite.user.posts_read_count}}</td> <td>{{number invite.user.posts_read_count}}</td>
<td>{{{format-duration invite.user.time_read}}}</td> <td>{{{format-duration invite.user.time_read}}}</td>
<td><span title="{{i18n 'user.invited.days_visited'}}">{{{unbound invite.user.days_visited}}}</span> <td>
<span title="{{i18n 'user.invited.days_visited'}}">{{{unbound invite.user.days_visited}}}</span>
/ /
<span title="{{i18n 'user.invited.account_age_days'}}">{{{unbound invite.user.days_since_created}}}</span></td> <span title="{{i18n 'user.invited.account_age_days'}}">{{{unbound invite.user.days_since_created}}}</span>
</td>
{{/if}} {{/if}}
{{else}} {{else}}
<td>{{unbound invite.email}}</td> <td>{{unbound invite.email}}</td>
@ -87,13 +89,13 @@
{{#if invite.rescinded}} {{#if invite.rescinded}}
{{i18n 'user.invited.rescinded'}} {{i18n 'user.invited.rescinded'}}
{{else}} {{else}}
{{d-button icon="times" action=(action "rescind") actionParam=invite class="btn" label="user.invited.rescind"}} {{d-button icon="times" action=(action "rescind") actionParam=invite label="user.invited.rescind"}}
{{/if}} {{/if}}
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;
{{#if invite.reinvited}} {{#if invite.reinvited}}
{{i18n 'user.invited.reinvited'}} {{i18n 'user.invited.reinvited'}}
{{else}} {{else}}
{{d-button icon="refresh" action=(action "reinvite") actionParam=invite class="btn" label="user.invited.reinvite"}} {{d-button icon="sync" action=(action "reinvite") actionParam=invite label="user.invited.reinvite"}}
{{/if}} {{/if}}
</td> </td>
{{/if}} {{/if}}