AddPerson search: fix aborting query when the query is altered by user

This commit is contained in:
Julien Fastré 2022-04-06 21:47:01 +02:00
parent 645549ae34
commit 095afb90c7
2 changed files with 20 additions and 9 deletions

View File

@ -45,7 +45,7 @@ const searchPersons = ({ query, options }, signal) => {
const searchEntities = ({ query, options }, signal) => {
let queryStr = parametersToString({ query, options });
let url = `/api/1.0/search.json?${queryStr}`;
return fetch(url)
return fetch(url, { signal })
.then(response => {
if (response.ok) { return response.json(); }
throw Error('Error with request resource response');

View File

@ -199,15 +199,26 @@ export default {
return;
}
if (query === this.search.query) {
if (this.currentSearchQueryController !== undefined) {
this.currentSearchQueryController.abort()
if (this.search.currentSearchQueryController !== null) {
this.search.currentSearchQueryController.abort();
}
this.currentSearchQueryController = new AbortController();
searchEntities({ query, options: this.options }, this.currentSearchQueryController)
this.search.currentSearchQueryController = new AbortController();
searchEntities({ query, options: this.options }, this.search.currentSearchQueryController.signal)
.then(suggested => new Promise((resolve, reject) => {
this.loadSuggestions(suggested.results);
resolve();
}));
}))
.catch(error => {
if (error instanceof DOMException) {
if (error.name === 'AbortError') {
console.log('request aborted due to user continue typing');
return;
}
}
throw error;
})
;
}
}.bind(this), query.length > 3 ? 300 : 700);
},