diff --git a/app/assets/javascripts/admin/components/screened-ip-address-form-component.js.es6 b/app/assets/javascripts/admin/components/screened-ip-address-form-component.js.es6 deleted file mode 100644 index 8702dbb2f..000000000 --- a/app/assets/javascripts/admin/components/screened-ip-address-form-component.js.es6 +++ /dev/null @@ -1,79 +0,0 @@ -import ScreenedIpAddress from 'admin/models/screened-ip-address'; -/** - 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 -**/ -const ScreenedIpAddressFormComponent = Ember.Component.extend({ - classNames: ['screened-ip-address-form'], - formSubmitted: false, - actionName: 'block', - - adminWhitelistEnabled: function() { - return Discourse.SiteSettings.use_admin_ip_whitelist; - }.property(), - - actionNames: function() { - if (this.get('adminWhitelistEnabled')) { - 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')}, - {id: 'allow_admin', name: I18n.t('admin.logs.screened_ips.actions.allow_admin')} - ]; - } else { - 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('adminWhitelistEnabled'), - - actions: { - submit: function() { - if (!this.get('formSubmitted')) { - var self = this; - this.set('formSubmitted', true); - var screenedIpAddress = 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', 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() { - 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'); - } - }); - }); - } -}); - -export default ScreenedIpAddressFormComponent; diff --git a/app/assets/javascripts/admin/components/screened-ip-address-form.js.es6 b/app/assets/javascripts/admin/components/screened-ip-address-form.js.es6 new file mode 100644 index 000000000..0f8a29ba8 --- /dev/null +++ b/app/assets/javascripts/admin/components/screened-ip-address-form.js.es6 @@ -0,0 +1,75 @@ +/** + 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. +**/ + +import ScreenedIpAddress from 'admin/models/screened-ip-address'; +import computed from 'ember-addons/ember-computed-decorators'; +import { on } from 'ember-addons/ember-computed-decorators'; + +export default Ember.Component.extend({ + classNames: ['screened-ip-address-form'], + formSubmitted: false, + actionName: 'block', + + @computed + adminWhitelistEnabled() { + return Discourse.SiteSettings.use_admin_ip_whitelist; + }, + + @computed("adminWhitelistEnabled") + actionNames(adminWhitelistEnabled) { + if (adminWhitelistEnabled) { + 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')}, + {id: 'allow_admin', name: I18n.t('admin.logs.screened_ips.actions.allow_admin')} + ]; + } else { + 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')} + ]; + } + }, + + actions: { + submit() { + if (!this.get('formSubmitted')) { + this.set('formSubmitted', true); + const screenedIpAddress = ScreenedIpAddress.create({ + ip_address: this.get('ip_address'), + action_name: this.get('actionName') + }); + screenedIpAddress.save().then(result => { + this.setProperties({ ip_address: '', formSubmitted: false }); + this.sendAction('action', ScreenedIpAddress.create(result.screened_ip_address)); + Ember.run.schedule('afterRender', () => this.$('.ip-address-input').focus()); + }).catch(e => { + this.set('formSubmitted', false); + const msg = (e.responseJSON && e.responseJSON.errors) ? + I18n.t("generic_error_with_reason", {error: e.responseJSON.errors.join('. ')}) : + I18n.t("generic_error"); + bootbox.alert(msg, () => this.$('.ip-address-input').focus()); + }); + } + } + }, + + @on("didInsertElement") + _init() { + Ember.run.schedule('afterRender', () => { + this.$('.ip-address-input').keydown(e => { + if (e.keyCode === 13) { + this.send('submit'); + } + }); + }); + } +}); diff --git a/app/assets/javascripts/discourse/templates/components/screened-ip-address-form.hbs b/app/assets/javascripts/admin/templates/components/screened-ip-address-form.hbs similarity index 65% rename from app/assets/javascripts/discourse/templates/components/screened-ip-address-form.hbs rename to app/assets/javascripts/admin/templates/components/screened-ip-address-form.hbs index 5f58a282c..e846b1c65 100644 --- a/app/assets/javascripts/discourse/templates/components/screened-ip-address-form.hbs +++ b/app/assets/javascripts/admin/templates/components/screened-ip-address-form.hbs @@ -1,4 +1,4 @@ {{i18n 'admin.logs.screened_ips.form.label'}} {{text-field value=ip_address disabled=formSubmitted class="ip-address-input" placeholderKey="admin.logs.screened_ips.form.ip_address" autocorrect="off" autocapitalize="off"}} {{combo-box content=actionNames value=actionName}} - +{{d-button action="submit" disabled=formSubmitted label="admin.logs.screened_ips.form.add"}} diff --git a/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.hbs b/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.hbs index 515c22a3a..5aaada9ea 100644 --- a/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.hbs +++ b/app/assets/javascripts/admin/templates/logs/screened_ip_addresses.hbs @@ -1,11 +1,14 @@
{{i18n 'admin.logs.screened_ips.description'}}
+