Tests + Refactoring for Suspension Modal

This commit is contained in:
Robin Ward
2017-09-13 14:11:33 -04:00
parent 6e48884274
commit 2a56cf8bb6
9 changed files with 112 additions and 44 deletions

View File

@ -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));
}
}
}