2019-09-07 21:54:44 +02:00
|
|
|
import { ViewController } from '../controller';
|
|
|
|
|
|
|
|
export class SearchController extends ViewController {
|
|
|
|
constructor(state) {
|
|
|
|
// Super passes state, view name, and default state to ViewController,
|
|
|
|
// which stores state in this.appState and the view controller's state to this.state
|
|
|
|
super(state, 'search', {
|
2019-09-10 01:09:25 +02:00
|
|
|
lastSearch: undefined,
|
2019-09-07 21:54:44 +02:00
|
|
|
done: false,
|
2019-09-11 01:16:01 +02:00
|
|
|
results: {
|
|
|
|
humans: [],
|
|
|
|
series: [],
|
|
|
|
works: [],
|
|
|
|
},
|
2019-09-12 01:34:11 +02:00
|
|
|
openModal: null,
|
2019-09-07 21:54:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// If using controller methods in an input's onchange or onclick instance,
|
|
|
|
// either bind the class's 'this' instance to the method first...
|
|
|
|
// or use `onclick=${() => controller.submit()}` to maintain the 'this' of the class instead.
|
|
|
|
}
|
|
|
|
|
2019-09-11 01:16:01 +02:00
|
|
|
get doneSearching() {
|
|
|
|
return this.state.done;
|
|
|
|
}
|
|
|
|
|
2019-09-07 21:54:44 +02:00
|
|
|
get results() {
|
2019-09-11 01:16:01 +02:00
|
|
|
return this.state.results;
|
2019-09-07 21:54:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get hasQuery() {
|
|
|
|
return this.appState.query.hasOwnProperty('for') && this.appState.query.for.trim() !== '';
|
|
|
|
}
|
|
|
|
|
2019-09-12 01:34:11 +02:00
|
|
|
get openModal() {
|
|
|
|
return this.state.openModal;
|
|
|
|
}
|
|
|
|
|
|
|
|
set openModal(modalId) {
|
|
|
|
this.state.openModal = modalId;
|
|
|
|
}
|
|
|
|
|
2019-09-10 01:09:25 +02:00
|
|
|
search() {
|
|
|
|
if (this.hasQuery) {
|
|
|
|
this.state.done = false;
|
|
|
|
this.state.lastSearch = this.appState.query.for;
|
2019-09-07 21:54:44 +02:00
|
|
|
|
2019-09-10 01:09:25 +02:00
|
|
|
const searchTerm = this.appState.query.for.trim();
|
2019-09-07 21:54:44 +02:00
|
|
|
|
2019-09-10 01:09:25 +02:00
|
|
|
return fetch(`/api/search?for=${searchTerm}&lang=${this.appState.language}`)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(responseJSON => {
|
|
|
|
this.state.results = responseJSON;
|
2019-09-07 21:54:44 +02:00
|
|
|
this.state.done = true;
|
2019-09-10 01:09:25 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
2019-09-07 21:54:44 +02:00
|
|
|
}
|
|
|
|
}
|