2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 13:36:50 +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()) { if (response.isValid()) {
int n = int.tryParse(response.data["count"].toString()) ?? 0; int n = int.tryParse(response.data["count"].toString()) ?? 0;
print("${URL} -> ${n} results");
return n; return n;
} else { } else {
return 0; return 0;

View File

@ -105,16 +105,17 @@ class _SearchDisplayState extends RefreshableState<SearchWidget> {
search(text); 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 { Future<void> search(String term) async {
setState(() {
nSearchResults = 0;
});
if (term.isEmpty) {
setState(() { setState(() {
// Do not search on an empty string // Do not search on an empty string
nPartResults = 0; nPartResults = 0;
@ -127,56 +128,55 @@ class _SearchDisplayState extends RefreshableState<SearchWidget> {
nSearchResults = 0; nSearchResults = 0;
}); });
if (term.isEmpty) {
return; return;
} }
// Search parts // Search parts
InvenTreePart().count( InvenTreePart().count(searchQuery: term).then((int n) {
searchQuery: term if (term == searchController.text) {
).then((int n) {
setState(() { setState(() {
nPartResults = n; nPartResults = n;
nSearchResults++; nSearchResults++;
}); });
}
}); });
// Search part categories // Search part categories
InvenTreePartCategory().count( InvenTreePartCategory().count(searchQuery: term,).then((int n) {
searchQuery: term, if (term == searchController.text) {
).then((int n) {
setState(() { setState(() {
nCategoryResults = n; nCategoryResults = n;
nSearchResults++; nSearchResults++;
}); });
}
}); });
// Search stock items // Search stock items
InvenTreeStockItem().count( InvenTreeStockItem().count(searchQuery: term).then((int n) {
searchQuery: term if (term == searchController.text) {
).then((int n) {
setState(() { setState(() {
nStockResults = n; nStockResults = n;
nSearchResults++; nSearchResults++;
}); });
}
}); });
// Search stock locations // Search stock locations
InvenTreeStockLocation().count( InvenTreeStockLocation().count(searchQuery: term).then((int n) {
searchQuery: term if (term == searchController.text) {
).then((int n) {
setState(() { setState(() {
nLocationResults = n; nLocationResults = n;
nSearchResults++; nSearchResults++;
}); });
}
}); });
// TDOO: Re-implement this once display for companies has been fixed
/*
// Search suppliers // Search suppliers
InvenTreeCompany().count( InvenTreeCompany().count(searchQuery: term,
searchQuery: term,
filters: { filters: {
"is_supplier": "true", "is_supplier": "true",
}, },
@ -186,6 +186,7 @@ class _SearchDisplayState extends RefreshableState<SearchWidget> {
nSearchResults++; nSearchResults++;
}); });
}); });
*/
// Search purchase orders // Search purchase orders
InvenTreePurchaseOrder().count( InvenTreePurchaseOrder().count(
@ -194,10 +195,12 @@ class _SearchDisplayState extends RefreshableState<SearchWidget> {
"outstanding": "true" "outstanding": "true"
} }
).then((int n) { ).then((int n) {
if (term == searchController.text) {
setState(() { setState(() {
nPurchaseOrderResults = n; nPurchaseOrderResults = n;
nSearchResults++; nSearchResults++;
}); });
}
}); });
} }