From a001634458d63f635ab5d2cc5ca768c9bd68d0f0 Mon Sep 17 00:00:00 2001 From: Dillon Fagan Date: Mon, 18 Nov 2024 10:08:59 -0500 Subject: [PATCH] use text field error text for unavailable and unknown items --- .../items/connected_thing_search_field.dart | 95 +++++++------------ 1 file changed, 34 insertions(+), 61 deletions(-) diff --git a/apps/librarian/lib/modules/loans/checkout/stepper/items/connected_thing_search_field.dart b/apps/librarian/lib/modules/loans/checkout/stepper/items/connected_thing_search_field.dart index d8ce017..7575c14 100644 --- a/apps/librarian/lib/modules/loans/checkout/stepper/items/connected_thing_search_field.dart +++ b/apps/librarian/lib/modules/loans/checkout/stepper/items/connected_thing_search_field.dart @@ -26,32 +26,39 @@ class ConnectedThingSearchField extends StatelessWidget { @override Widget build(BuildContext context) { - return TextField( - controller: _textController, - onSubmitted: (_) => _submit(), - decoration: InputDecoration( - hintText: 'Enter Item Number', - prefixIcon: const Icon(Icons.numbers), - suffixIcon: IconButton( - tooltip: 'Add Item', - onPressed: () => _submit(), - icon: const Icon(Icons.add_rounded), - ), - ), - inputFormatters: [ - FilteringTextInputFormatter.digitsOnly, - ], - ); + return ListenableBuilder( + listenable: controller, + builder: (context, child) { + return TextField( + controller: _textController, + onSubmitted: (_) => _submit(), + decoration: InputDecoration( + errorText: controller.errorText, + hintText: 'Enter Item Number', + prefixIcon: const Icon(Icons.numbers), + suffixIcon: IconButton( + tooltip: 'Add Item', + onPressed: () => _submit(), + icon: const Icon(Icons.add_rounded), + ), + ), + inputFormatters: [ + FilteringTextInputFormatter.digitsOnly, + ], + ); + }); } } -class ThingSearchController { +class ThingSearchController extends ChangeNotifier { final BuildContext context; final InventoryRepository repository; final void Function(ItemModel) onMatchFound; bool isLoading = false; + String? errorText; + ThingSearchController({ required this.context, required this.repository, @@ -63,51 +70,17 @@ class ThingSearchController { final match = await repository.getItem(number: int.parse(value)); isLoading = false; - if (match != null) { - if (!match.available) { - _showThingCheckedOutDialog(match); - } else { - onMatchFound(match); - } - } else { - _showUnknownThingDialog(value); + if (match == null) { + errorText = '#$value could not be found.'; + notifyListeners(); + return; } - } - void _showThingCheckedOutDialog(ItemModel thing) { - showDialog( - context: context, - builder: (context) { - return AlertDialog( - title: const Text("Item Unavailable"), - content: Text( - "Item #${thing.number} is checked out or not available for lending."), - actions: [ - TextButton( - child: const Text("OK"), - onPressed: () => Navigator.pop(context), - ) - ], - ); - }, - ); - } - - void _showUnknownThingDialog(String searchValue) { - showDialog( - context: context, - builder: (context) { - return AlertDialog( - title: Text("Item #$searchValue does not exist."), - content: const Text("Try another number."), - actions: [ - TextButton( - child: const Text("OK"), - onPressed: () => Navigator.pop(context), - ) - ], - ); - }, - ); + if (!match.available) { + errorText = '#$value is unavailable.'; + notifyListeners(); + } else { + onMatchFound(match); + } } }