2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 05:26:47 +00:00

Prevent "old" search results from mucking up the displayed data

- Only accept results if the search term has not changed
This commit is contained in:
Oliver Walters 2022-05-19 21:06:20 +10:00
parent acf89426ce
commit 941ee5e172
2 changed files with 53 additions and 52 deletions

View File

@ -216,8 +216,6 @@ class InvenTreeModel {
if (response.isValid()) {
int n = int.tryParse(response.data["count"].toString()) ?? 0;
print("${URL} -> ${n} results");
return n;
} else {
return 0;

View File

@ -105,78 +105,78 @@ class _SearchDisplayState extends RefreshableState<SearchWidget> {
search(text);
});
}
}
/*
* Initiate multiple search requests to the server.
* Each request returns at *some point* in the future,
* by which time the search input may have changed, giving unexpected results.
*
* So, each request only causes an update *if* the search term is still the same when it completes
*/
Future<void> search(String term) async {
setState(() {
// Do not search on an empty string
nPartResults = 0;
nCategoryResults = 0;
nStockResults = 0;
nLocationResults = 0;
nSupplierResults = 0;
nPurchaseOrderResults = 0;
nSearchResults = 0;
});
if (term.isEmpty) {
setState(() {
// Do not search on an empty string
nPartResults = 0;
nCategoryResults = 0;
nStockResults = 0;
nLocationResults = 0;
nSupplierResults = 0;
nPurchaseOrderResults = 0;
nSearchResults = 0;
});
return;
}
// Search parts
InvenTreePart().count(
searchQuery: term
).then((int n) {
setState(() {
nPartResults = n;
nSearchResults++;
});
InvenTreePart().count(searchQuery: term).then((int n) {
if (term == searchController.text) {
setState(() {
nPartResults = n;
nSearchResults++;
});
}
});
// Search part categories
InvenTreePartCategory().count(
searchQuery: term,
).then((int n) {
setState(() {
nCategoryResults = n;
nSearchResults++;
});
InvenTreePartCategory().count(searchQuery: term,).then((int n) {
if (term == searchController.text) {
setState(() {
nCategoryResults = n;
nSearchResults++;
});
}
});
// Search stock items
InvenTreeStockItem().count(
searchQuery: term
).then((int n) {
setState(() {
nStockResults = n;
nSearchResults++;
});
InvenTreeStockItem().count(searchQuery: term).then((int n) {
if (term == searchController.text) {
setState(() {
nStockResults = n;
nSearchResults++;
});
}
});
// Search stock locations
InvenTreeStockLocation().count(
searchQuery: term
).then((int n) {
setState(() {
nLocationResults = n;
InvenTreeStockLocation().count(searchQuery: term).then((int n) {
if (term == searchController.text) {
setState(() {
nLocationResults = n;
nSearchResults++;
});
nSearchResults++;
});
}
});
// TDOO: Re-implement this once display for companies has been fixed
/*
// Search suppliers
InvenTreeCompany().count(
searchQuery: term,
InvenTreeCompany().count(searchQuery: term,
filters: {
"is_supplier": "true",
},
@ -186,6 +186,7 @@ class _SearchDisplayState extends RefreshableState<SearchWidget> {
nSearchResults++;
});
});
*/
// Search purchase orders
InvenTreePurchaseOrder().count(
@ -194,10 +195,12 @@ class _SearchDisplayState extends RefreshableState<SearchWidget> {
"outstanding": "true"
}
).then((int n) {
setState(() {
nPurchaseOrderResults = n;
nSearchResults++;
});
if (term == searchController.text) {
setState(() {
nPurchaseOrderResults = n;
nSearchResults++;
});
}
});
}