mirror of
				https://github.com/inventree/inventree-app.git
				synced 2025-11-04 15:25:48 +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:
		@@ -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;
 | 
			
		||||
 
 | 
			
		||||
@@ -105,16 +105,17 @@ 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(() {
 | 
			
		||||
      nSearchResults = 0;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    if (term.isEmpty) {
 | 
			
		||||
    setState(() {
 | 
			
		||||
      // Do not search on an empty string
 | 
			
		||||
      nPartResults = 0;
 | 
			
		||||
@@ -127,56 +128,55 @@ class _SearchDisplayState extends RefreshableState<SearchWidget> {
 | 
			
		||||
      nSearchResults = 0;
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    if (term.isEmpty) {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Search parts
 | 
			
		||||
    InvenTreePart().count(
 | 
			
		||||
      searchQuery: term
 | 
			
		||||
    ).then((int n) {
 | 
			
		||||
    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) {
 | 
			
		||||
    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) {
 | 
			
		||||
    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) {
 | 
			
		||||
    InvenTreeStockLocation().count(searchQuery: term).then((int n) {
 | 
			
		||||
      if (term == searchController.text) {
 | 
			
		||||
        setState(() {
 | 
			
		||||
          nLocationResults = n;
 | 
			
		||||
 | 
			
		||||
          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) {
 | 
			
		||||
      if (term == searchController.text) {
 | 
			
		||||
        setState(() {
 | 
			
		||||
          nPurchaseOrderResults = n;
 | 
			
		||||
          nSearchResults++;
 | 
			
		||||
        });
 | 
			
		||||
      }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user