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()) {
 | 
					    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;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -105,78 +105,78 @@ 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(() {
 | 
					    setState(() {
 | 
				
			||||||
 | 
					      // Do not search on an empty string
 | 
				
			||||||
 | 
					      nPartResults = 0;
 | 
				
			||||||
 | 
					      nCategoryResults = 0;
 | 
				
			||||||
 | 
					      nStockResults = 0;
 | 
				
			||||||
 | 
					      nLocationResults = 0;
 | 
				
			||||||
 | 
					      nSupplierResults = 0;
 | 
				
			||||||
 | 
					      nPurchaseOrderResults = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      nSearchResults = 0;
 | 
					      nSearchResults = 0;
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (term.isEmpty) {
 | 
					    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;
 | 
					      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) {
 | 
				
			||||||
      setState(() {
 | 
					      if (term == searchController.text) {
 | 
				
			||||||
        nPurchaseOrderResults = n;
 | 
					        setState(() {
 | 
				
			||||||
        nSearchResults++;
 | 
					          nPurchaseOrderResults = n;
 | 
				
			||||||
      });
 | 
					          nSearchResults++;
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user