define('home/views/SettingsBlockingView',[
'backbone-marionette',
'core/api',
'home/views/SettingsUserBlockingNavView',
'home/views/SettingsBlockingUserListView',
'home/templates/settingsBlocking',
'home/templates/settingsBlockingAlertError',
], function (
Marionette,
api,
SettingsUserBlockingNavView,
SettingsBlockingUserListView,
settingsBlockingTemplate,
settingsBlockingAlertErrorTemplate
) {
'use strict';
var SettingsBlockingView = Marionette.Layout.extend({
events: {
'click [data-action=unblock]': 'unblockUser',
'click [data-action=close-alert]': 'closeAlert',
},
template: settingsBlockingTemplate,
regions: {
alertRegion: '[data-role=alert-message]',
buttonsRegion: '[data-role=buttons]',
usersRegion: '[data-role=users]',
},
closeAlert: function (evt) {
evt.preventDefault();
this.alertRegion.close();
},
unblockUser: function (evt) {
evt.preventDefault();
var username = evt.target.getAttribute('data-username');
var collection = this.collection;
var user = collection.findWhere({ username: username });
var onSuccessfulUnBlock = function () {
collection.remove(user);
// Jump back to first page if we've run out of entries for this page
if (!collection.size())
collection.fetch();
};
this.alertRegion.close();
user.unblock().then(onSuccessfulUnBlock).fail(function (xhr) {
var response = xhr && xhr.responseJSON && xhr.responseJSON;
var errorCode = response && response.code;
// If the user is already blocked there is no need to display an alert
if (
errorCode === api.ERROR_CODES.OBJ_NOT_FOUND &&
response.response === 'UserBlock matching query does not exist.'
)
return void onSuccessfulUnBlock();
this.alertRegion.show(new Marionette.ItemView({
template: settingsBlockingAlertErrorTemplate,
}));
}.bind(this));
},
onDomRefresh: function () {
this.usersRegion.show(new SettingsBlockingUserListView({
collection: this.collection,
}));
this.buttonsRegion.show(new SettingsUserBlockingNavView({
collection: this.collection,
}));
},
});
return SettingsBlockingView;
});