import Component from "@glimmer/component"; import { tracked } from "@glimmer/tracking"; import { on } from "@ember/modifier"; import { action } from "@ember/object"; import { service } from "@ember/service"; import { TrackedArray } from "@ember-compat/tracked-built-ins"; import DButton from "discourse/components/d-button"; import DModal from "discourse/components/d-modal"; import { ajax } from "discourse/lib/ajax"; import { extractError } from "discourse/lib/ajax-error"; import { bind } from "discourse-common/utils/decorators"; import { i18n } from "discourse-i18n"; const BULK_DELETE_CHANNEL = "/bulk-user-delete"; export default class BulkUserDeleteConfirmation extends Component { @service messageBus; @tracked confirmButtonDisabled = true; @tracked deleteStarted = false; @tracked logs = new TrackedArray(); failedUsernames = []; callAfterBulkDelete = false; blockIpAndEmail = false; constructor() { super(...arguments); this.messageBus.subscribe(BULK_DELETE_CHANNEL, this.onDeleteProgress); } willDestroy() { super.willDestroy(...arguments); this.messageBus.unsubscribe(BULK_DELETE_CHANNEL, this.onDeleteProgress); } get confirmDeletePhrase() { return i18n( "admin.users.bulk_actions.delete.confirmation_modal.confirmation_phrase", { count: this.args.model.userIds.length } ); } #logError(line) { this.#log(line, "error"); } #logSuccess(line) { this.#log(line, "success"); } #logNeutral(line) { this.#log(line, "neutral"); } #log(line, type) { this.logs.push({ line, type, }); } @bind onDeleteProgress(data) { if (data.success) { this.#logSuccess( i18n( "admin.users.bulk_actions.delete.confirmation_modal.user_delete_succeeded", data ) ); } else if (data.failed) { this.failedUsernames.push(data.username); this.#logError( i18n( "admin.users.bulk_actions.delete.confirmation_modal.user_delete_failed", data ) ); } if (data.position === data.total) { this.callAfterBulkDelete = true; this.#logNeutral( i18n( "admin.users.bulk_actions.delete.confirmation_modal.bulk_delete_finished" ) ); if (this.failedUsernames.length > 0) { this.#logNeutral( i18n( "admin.users.bulk_actions.delete.confirmation_modal.failed_to_delete_users" ) ); for (const username of this.failedUsernames) { this.#logNeutral(`* ${username}`); } } } } @action onPromptInput(event) { this.confirmButtonDisabled = event.target.value.toLowerCase() !== this.confirmDeletePhrase; } @action async startDelete() { this.deleteStarted = true; this.confirmButtonDisabled = true; this.#logNeutral( i18n( "admin.users.bulk_actions.delete.confirmation_modal.bulk_delete_starting" ) ); try { await ajax("/admin/users/destroy-bulk.json", { type: "DELETE", data: { user_ids: this.args.model.userIds, block_ip_and_email: this.blockIpAndEmail, }, }); this.callAfterBulkDelete = true; } catch (err) { this.#logError(extractError(err)); this.confirmButtonDisabled = false; } } @action closeModal() { this.args.closeModal(); if (this.callAfterBulkDelete) { this.args.model?.afterBulkDelete(); } } @action toggleBlockIpAndEmail(event) { this.blockIpAndEmail = event.target.checked; } }