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