mirror of
https://github.com/discourse/discourse.git
synced 2024-12-07 03:23:45 +08:00
0756486b65
Why this change? A system test which was testing our ability to add a form template to a category was flaky. This turned out to be a client side bug where the `FormTemplateChooser` dropdown may not display any dropdown options if the ajax request to fetch the categories form template has not been completed when the dropdown is opened. What does this change do? Make use of select-kit's `triggerSearch` function to fetch the records which will properly rerender the dropdown options.
71 lines
1.6 KiB
JavaScript
71 lines
1.6 KiB
JavaScript
import { computed } from "@ember/object";
|
|
import FormTemplate from "discourse/models/form-template";
|
|
import MultiSelectComponent from "select-kit/components/multi-select";
|
|
|
|
export default MultiSelectComponent.extend({
|
|
pluginApiIdentifiers: ["form-template-chooser"],
|
|
classNames: ["form-template-chooser"],
|
|
selectKitOptions: {
|
|
none: "form_template_chooser.select_template",
|
|
},
|
|
|
|
init() {
|
|
this._super(...arguments);
|
|
this.triggerSearch();
|
|
},
|
|
|
|
didUpdateAttrs() {
|
|
this._super(...arguments);
|
|
this.set("templatesLoaded", false);
|
|
this.triggerSearch();
|
|
},
|
|
|
|
@computed("templates")
|
|
get content() {
|
|
return this.templates;
|
|
},
|
|
|
|
search(filter) {
|
|
if (this.get("templatesLoaded")) {
|
|
return this._super(filter);
|
|
} else {
|
|
return this._fetchTemplates();
|
|
}
|
|
},
|
|
|
|
async _fetchTemplates() {
|
|
if (this.get("loadingTemplates")) {
|
|
return;
|
|
}
|
|
|
|
this.set("templatesLoaded", false);
|
|
this.set("loadingTemplates", true);
|
|
|
|
const result = await FormTemplate.findAll();
|
|
|
|
let sortedTemplates = this._sortTemplatesByName(result);
|
|
|
|
if (this.filteredIds) {
|
|
sortedTemplates = sortedTemplates.filter((t) =>
|
|
this.filteredIds.includes(t.id)
|
|
);
|
|
}
|
|
|
|
if (sortedTemplates.length === 0) {
|
|
this.set("selectKit.options.disabled", true);
|
|
}
|
|
|
|
this.setProperties({
|
|
templates: sortedTemplates,
|
|
loadingTemplates: false,
|
|
templatesLoaded: true,
|
|
});
|
|
|
|
return this.templates;
|
|
},
|
|
|
|
_sortTemplatesByName(templates) {
|
|
return templates.sort((a, b) => a.name.localeCompare(b.name));
|
|
},
|
|
});
|