diff --git a/app/assets/javascripts/admin/components/screened_ip_address_form_component.js b/app/assets/javascripts/admin/components/screened_ip_address_form_component.js new file mode 100644 index 00000000000..dbae5a9aac9 --- /dev/null +++ b/app/assets/javascripts/admin/components/screened_ip_address_form_component.js @@ -0,0 +1,64 @@ +/** + A form to create an IP address that will be blocked or whitelisted. + Example usage: + + {{screened-ip-address-form action="recordAdded"}} + + where action is a callback on the controller or route that will get called after + the new record is successfully saved. It is called with the new ScreenedIpAddress record + as an argument. + + @class ScreenedIpAddressFormComponent + @extends Ember.Component + @namespace Discourse + @module Discourse +**/ +Discourse.ScreenedIpAddressFormComponent = Ember.Component.extend({ + classNames: ['screened-ip-address-form'], + formSubmitted: false, + actionName: 'block', + + actionNames: function() { + return [ + {id: 'block', name: I18n.t('admin.logs.screened_ips.actions.block')}, + {id: 'do_nothing', name: I18n.t('admin.logs.screened_ips.actions.do_nothing')} + ]; + }.property(), + + actions: { + submit: function() { + if (!this.get('formSubmitted')) { + var self = this; + this.set('formSubmitted', true); + var screenedIpAddress = Discourse.ScreenedIpAddress.create({ip_address: this.get('ip_address'), action_name: this.get('actionName')}); + screenedIpAddress.save().then(function(result) { + self.set('ip_address', ''); + self.set('formSubmitted', false); + self.sendAction('action', Discourse.ScreenedIpAddress.create(result.screened_ip_address)); + Em.run.schedule('afterRender', function() { self.$('.ip-address-input').focus(); }); + }, function(e) { + self.set('formSubmitted', false); + var msg; + if (e.responseJSON && e.responseJSON.errors) { + msg = I18n.t("generic_error_with_reason", {error: e.responseJSON.errors.join('. ')}); + } else { + msg = I18n.t("generic_error"); + } + bootbox.alert(msg, function() { self.$('.ip-address-input').focus(); }); + }); + } + } + }, + + didInsertElement: function(e) { + var self = this; + this._super(); + Em.run.schedule('afterRender', function() { + self.$('.ip-address-input').keydown(function(e) { + if (e.keyCode === 13) { // enter key + self.send('submit'); + } + }); + }); + } +}); diff --git a/app/assets/javascripts/admin/controllers/admin_logs_screened_ip_addresses_controller.js b/app/assets/javascripts/admin/controllers/admin_logs_screened_ip_addresses_controller.js index 303b7fb3434..bbf06d69921 100644 --- a/app/assets/javascripts/admin/controllers/admin_logs_screened_ip_addresses_controller.js +++ b/app/assets/javascripts/admin/controllers/admin_logs_screened_ip_addresses_controller.js @@ -18,6 +18,12 @@ Discourse.AdminLogsScreenedIpAddressesController = Ember.ArrayController.extend( self.set('content', result); self.set('loading', false); }); + }, + + actions: { + recordAdded: function(arg) { + this.get("content").unshiftObject(arg); + } } }); @@ -27,12 +33,12 @@ Discourse.AdminLogsScreenedIpAddressController = Ember.ObjectController.extend({ actions: { allow: function(record) { - record.set('action', 'do_nothing'); + record.set('action_name', 'do_nothing'); this.send('save', record); }, block: function(record) { - record.set('action', 'block'); + record.set('action_name', 'block'); this.send('save', record); }, diff --git a/app/assets/javascripts/admin/models/screened_ip_address.js b/app/assets/javascripts/admin/models/screened_ip_address.js index 5a635214d1e..b209d7f6205 100644 --- a/app/assets/javascripts/admin/models/screened_ip_address.js +++ b/app/assets/javascripts/admin/models/screened_ip_address.js @@ -9,20 +9,20 @@ **/ Discourse.ScreenedIpAddress = Discourse.Model.extend({ actionName: function() { - return I18n.t("admin.logs.screened_ips.actions." + this.get('action')); - }.property('action'), + return I18n.t("admin.logs.screened_ips.actions." + this.get('action_name')); + }.property('action_name'), isBlocked: function() { - return (this.get('action') === 'block'); - }.property('action'), + return (this.get('action_name') === 'block'); + }.property('action_name'), actionIcon: function() { - if (this.get('action') === 'block') { + if (this.get('action_name') === 'block') { return this.get('blockIcon'); } else { return this.get('doNothingIcon'); } - }.property('action'), + }.property('action_name'), blockIcon: function() { return 'icon-ban-circle'; @@ -33,9 +33,9 @@ Discourse.ScreenedIpAddress = Discourse.Model.extend({ }.property(), save: function() { - return Discourse.ajax("/admin/logs/screened_ip_addresses/" + this.get('id') + ".json", { - type: 'PUT', - data: {ip_address: this.get('ip_address'), action_name: this.get('action')} + return Discourse.ajax("/admin/logs/screened_ip_addresses" + (this.id ? '/' + this.id : '') + ".json", { + type: this.id ? 'PUT' : 'POST', + data: {ip_address: this.get('ip_address'), action_name: this.get('action_name')} }); }, diff --git a/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.js.handlebars b/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.js.handlebars index b929daf340c..920306ae7e6 100644 --- a/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.js.handlebars +++ b/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.js.handlebars @@ -1,5 +1,8 @@
{{i18n admin.logs.screened_ips.description}}
+{{screened-ip-address-form action="recordAdded"}} +