DEV: Replace Category.findById with Category.asyncFindByIds in easy cases (#26270)

This commit is contained in:
Daniel Waterworth 2024-04-05 11:59:10 -05:00 committed by GitHub
parent 1df97e86c1
commit cafdc29806
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 37 additions and 28 deletions

View File

@ -22,6 +22,7 @@ export class MultiCache {
this.fetchTimes = [this.fetchTimes[this.fetchTimes.length - 1], new Date()]; this.fetchTimes = [this.fetchTimes[this.fetchTimes.length - 1], new Date()];
const notFound = []; const notFound = [];
ids = ids.uniq();
for (const id of ids) { for (const id of ids) {
if (!this.values.has(id)) { if (!this.values.has(id)) {

View File

@ -190,10 +190,9 @@ export default class History extends Component {
); );
} }
revert(post, postVersion) { async revert(post, postVersion) {
post try {
.revertToRevision(postVersion) const result = await post.revertToRevision(postVersion);
.then((result) => {
this.refresh(post.id, postVersion); this.refresh(post.id, postVersion);
if (result.topic) { if (result.topic) {
post.set("topic.slug", result.topic.slug); post.set("topic.slug", result.topic.slug);
@ -201,15 +200,17 @@ export default class History extends Component {
post.set("topic.fancy_title", result.topic.fancy_title); post.set("topic.fancy_title", result.topic.fancy_title);
} }
if (result.category_id) { if (result.category_id) {
post.set("topic.category", Category.findById(result.category_id)); post.set(
"topic.category",
await Category.asyncFindById(result.category_id)
);
} }
this.args.closeModal(); this.args.closeModal();
}) } catch (e) {
.catch((e) => {
if (e.jqXHR.responseJSON?.errors?.[0]) { if (e.jqXHR.responseJSON?.errors?.[0]) {
this.dialog.alert(e.jqXHR.responseJSON.errors[0]); this.dialog.alert(e.jqXHR.responseJSON.errors[0]);
} }
}); }
} }
get editButtonLabel() { get editButtonLabel() {

View File

@ -157,6 +157,10 @@ export default class Category extends RestModel {
return categories; return categories;
} }
static async asyncFindById(id) {
return (await Category.asyncFindByIds([id]))[0];
}
static findBySlugAndParent(slug, parentCategory) { static findBySlugAndParent(slug, parentCategory) {
if (this.slugEncoded()) { if (this.slugEncoded()) {
slug = encodeURI(slug); slug = encodeURI(slug);

View File

@ -450,7 +450,7 @@ export default class Group extends RestModel {
}); });
} }
findPosts(opts) { async findPosts(opts) {
opts = opts || {}; opts = opts || {};
const type = opts.type || "posts"; const type = opts.type || "posts";
const data = {}; const data = {};
@ -463,14 +463,17 @@ export default class Group extends RestModel {
data.category_id = parseInt(opts.categoryId, 10); data.category_id = parseInt(opts.categoryId, 10);
} }
return ajax(`/groups/${this.name}/${type}.json`, { data }).then((posts) => { const posts = await ajax(`/groups/${this.name}/${type}.json`, { data });
const categories = await Category.asyncFindByIds(
posts.map((p) => p.category_id)
);
return posts.map((p) => { return posts.map((p) => {
p.user = User.create(p.user); p.user = User.create(p.user);
p.topic = Topic.create(p.topic); p.topic = Topic.create(p.topic);
p.category = Category.findById(p.category_id); p.category = categories[p.category_id];
return EmberObject.create(p); return EmberObject.create(p);
}); });
});
} }
setNotification(notification_level, userId) { setNotification(notification_level, userId) {