DEV: Refactor self-delete dialog (#18180)

Also adds missing test
This commit is contained in:
Penar Musaraj 2022-09-12 15:45:53 +02:00 committed by GitHub
parent fa58eea64e
commit 5c0f770f0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 27 deletions

View File

@ -4,14 +4,16 @@ import CanCheckEmails from "discourse/mixins/can-check-emails";
import Controller from "@ember/controller"; import Controller from "@ember/controller";
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
import I18n from "I18n"; import I18n from "I18n";
import bootbox from "bootbox";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
import { findAll } from "discourse/models/login-method"; import { findAll } from "discourse/models/login-method";
import DiscourseURL from "discourse/lib/url";
import getURL from "discourse-common/lib/get-url"; import getURL from "discourse-common/lib/get-url";
import { iconHTML } from "discourse-common/lib/icon-library";
import { popupAjaxError } from "discourse/lib/ajax-error"; import { popupAjaxError } from "discourse/lib/ajax-error";
import { inject as service } from "@ember/service";
import { next } from "@ember/runloop";
export default Controller.extend(CanCheckEmails, { export default Controller.extend(CanCheckEmails, {
dialog: service(),
init() { init() {
this._super(...arguments); this._super(...arguments);
@ -168,39 +170,38 @@ export default Controller.extend(CanCheckEmails, {
}, },
delete() { delete() {
this.set("deleting", true); this.dialog.alert({
const message = I18n.t("user.delete_account_confirm"), message: I18n.t("user.delete_account_confirm"),
model = this.model, buttons: [
buttons = [
{ {
label: I18n.t("cancel"), icon: "exclamation-triangle",
class: "d-modal-cancel", label: I18n.t("user.delete_account"),
link: true, class: "btn-danger",
callback: () => { action: () => {
this.set("deleting", false); return this.model.delete().then(
},
},
{
label:
iconHTML("exclamation-triangle") + I18n.t("user.delete_account"),
class: "btn btn-danger",
callback() {
model.delete().then(
() => { () => {
bootbox.alert( next(() => {
I18n.t("user.deleted_yourself"), this.dialog.alert({
() => (window.location = getURL("/")) message: I18n.t("user.deleted_yourself"),
); didConfirm: () =>
DiscourseURL.redirectAbsolute(getURL("/")),
didCancel: () =>
DiscourseURL.redirectAbsolute(getURL("/")),
});
});
}, },
() => { () => {
bootbox.alert(I18n.t("user.delete_yourself_not_allowed")); this.dialog.alert(I18n.t("user.delete_yourself_not_allowed"));
this.set("deleting", false); this.set("deleting", false);
} }
); );
}, },
}, },
]; {
bootbox.dialog(message, buttons, { classes: "delete-account" }); label: I18n.t("composer.cancel"),
},
],
});
}, },
revokeAccount(account) { revokeAccount(account) {
@ -212,7 +213,7 @@ export default Controller.extend(CanCheckEmails, {
if (result.success) { if (result.success) {
this.model.associated_accounts.removeObject(account); this.model.associated_accounts.removeObject(account);
} else { } else {
bootbox.alert(result.message); this.dialog.alert(result.message);
} }
}) })
.catch(popupAjaxError) .catch(popupAjaxError)

View File

@ -0,0 +1,38 @@
import { acceptance, query } from "discourse/tests/helpers/qunit-helpers";
import { click, visit } from "@ember/test-helpers";
import { test } from "qunit";
import DiscourseURL from "discourse/lib/url";
import I18n from "I18n";
import sinon from "sinon";
acceptance("User Profile - Account - Self Delete", function (needs) {
needs.user({
username: "charlie",
});
needs.pretender((server, helper) => {
server.delete("/u/charlie.json", () => helper.response({ success: true }));
});
test("Delete dialog", async function (assert) {
sinon.stub(DiscourseURL, "redirectAbsolute");
await visit("/u/charlie/preferences/account");
await click(".delete-account .btn-danger");
await click(".dialog-footer .btn-danger");
assert.strictEqual(
query(".dialog-body").textContent.trim(),
I18n.t("user.deleted_yourself"),
"confirmation dialog is shown"
);
await click(".dialog-footer .btn-primary");
assert.ok(
DiscourseURL.redirectAbsolute.calledWith("/"),
"redirects to home after deleting"
);
});
});

View File

@ -2499,6 +2499,7 @@ export default {
last_posted_at: null, last_posted_at: null,
last_seen_at: null, last_seen_at: null,
created_at: "2019-03-06T19:06:20.340Z", created_at: "2019-03-06T19:06:20.340Z",
can_delete_account: true,
can_edit: true, can_edit: true,
can_edit_username: true, can_edit_username: true,
can_edit_email: true, can_edit_email: true,