From ea8373351bc9e2a00645e045cea6ef9043c0ded0 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Tue, 22 Jan 2019 15:09:04 +0100 Subject: [PATCH] DEV: refactoring ip-lookup (#6923) --- .../admin/components/ip-lookup.js.es6 | 65 +++++++++---------- .../templates/components/ip-lookup.hbs | 52 ++++++++++----- 2 files changed, 64 insertions(+), 53 deletions(-) diff --git a/app/assets/javascripts/admin/components/ip-lookup.js.es6 b/app/assets/javascripts/admin/components/ip-lookup.js.es6 index 1ddb37faf7c..bea470f8cb1 100644 --- a/app/assets/javascripts/admin/components/ip-lookup.js.es6 +++ b/app/assets/javascripts/admin/components/ip-lookup.js.es6 @@ -1,3 +1,4 @@ +import { default as computed } from "ember-addons/ember-computed-decorators"; import { ajax } from "discourse/lib/ajax"; import AdminUser from "admin/models/admin-user"; import copyText from "discourse/lib/copy-text"; @@ -5,43 +6,39 @@ import copyText from "discourse/lib/copy-text"; export default Ember.Component.extend({ classNames: ["ip-lookup"], - otherAccountsToDelete: function() { + @computed("other_accounts.length", "totalOthersWithSameIP") + otherAccountsToDelete(otherAccountsLength, totalOthersWithSameIP) { // can only delete up to 50 accounts at a time - var total = Math.min(50, this.get("totalOthersWithSameIP") || 0); - var visible = Math.min(50, this.get("other_accounts.length") || 0); + const total = Math.min(50, totalOthersWithSameIP || 0); + const visible = Math.min(50, otherAccountsLength || 0); return Math.max(visible, total); - }.property("other_accounts", "totalOthersWithSameIP"), + }, actions: { - lookup: function() { - var self = this; + lookup() { this.set("show", true); if (!this.get("location")) { - ajax("/admin/users/ip-info", { - data: { ip: this.get("ip") } - }).then(function(location) { - self.set("location", Ember.Object.create(location)); - }); + ajax("/admin/users/ip-info", { data: { ip: this.get("ip") } }).then( + location => this.set("location", Ember.Object.create(location)) + ); } if (!this.get("other_accounts")) { this.set("otherAccountsLoading", true); - var data = { + const data = { ip: this.get("ip"), exclude: this.get("userId"), order: "trust_level DESC" }; - ajax("/admin/users/total-others-with-same-ip", { data }).then(function( - result - ) { - self.set("totalOthersWithSameIP", result.total); - }); + ajax("/admin/users/total-others-with-same-ip", { data }).then(result => + this.set("totalOthersWithSameIP", result.total) + ); - AdminUser.findAll("active", data).then(function(users) { - self.setProperties({ + AdminUser.findAll("active", data).then(users => { + this.setProperties({ other_accounts: users, otherAccountsLoading: false }); @@ -49,11 +46,11 @@ export default Ember.Component.extend({ } }, - hide: function() { + hide() { this.set("show", false); }, - copy: function() { + copy() { let text = `IP: ${this.get("ip")}\n`; const location = this.get("location"); if (location) { @@ -73,25 +70,25 @@ export default Ember.Component.extend({ text += `: ${location.organization}\n`; } } - const copyRange = $('

'); - copyRange.html(text.trim().replace(/\n/g, "
")); - $(document.body).append(copyRange); - if (copyText(text, copyRange[0])) { + + const $copyRange = $('

'); + $copyRange.html(text.trim().replace(/\n/g, "
")); + $(document.body).append($copyRange); + if (copyText(text, $copyRange[0])) { this.set("copied", true); Ember.run.later(() => this.set("copied", false), 2000); } - copyRange.remove(); + $copyRange.remove(); }, - deleteOtherAccounts: function() { - var self = this; + deleteOtherAccounts() { bootbox.confirm( I18n.t("ip_lookup.confirm_delete_other_accounts"), I18n.t("no_value"), I18n.t("yes_value"), - function(confirmed) { + confirmed => { if (confirmed) { - self.setProperties({ + this.setProperties({ other_accounts: null, otherAccountsLoading: true, totalOthersWithSameIP: null @@ -100,13 +97,11 @@ export default Ember.Component.extend({ ajax("/admin/users/delete-others-with-same-ip.json", { type: "DELETE", data: { - ip: self.get("ip"), - exclude: self.get("userId"), + ip: this.get("ip"), + exclude: this.get("userId"), order: "trust_level DESC" } - }).then(function() { - self.send("lookup"); - }); + }).then(() => this.send("lookup")); } } ); diff --git a/app/assets/javascripts/discourse/templates/components/ip-lookup.hbs b/app/assets/javascripts/discourse/templates/components/ip-lookup.hbs index 573db188560..ebcd9220e59 100644 --- a/app/assets/javascripts/discourse/templates/components/ip-lookup.hbs +++ b/app/assets/javascripts/discourse/templates/components/ip-lookup.hbs @@ -1,36 +1,44 @@ {{#if ip}} {{/if}} {{#if show}}
{{d-icon "times"}} {{#if copied}} - {{d-icon "copy"}} {{i18n "ip_lookup.copied"}} + + {{d-icon "copy"}} + {{i18n "ip_lookup.copied"}} + {{else}} - {{d-icon "copy"}} + + {{d-icon "copy"}} + {{/if}} -

{{i18n 'ip_lookup.title'}}

-

{{{i18n 'ip_lookup.powered_by'}}}

+

{{i18n "ip_lookup.title"}}

+

{{{i18n "ip_lookup.powered_by"}}}

{{#if location}} {{#if location.hostname}} -
{{i18n 'ip_lookup.hostname'}}
+
{{i18n "ip_lookup.hostname"}}
{{location.hostname}}
{{/if}} -
{{i18n 'ip_lookup.location'}}
+
{{i18n "ip_lookup.location"}}
{{#if location.location}} - {{location.location}} + + {{location.location}} + {{else}} - {{i18n 'ip_lookup.location_not_found'}} + {{i18n "ip_lookup.location_not_found"}} {{/if}}
{{#if location.organization}} -
{{i18n 'ip_lookup.organisation'}}
+
{{i18n "ip_lookup.organisation"}}
{{location.organization}}
{{/if}} {{else}} @@ -38,31 +46,39 @@ {{/if}}
- {{i18n 'ip_lookup.other_accounts'}} + {{i18n "ip_lookup.other_accounts"}} {{totalOthersWithSameIP}} {{#if other_accounts.length}} {{/if}}
+ {{#conditional-loading-spinner size="small" condition=otherAccountsLoading}} {{#if other_accounts.length}}
- - - - - + + + + + {{#each other_accounts as |a|}} - +
{{i18n 'ip_lookup.username'}}{{i18n 'ip_lookup.trust_level'}}{{i18n 'ip_lookup.read_time'}}{{i18n 'ip_lookup.topics_entered'}}{{i18n 'ip_lookup.post_count'}}{{i18n "ip_lookup.username"}}{{i18n "ip_lookup.trust_level"}}{{i18n "ip_lookup.read_time"}}{{i18n "ip_lookup.topics_entered"}}{{i18n "ip_lookup.post_count"}}
{{#link-to "adminUser" a}}{{avatar a usernamePath="user.username" imageSize="small"}} {{a.username}}{{/link-to}} + {{#link-to "adminUser" a}} + {{avatar a usernamePath="user.username" imageSize="small"}} +   + {{a.username}} + {{/link-to}} + {{a.trustLevel.id}} {{a.time_read}} {{a.topics_entered}}