REFACTOR: choose-topic component (#7636)

This commit is contained in:
Joffrey JAFFEUX 2019-05-29 16:21:13 +02:00 committed by GitHub
parent b2fc80e4b7
commit bb6337d6c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 22 deletions

View File

@ -1,34 +1,44 @@
import debounce from "discourse/lib/debounce"; import debounce from "discourse/lib/debounce";
import { searchForTerm } from "discourse/lib/search"; import { searchForTerm } from "discourse/lib/search";
import { observes } from "ember-addons/ember-computed-decorators";
export default Ember.Component.extend({ export default Ember.Component.extend({
loading: null, loading: null,
noResults: null, noResults: null,
topics: null, topics: null,
selectedTopicId: null,
currentTopicId: null,
topicTitle: null,
topicTitleChanged: function() { @observes("topicTitle")
topicTitleChanged() {
this.setProperties({ this.setProperties({
loading: true, loading: true,
noResults: true, noResults: true,
selectedTopicId: null selectedTopicId: null
}); });
this.search(this.topicTitle);
}.observes("topicTitle"),
topicsChanged: function() { this.search(this.topicTitle);
const topics = this.topics; },
if (topics) {
this.set("noResults", topics.length === 0); @observes("topics")
topicsChanged() {
if (this.topics) {
this.set("noResults", this.topics.length === 0);
} }
this.set("loading", false); this.set("loading", false);
}.observes("topics"), },
search: debounce(function(title) { search: debounce(function(title) {
const self = this, if (!this.element || this.isDestroying || this.isDestroyed) {
currentTopicId = this.currentTopicId; return;
}
const currentTopicId = this.currentTopicId;
if (Ember.isEmpty(title)) { if (Ember.isEmpty(title)) {
self.setProperties({ topics: null, loading: false }); this.setProperties({ topics: null, loading: false });
return; return;
} }
@ -36,27 +46,24 @@ export default Ember.Component.extend({
typeFilter: "topic", typeFilter: "topic",
searchForId: true, searchForId: true,
restrictToArchetype: "regular" restrictToArchetype: "regular"
}).then(function(results) { }).then(results => {
if (results && results.posts && results.posts.length > 0) { if (results && results.posts && results.posts.length > 0) {
self.set( this.set(
"topics", "topics",
results.posts results.posts.mapBy("topic").filter(t => t.id !== currentTopicId)
.mapBy("topic")
.filter(t => t.get("id") !== currentTopicId)
); );
} else { } else {
self.setProperties({ topics: null, loading: false }); this.setProperties({ topics: null, loading: false });
} }
}); });
}, 300), }, 300),
actions: { actions: {
chooseTopic(topic) { chooseTopic(topic) {
const topicId = Ember.get(topic, "id"); this.set("selectedTopicId", topic.id);
this.set("selectedTopicId", topicId); Ember.run.next(() => {
Ember.run.next(() => document.getElementById(`choose-topic-${topic.id}`).checked = true;
$("#choose-topic-" + topicId).prop("checked", "true") });
);
return false; return false;
} }
} }