discourse/app/assets/javascripts/admin/controllers/admin-logs-screened-ip-addresses.js.es6
Jarek Radosz fe588cc7f8
DEV: Fix function prototype deprecations (#8681)
* DEV: Fix the function prototype observers deprecation

DEPRECATION: Function prototype extensions have been deprecated, please migrate from function(){}.observes('foo') to observer('foo', function() {}). [deprecation id: function-prototype-extensions.observes] See https://deprecations.emberjs.com/v3.x/#toc_function-prototype-extensions-observes for more details.

* DEV: Fix the function prototype event listeners deprecation

DEPRECATION: Function prototype extensions have been deprecated, please migrate from function(){}.on('foo') to on('foo', function() {}). [deprecation id: function-prototype-extensions.on] See https://deprecations.emberjs.com/v3.x/#toc_function-prototype-extensions-on for more details.

* DEV: Simplify `default as` imports

Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
2020-01-16 18:56:53 +01:00

136 lines
3.7 KiB
JavaScript

import Controller from "@ember/controller";
import discourseDebounce from "discourse/lib/debounce";
import { outputExportResult } from "discourse/lib/export-result";
import { exportEntity } from "discourse/lib/export-csv";
import ScreenedIpAddress from "admin/models/screened-ip-address";
import { observes } from "discourse-common/utils/decorators";
export default Controller.extend({
loading: false,
filter: null,
savedIpAddress: null,
@observes("filter")
show: discourseDebounce(function() {
this.set("loading", true);
ScreenedIpAddress.findAll(this.filter).then(result => {
this.setProperties({ model: result, loading: false });
});
}, 250),
actions: {
allow(record) {
record.set("action_name", "do_nothing");
record.save();
},
block(record) {
record.set("action_name", "block");
record.save();
},
edit(record) {
if (!record.get("editing")) {
this.set("savedIpAddress", record.get("ip_address"));
}
record.set("editing", true);
},
cancel(record) {
const savedIpAddress = this.savedIpAddress;
if (savedIpAddress && record.get("editing")) {
record.set("ip_address", savedIpAddress);
}
record.set("editing", false);
},
save(record) {
const wasEditing = record.get("editing");
record.set("editing", false);
record
.save()
.then(() => this.set("savedIpAddress", null))
.catch(e => {
if (e.jqXHR.responseJSON && e.jqXHR.responseJSON.errors) {
bootbox.alert(
I18n.t("generic_error_with_reason", {
error: e.jqXHR.responseJSON.errors.join(". ")
})
);
} else {
bootbox.alert(I18n.t("generic_error"));
}
if (wasEditing) record.set("editing", true);
});
},
destroy(record) {
return bootbox.confirm(
I18n.t("admin.logs.screened_ips.delete_confirm", {
ip_address: record.get("ip_address")
}),
I18n.t("no_value"),
I18n.t("yes_value"),
result => {
if (result) {
record
.destroy()
.then(deleted => {
if (deleted) {
this.model.removeObject(record);
} else {
bootbox.alert(I18n.t("generic_error"));
}
})
.catch(e => {
bootbox.alert(
I18n.t("generic_error_with_reason", {
error: `http: ${e.status} - ${e.body}`
})
);
});
}
}
);
},
recordAdded(arg) {
this.model.unshiftObject(arg);
},
rollUp() {
return bootbox.confirm(
I18n.t("admin.logs.screened_ips.roll_up_confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
confirmed => {
if (confirmed) {
this.set("loading", true);
return ScreenedIpAddress.rollUp().then(results => {
if (results && results.subnets) {
if (results.subnets.length > 0) {
this.send("show");
bootbox.alert(
I18n.t("admin.logs.screened_ips.rolled_up_some_subnets", {
subnets: results.subnets.join(", ")
})
);
} else {
this.set("loading", false);
bootbox.alert(
I18n.t("admin.logs.screened_ips.rolled_up_no_subnet")
);
}
}
});
}
}
);
},
exportScreenedIpList() {
exportEntity("screened_ip").then(outputExportResult);
}
}
});