2021-02-01 14:58:37 +01:00
|
|
|
import WizardFieldValidator from "../../wizard/components/validator";
|
2021-02-05 13:59:30 +01:00
|
|
|
import { deepMerge } from "discourse-common/lib/object";
|
|
|
|
import { observes } from "discourse-common/utils/decorators";
|
|
|
|
import { cancel, later } from "@ember/runloop";
|
|
|
|
import { A } from '@ember/array';
|
|
|
|
import EmberObject from "@ember/object";
|
2021-02-01 14:58:37 +01:00
|
|
|
|
|
|
|
export default WizardFieldValidator.extend({
|
2021-02-05 13:59:30 +01:00
|
|
|
similarTopics: [],
|
|
|
|
|
2021-02-01 14:58:37 +01:00
|
|
|
validate() {
|
2021-02-05 13:59:30 +01:00
|
|
|
},
|
|
|
|
@observes("field.value")
|
|
|
|
customValidate(){
|
|
|
|
const lastKeyUp = new Date();
|
|
|
|
this._lastKeyUp = lastKeyUp;
|
|
|
|
|
|
|
|
// One second from now, check to see if the last key was hit when
|
|
|
|
// we recorded it. If it was, the user paused typing.
|
|
|
|
cancel(this._lastKeyTimeout);
|
|
|
|
this._lastKeyTimeout = later(() => {
|
|
|
|
if (lastKeyUp !== this._lastKeyUp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateSimilarTopics();
|
|
|
|
}, 1000);
|
2021-02-01 14:58:37 +01:00
|
|
|
},
|
|
|
|
|
2021-02-05 13:59:30 +01:00
|
|
|
updateSimilarTopics(){
|
|
|
|
this.backendValidate({
|
|
|
|
title: this.get("field.value"),
|
|
|
|
categories: this.get('validation.categories'),
|
|
|
|
date_after: this.get('validation.date_after')
|
|
|
|
}).then((result) => {
|
|
|
|
const similarTopics = A(deepMerge(result['topics'], result['similar_topics']));
|
|
|
|
similarTopics.forEach(function(topic, index) {
|
|
|
|
similarTopics[index] = EmberObject.create(topic);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.set('similarTopics', similarTopics);
|
|
|
|
});
|
|
|
|
},
|
2021-02-01 14:58:37 +01:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2021-02-05 13:59:30 +01:00
|
|
|
},
|
|
|
|
actions: {
|
2021-02-11 17:53:13 +01:00
|
|
|
closeMessage(){
|
|
|
|
this.set('showMessage', false);
|
|
|
|
}
|
2021-02-01 14:58:37 +01:00
|
|
|
}
|
|
|
|
});
|