From 2a56cf8bb6373da03915b08d71ae98628bbab3fd Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 13 Sep 2017 14:11:33 -0400 Subject: [PATCH] Tests + Refactoring for Suspension Modal --- .../admin/controllers/admin-user-index.js.es6 | 4 +- .../modals/admin-suspend-user.js.es6 | 45 +++++++++++++------ .../admin/models/admin-user.js.es6 | 17 ++++--- .../admin/services/admin-tools.js.es6 | 2 +- .../templates/modal/admin-suspend-user.hbs | 1 + .../admin/templates/user-index.hbs | 34 +++++++------- .../stylesheets/common/admin/suspend.scss | 4 ++ app/controllers/admin/users_controller.rb | 18 +++++++- .../acceptance/admin-suspend-user-test.js.es6 | 31 ++++++++++++- 9 files changed, 112 insertions(+), 44 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/admin-user-index.js.es6 b/app/assets/javascripts/admin/controllers/admin-user-index.js.es6 index c594f1db672..69c2bca9b86 100644 --- a/app/assets/javascripts/admin/controllers/admin-user-index.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-user-index.js.es6 @@ -58,7 +58,6 @@ export default Ember.Controller.extend(CanCheckEmails, { saveTrustLevel() { return this.get("model").saveTrustLevel(); }, restoreTrustLevel() { return this.get("model").restoreTrustLevel(); }, lockTrustLevel(locked) { return this.get("model").lockTrustLevel(locked); }, - unsuspend() { return this.get("model").unsuspend(); }, unblock() { return this.get("model").unblock(); }, block() { return this.get("model").block(); }, deleteAllPosts() { return this.get("model").deleteAllPosts(); }, @@ -68,6 +67,9 @@ export default Ember.Controller.extend(CanCheckEmails, { showSuspendModal() { this.get('adminTools').showSuspendModal(this.get('model')); }, + unsuspend() { + this.get("model").unsuspend().catch(popupAjaxError); + }, toggleUsernameEdit() { diff --git a/app/assets/javascripts/admin/controllers/modals/admin-suspend-user.js.es6 b/app/assets/javascripts/admin/controllers/modals/admin-suspend-user.js.es6 index d3e19de5694..4c3f2a91990 100644 --- a/app/assets/javascripts/admin/controllers/modals/admin-suspend-user.js.es6 +++ b/app/assets/javascripts/admin/controllers/modals/admin-suspend-user.js.es6 @@ -1,24 +1,41 @@ import ModalFunctionality from 'discourse/mixins/modal-functionality'; +import computed from 'ember-addons/ember-computed-decorators'; +import { popupAjaxError } from 'discourse/lib/ajax-error'; export default Ember.Controller.extend(ModalFunctionality, { + duration: null, + reason: null, + message: null, + loading: false, - submitDisabled: function() { - return (!this.get('reason') || this.get('reason').length < 1); - }.property('reason'), + onShow() { + this.setProperties({ + duration: null, + reason: null, + message: null, + loading: false + }); + }, + + @computed('reason', 'loading') + submitDisabled(reason, loading) { + return (loading || !reason || reason.length < 1); + }, actions: { - suspend: function() { - if (this.get('submitDisabled')) return; - var duration = parseInt(this.get('duration'), 10); + suspend() { + if (this.get('submitDisabled')) { return; } + + let duration = parseInt(this.get('duration'), 10); if (duration > 0) { - var self = this; - this.send('hideModal'); - this.get('model').suspend(duration, this.get('reason')).then(function() { - window.location.reload(); - }, function(e) { - var error = I18n.t('admin.user.suspend_failed', { error: "http: " + e.status + " - " + e.body }); - bootbox.alert(error, function() { self.send('reopenModal'); }); - }); + this.set('loading', true); + this.get('model').suspend({ + duration, + reason: this.get('reason'), + message: this.get('message') + }).then(() => { + this.send('closeModal'); + }).catch(popupAjaxError).finally(() => this.set('loading', false)); } } } diff --git a/app/assets/javascripts/admin/models/admin-user.js.es6 b/app/assets/javascripts/admin/models/admin-user.js.es6 index 1677d3add40..59c3c3ee1d3 100644 --- a/app/assets/javascripts/admin/models/admin-user.js.es6 +++ b/app/assets/javascripts/admin/models/admin-user.js.es6 @@ -240,21 +240,20 @@ const AdminUser = Discourse.User.extend({ return suspended_at.format('L') + " - " + suspended_till.format('L'); }.property('suspended_till', 'suspended_at'), - suspend(duration, reason) { - return ajax("/admin/users/" + this.id + "/suspend", { + suspend(data) { + return ajax(`/admin/users/${this.id}/suspend`, { type: 'PUT', - data: { duration: duration, reason: reason } + data + }).then(result => { + this.setProperties(result.suspension); }); }, unsuspend() { - return ajax("/admin/users/" + this.id + "/unsuspend", { + return ajax(`/admin/users/${this.id}/unsuspend`, { type: 'PUT' - }).then(function() { - window.location.reload(); - }).catch(function(e) { - var error = I18n.t('admin.user.unsuspend_failed', { error: "http: " + e.status + " - " + e.body }); - bootbox.alert(error); + }).then(result => { + this.setProperties(result.suspension); }); }, diff --git a/app/assets/javascripts/admin/services/admin-tools.js.es6 b/app/assets/javascripts/admin/services/admin-tools.js.es6 index ce7a6e3b793..a65b3bdde1c 100644 --- a/app/assets/javascripts/admin/services/admin-tools.js.es6 +++ b/app/assets/javascripts/admin/services/admin-tools.js.es6 @@ -22,7 +22,7 @@ export default Ember.Service.extend({ showSuspendModal(user) { showModal('admin-suspend-user', { - user, + model: user, admin: true, modalClass: 'suspend-user-modal' }); diff --git a/app/assets/javascripts/admin/templates/modal/admin-suspend-user.hbs b/app/assets/javascripts/admin/templates/modal/admin-suspend-user.hbs index 8f7ca5b8c12..2810a0ed387 100644 --- a/app/assets/javascripts/admin/templates/modal/admin-suspend-user.hbs +++ b/app/assets/javascripts/admin/templates/modal/admin-suspend-user.hbs @@ -44,4 +44,5 @@ icon="ban" label="admin.user.suspend"}} {{d-modal-cancel close=(action "closeModal")}} + {{conditional-loading-spinner condition=loading size="small"}} diff --git a/app/assets/javascripts/admin/templates/user-index.hbs b/app/assets/javascripts/admin/templates/user-index.hbs index 1ea3703741c..7b40e0469b4 100644 --- a/app/assets/javascripts/admin/templates/user-index.hbs +++ b/app/assets/javascripts/admin/templates/user-index.hbs @@ -296,29 +296,33 @@ -
-
{{i18n 'admin.user.suspended'}}
-
{{i18n-yes-no model.isSuspended}}
-
- {{#if model.isSuspended}} - {{d-button class="btn-danger" action="unsuspend" icon="ban" label="admin.user.unsuspend"}} - {{suspendDuration}} - {{i18n 'admin.user.suspended_explanation'}} - {{else}} - {{#if model.canSuspend}} +
+
{{i18n 'admin.user.suspended'}}
+
{{i18n-yes-no model.isSuspended}}
+
+ {{#if model.isSuspended}} {{d-button - class="btn-danger suspend-user" - action="showSuspendModal" + class="btn-danger unsuspend-user" + action=(action "unsuspend") icon="ban" - label="admin.user.suspend"}} + label="admin.user.unsuspend"}} + {{suspendDuration}} {{i18n 'admin.user.suspended_explanation'}} + {{else}} + {{#if model.canSuspend}} + {{d-button + class="btn-danger suspend-user" + action=(action "showSuspendModal") + icon="ban" + label="admin.user.suspend"}} + {{i18n 'admin.user.suspended_explanation'}} + {{/if}} {{/if}} - {{/if}}
{{#if model.isSuspended}} -
+
{{i18n 'admin.user.suspended_by'}}
{{#link-to 'adminUser' suspendedBy}}{{avatar model.suspendedBy imageSize="tiny"}}{{/link-to}} diff --git a/app/assets/stylesheets/common/admin/suspend.scss b/app/assets/stylesheets/common/admin/suspend.scss index 93e22daae7a..ba0682cdf2e 100644 --- a/app/assets/stylesheets/common/admin/suspend.scss +++ b/app/assets/stylesheets/common/admin/suspend.scss @@ -16,4 +16,8 @@ box-sizing: border-box; height: 2.5em; } + + .inline-spinner { + float: right; + } } diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index d2533deb2f0..07dd472db9e 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -55,11 +55,20 @@ class Admin::UsersController < Admin::AdminController guardian.ensure_can_suspend!(@user) @user.suspended_till = params[:duration].to_i.days.from_now @user.suspended_at = DateTime.now + @user.save! @user.revoke_api_key StaffActionLogger.new(current_user).log_user_suspend(@user, params[:reason]) @user.logged_out - render body: nil + + render_json_dump( + suspension: { + suspended: true, + suspend_reason: params[:reason], + suspended_till: @user.suspended_till, + suspended_at: @user.suspended_at + } + ) end def unsuspend @@ -68,7 +77,12 @@ class Admin::UsersController < Admin::AdminController @user.suspended_at = nil @user.save! StaffActionLogger.new(current_user).log_user_unsuspend(@user) - render body: nil + + render_json_dump( + suspension: { + suspended: false + } + ) end def log_out diff --git a/test/javascripts/acceptance/admin-suspend-user-test.js.es6 b/test/javascripts/acceptance/admin-suspend-user-test.js.es6 index 66463727024..0c2c62b0db5 100644 --- a/test/javascripts/acceptance/admin-suspend-user-test.js.es6 +++ b/test/javascripts/acceptance/admin-suspend-user-test.js.es6 @@ -1,6 +1,22 @@ import { acceptance } from "helpers/qunit-helpers"; -acceptance("Admin - Suspend User", { loggedIn: true }); +acceptance("Admin - Suspend User", { + loggedIn: true, + + pretend(server, helper) { + server.put('/admin/users/:user_id/suspend', () => helper.response(200, { + suspension: { + suspended: true + } + })); + + server.put('/admin/users/:user_id/unsuspend', () => helper.response(200, { + suspension: { + suspended: false + } + })); + } +}); QUnit.test("suspend a user - cancel", assert => { visit("/admin/users/1234/regular"); @@ -16,8 +32,13 @@ QUnit.test("suspend a user - cancel", assert => { }); }); -QUnit.test("suspend a user", assert => { +QUnit.test("suspend, then unsuspend a user", assert => { visit("/admin/users/1234/regular"); + + andThen(() => { + assert.ok(!exists('.suspension-info')); + }); + click(".suspend-user"); andThen(() => { @@ -32,5 +53,11 @@ QUnit.test("suspend a user", assert => { click('.perform-suspend'); andThen(() => { assert.equal(find('.suspend-user-modal:visible').length, 0); + assert.ok(exists('.suspension-info')); + }); + + click('.unsuspend-user'); + andThen(() => { + assert.ok(!exists('.suspension-info')); }); });