From 2f8d02b0bef79ca12734316bdebc2fb1833bc6ee Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 2 Oct 2021 22:31:28 +1000 Subject: [PATCH] API_Form: Allow customizable icon - Also fixes for API search --- lib/api_form.dart | 50 ++++++++++++++++++++++--------- lib/inventree/model.dart | 16 +++++++--- lib/inventree/purchase_order.dart | 2 ++ lib/widget/search.dart | 5 ++-- lib/widget/stock_detail.dart | 8 +---- 5 files changed, 53 insertions(+), 28 deletions(-) diff --git a/lib/api_form.dart b/lib/api_form.dart index c099300a..50e44d54 100644 --- a/lib/api_form.dart +++ b/lib/api_form.dart @@ -559,7 +559,16 @@ Map extractFields(APIResponse response) { * @param method is the HTTP method to use to send the form data to the server (e.g. POST / PATCH) */ -Future launchApiForm(BuildContext context, String title, String url, Map fields, {String fileField = "", Map modelData = const {}, String method = "PATCH", Function(Map)? onSuccess, Function? onCancel}) async { +Future launchApiForm( + BuildContext context, String title, String url, Map fields, + { + String fileField = "", + Map modelData = const {}, + String method = "PATCH", + Function(Map)? onSuccess, + Function? onCancel, + IconData icon = FontAwesomeIcons.save, + }) async { var options = await InvenTreeAPI().options(url); @@ -641,12 +650,13 @@ Future launchApiForm(BuildContext context, String title, String url, Map APIFormWidget( - title, - url, - formFields, - method, - onSuccess: onSuccess, - fileField: fileField, + title, + url, + formFields, + method, + onSuccess: onSuccess, + fileField: fileField, + icon: icon, )) ); } @@ -663,6 +673,7 @@ class APIFormWidget extends StatefulWidget { Key? key, this.onSuccess, this.fileField = "", + this.icon = FontAwesomeIcons.save, } ) : super(key: key); @@ -677,29 +688,34 @@ class APIFormWidget extends StatefulWidget { final String fileField; + // Icon + final IconData icon; + final List fields; final Function(Map)? onSuccess; @override - _APIFormWidgetState createState() => _APIFormWidgetState(title, url, fields, method, onSuccess, fileField); + _APIFormWidgetState createState() => _APIFormWidgetState(title, url, fields, method, onSuccess, fileField, icon); } class _APIFormWidgetState extends State { - _APIFormWidgetState(this.title, this.url, this.fields, this.method, this.onSuccess, this.fileField) : super(); + _APIFormWidgetState(this.title, this.url, this.fields, this.method, this.onSuccess, this.fileField, this.icon) : super(); final _formKey = GlobalKey(); - String title; + final String title; - String url; + final String url; - String method; + final String method; - String fileField; + final String fileField; + + final IconData icon; List fields; @@ -878,6 +894,12 @@ class _APIFormWidgetState extends State { field.data["errors"] = response.data[field.name]; } break; + case 405: + showSnackIcon( + L10().response405, + success: false, + ); + break; // TODO: Other status codes? } @@ -895,7 +917,7 @@ class _APIFormWidgetState extends State { title: Text(title), actions: [ IconButton( - icon: FaIcon(FontAwesomeIcons.save), + icon: FaIcon(icon), onPressed: () { if (_formKey.currentState!.validate()) { diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index 4eb234ec..03e7fc13 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -118,7 +118,7 @@ class InvenTreeModel { Map jsondata = {}; // Accessor for the API - InvenTreeAPI api = InvenTreeAPI(); + InvenTreeAPI get api => InvenTreeAPI(); int get pk => (jsondata["pk"] ?? -1) as int; @@ -167,11 +167,19 @@ class InvenTreeModel { String get url => "${URL}/${pk}/".replaceAll("//", "/"); // Search this Model type in the database - Future> search(BuildContext context, String searchTerm, {Map filters = const {}}) async { + Future> search(String searchTerm, {Map filters = const {}, int offset = 0, int limit = 25}) async { - filters["search"] = searchTerm; + Map searchFilters = {}; - final results = list(filters: filters); + for (String key in filters.keys) { + searchFilters[key] = filters[key] ?? ""; + } + + searchFilters["search"] = searchTerm; + searchFilters["offset"] = "${offset}"; + searchFilters["limit"] = "${limit}"; + + final results = list(filters: searchFilters); return results; diff --git a/lib/inventree/purchase_order.dart b/lib/inventree/purchase_order.dart index 349066e3..ba979a28 100644 --- a/lib/inventree/purchase_order.dart +++ b/lib/inventree/purchase_order.dart @@ -20,6 +20,8 @@ class InvenTreePurchaseOrder extends InvenTreeModel { @override String get URL => "order/po/"; + String get receive_url => "${url}receive/"; + @override Map formFields() { return { diff --git a/lib/widget/search.dart b/lib/widget/search.dart index 4f2cbd23..c209310c 100644 --- a/lib/widget/search.dart +++ b/lib/widget/search.dart @@ -71,7 +71,7 @@ class PartSearchDelegate extends SearchDelegate { _filters["cascade"] = "true"; - final results = await InvenTreePart().search(context, query, filters: _filters); + final results = await InvenTreePart().search(query, filters: _filters); partResults.clear(); @@ -260,8 +260,7 @@ class StockSearchDelegate extends SearchDelegate { // Enable cascading part search by default _filters["cascade"] = "true"; - final results = await InvenTreeStockItem().search( - context, query, filters: _filters); + final results = await InvenTreeStockItem().search(query, filters: _filters); itemResults.clear(); diff --git a/lib/widget/stock_detail.dart b/lib/widget/stock_detail.dart index 7ed69124..c10ac170 100644 --- a/lib/widget/stock_detail.dart +++ b/lib/widget/stock_detail.dart @@ -327,13 +327,7 @@ class _StockItemDisplayState extends RefreshableState { }, onFind: (String filter) async { - Map _filters = { - "search": filter, - "offset": "0", - "limit": "25" - }; - - final List results = await InvenTreeStockLocation().list(filters: _filters); + final results = await InvenTreeStockLocation().search(filter); List items = [];