DEV: switch to asyncFindByIds in category-list site-setting component (#25592)

We no longer want to rely on preloading every single category, which
means we need to do http requests for categories when we need them.
This commit is contained in:
Daniel Waterworth 2024-02-07 12:07:32 -06:00 committed by GitHub
parent 225db41bfc
commit 67229a7739
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,13 +1,37 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import didUpdate from "@ember/render-modifiers/modifiers/did-update";
import Category from "discourse/models/category";
import htmlSafe from "discourse-common/helpers/html-safe";
import SettingValidationMessage from "admin/components/setting-validation-message";
import CategorySelector from "select-kit/components/category-selector";
export default class CategoryList extends Component {
get selectedCategories() {
return Category.findByIds(this.args.value.split("|").filter(Boolean));
@tracked selectedCategories = [];
constructor() {
super(...arguments);
this.pendingCategoriesRequest = Promise.resolve();
this.valueChanged();
}
get categoryIds() {
return this.args.value
.split("|")
.map((x) => parseInt(x, 10))
.filter(Boolean);
}
async updateSelectedCategories() {
await this.pendingCategoriesRequest;
this.selectedCategories = await Category.asyncFindByIds(this.categoryIds);
}
@action
valueChanged() {
this.pendingCategoriesRequest = this.updateSelectedCategories();
}
@action
@ -16,12 +40,14 @@ export default class CategoryList extends Component {
}
<template>
<CategorySelector
@categories={{this.selectedCategories}}
@onChange={{this.onChangeSelectedCategories}}
/>
<div ...attributes {{didUpdate this.valueChanged @value}}>
<CategorySelector
@categories={{this.selectedCategories}}
@onChange={{this.onChangeSelectedCategories}}
/>
<div class="desc">{{htmlSafe this.setting.description}}</div>
<SettingValidationMessage @message={{this.validationMessage}} />
<div class="desc">{{htmlSafe this.setting.description}}</div>
<SettingValidationMessage @message={{this.validationMessage}} />
</div>
</template>
}