DEV: Convert `delete-user-posts-progress` modal to component-based API (#22916)
https://github.com/discourse/discourse/assets/50783505/414ffcc5-06e9-470f-b160-83b4c12bbb96
This commit is contained in:
parent
514f5d25e6
commit
7c8b0b9869
|
@ -0,0 +1,14 @@
|
|||
<DModal
|
||||
@title={{i18n "admin.user.delete_posts.progress.title"}}
|
||||
@closeModal={{@closeModal}}
|
||||
class="admin-delete-user-posts-progress-modal"
|
||||
@flash={{this.flash}}
|
||||
@dismissable={{false}}
|
||||
>
|
||||
<:body>
|
||||
<p>{{i18n "admin.user.delete_posts.progress.description"}}</p>
|
||||
<div class="progress-bar">
|
||||
<span style={{html-safe (concat "width: " this.deletedPercentage "%")}} />
|
||||
</div>
|
||||
</:body>
|
||||
</DModal>
|
|
@ -0,0 +1,42 @@
|
|||
import Component from "@glimmer/component";
|
||||
import { action } from "@ember/object";
|
||||
import { tracked } from "@glimmer/tracking";
|
||||
import AdminUser from "admin/models/admin-user";
|
||||
import { extractError } from "discourse/lib/ajax-error";
|
||||
import I18n from "I18n";
|
||||
|
||||
export default class DeleteUserPostsProgress extends Component {
|
||||
@tracked deletedPosts = 0;
|
||||
@tracked flash;
|
||||
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.deletePosts();
|
||||
}
|
||||
|
||||
get userPostCount() {
|
||||
return this.args.model.user.get("post_count");
|
||||
}
|
||||
|
||||
get deletedPercentage() {
|
||||
return Math.floor((this.deletedPosts * 100) / this.userPostCount);
|
||||
}
|
||||
|
||||
@action
|
||||
async deletePosts() {
|
||||
try {
|
||||
const progress = await this.args.model.user.deleteAllPosts();
|
||||
this.deletedPosts = progress.posts_deleted;
|
||||
this.args.model.updateUserPostCount(
|
||||
this.userPostCount - this.deletedPosts
|
||||
);
|
||||
// continue deleting posts if more remain, otherwise exit
|
||||
this.userPostCount > 0 ? this.deletePosts() : this.args.closeModal();
|
||||
} catch (e) {
|
||||
AdminUser.find(this.args.model.user.id).then((u) =>
|
||||
this.args.model.user.setProperties(u)
|
||||
);
|
||||
this.flash = extractError(e, I18n.t("admin.user.delete_posts_failed"));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,7 +3,6 @@ import { inject as service } from "@ember/service";
|
|||
import { and, notEmpty } from "@ember/object/computed";
|
||||
import DiscourseURL, { userPath } from "discourse/lib/url";
|
||||
import { fmt, propertyNotEqual, setting } from "discourse/lib/computed";
|
||||
import AdminUser from "admin/models/admin-user";
|
||||
import CanCheckEmails from "discourse/mixins/can-check-emails";
|
||||
import Controller from "@ember/controller";
|
||||
import I18n from "I18n";
|
||||
|
@ -11,12 +10,13 @@ import { ajax } from "discourse/lib/ajax";
|
|||
import discourseComputed from "discourse-common/utils/decorators";
|
||||
import getURL from "discourse-common/lib/get-url";
|
||||
import { htmlSafe } from "@ember/template";
|
||||
import { extractError, popupAjaxError } from "discourse/lib/ajax-error";
|
||||
import showModal from "discourse/lib/show-modal";
|
||||
import { popupAjaxError } from "discourse/lib/ajax-error";
|
||||
import AdminUser from "admin/models/admin-user";
|
||||
import MergeUsersConfirmationModal from "../components/modal/merge-users-confirmation";
|
||||
import MergeUsersPromptModal from "../components/modal/merge-users-prompt";
|
||||
import MergeUsersProgressModal from "../components/modal/merge-users-progress";
|
||||
import DeletePostsConfirmationModal from "../components/modal/delete-posts-confirmation";
|
||||
import DeleteUserPostsProgressModal from "../components/modal/delete-user-posts-progress";
|
||||
|
||||
export default class AdminUserIndexController extends Controller.extend(
|
||||
CanCheckEmails
|
||||
|
@ -628,42 +628,18 @@ export default class AdminUserIndexController extends Controller.extend(
|
|||
});
|
||||
}
|
||||
|
||||
@action
|
||||
updateUserPostCount(count) {
|
||||
this.model.set("post_count", count);
|
||||
}
|
||||
|
||||
@action
|
||||
deleteAllPosts() {
|
||||
let deletedPosts = 0;
|
||||
let deletedPercentage = 0;
|
||||
const user = this.model;
|
||||
|
||||
const performDelete = (progressModal) => {
|
||||
this.model
|
||||
.deleteAllPosts()
|
||||
.then(({ posts_deleted }) => {
|
||||
if (posts_deleted === 0) {
|
||||
user.set("post_count", 0);
|
||||
progressModal.send("closeModal");
|
||||
} else {
|
||||
deletedPosts += posts_deleted;
|
||||
deletedPercentage = Math.floor(
|
||||
(deletedPosts * 100) / user.get("post_count")
|
||||
);
|
||||
progressModal.setProperties({
|
||||
deletedPercentage,
|
||||
});
|
||||
performDelete(progressModal);
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
progressModal.send("closeModal");
|
||||
let error;
|
||||
AdminUser.find(user.get("id")).then((u) => user.setProperties(u));
|
||||
error = extractError(e, I18n.t("admin.user.delete_posts_failed"));
|
||||
this.dialog.alert(error);
|
||||
});
|
||||
};
|
||||
|
||||
const progressModal = showModal("admin-delete-user-posts-progress", {
|
||||
admin: true,
|
||||
this.modal.show(DeleteUserPostsProgressModal, {
|
||||
model: {
|
||||
user: this.model,
|
||||
updateUserPostCount: this.updateUserPostCount,
|
||||
},
|
||||
});
|
||||
performDelete(progressModal);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
import Controller from "@ember/controller";
|
||||
import ModalFunctionality from "discourse/mixins/modal-functionality";
|
||||
|
||||
export default class AdminDeleteUserPostsProgressController extends Controller.extend(
|
||||
ModalFunctionality
|
||||
) {
|
||||
deletedPercentage = 0;
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
<DModalBody
|
||||
@title="admin.user.delete_posts.progress.title"
|
||||
@dismissable={{false}}
|
||||
>
|
||||
<p>{{I18n "admin.user.delete_posts.progress.description"}}</p>
|
||||
<div class="progress-bar"><span
|
||||
style={{html-safe (concat "width: " this.deletedPercentage "%")}}
|
||||
></span></div>
|
||||
</DModalBody>
|
|
@ -49,7 +49,6 @@ const KNOWN_LEGACY_MODALS = [
|
|||
"topic-summary",
|
||||
"user-status",
|
||||
"admin-watched-word-test",
|
||||
"admin-delete-user-posts-progress",
|
||||
"admin-install-theme",
|
||||
"admin-penalize-user",
|
||||
"admin-theme-change",
|
||||
|
|
Loading…
Reference in New Issue