Skip to content

Commit 7c1f4cf

Browse files
authored
Merge pull request #95 from pvdthings/checkout-too-many-dialogs
replace some dialogs in checkout process with text field errors
2 parents bc00147 + 7bec2c0 commit 7c1f4cf

File tree

3 files changed

+52
-101
lines changed

3 files changed

+52
-101
lines changed

apps/librarian/lib/modules/loans/checkout/stepper/items/connected_thing_search_field.dart

Lines changed: 45 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -26,88 +26,72 @@ class ConnectedThingSearchField extends StatelessWidget {
2626

2727
@override
2828
Widget build(BuildContext context) {
29-
return TextField(
30-
controller: _textController,
31-
onSubmitted: (_) => _submit(),
32-
decoration: InputDecoration(
33-
hintText: 'Enter Item Number',
34-
prefixIcon: const Icon(Icons.numbers),
35-
suffixIcon: IconButton(
36-
tooltip: 'Add Item',
37-
onPressed: () => _submit(),
38-
icon: const Icon(Icons.add_rounded),
39-
),
40-
),
41-
inputFormatters: [
42-
FilteringTextInputFormatter.digitsOnly,
43-
],
44-
);
29+
return ListenableBuilder(
30+
listenable: controller,
31+
builder: (context, child) {
32+
return TextField(
33+
controller: _textController,
34+
onSubmitted: (_) => _submit(),
35+
decoration: InputDecoration(
36+
errorText: controller.errorText,
37+
hintText: 'Enter Item Number',
38+
prefixIcon: const Icon(Icons.numbers),
39+
suffixIcon: IconButton(
40+
tooltip: 'Add Item',
41+
onPressed: () => _submit(),
42+
icon: const Icon(Icons.add_rounded),
43+
),
44+
),
45+
inputFormatters: [
46+
FilteringTextInputFormatter.digitsOnly,
47+
],
48+
);
49+
});
4550
}
4651
}
4752

48-
class ThingSearchController {
53+
class ThingSearchController extends ChangeNotifier {
4954
final BuildContext context;
55+
final List<ItemModel> items;
5056
final InventoryRepository repository;
5157
final void Function(ItemModel) onMatchFound;
5258

5359
bool isLoading = false;
5460

61+
String? errorText;
62+
5563
ThingSearchController({
5664
required this.context,
65+
required this.items,
5766
required this.repository,
5867
required this.onMatchFound,
5968
});
6069

6170
Future<void> search(String value) async {
71+
final itemNumber = int.parse(value);
72+
73+
if (items.any((t) => t.number == itemNumber)) {
74+
errorText = '#$value is already added to this loan.';
75+
notifyListeners();
76+
return;
77+
}
78+
6279
isLoading = true;
63-
final match = await repository.getItem(number: int.parse(value));
80+
final match = await repository.getItem(number: itemNumber);
6481
isLoading = false;
6582

66-
if (match != null) {
67-
if (!match.available) {
68-
_showThingCheckedOutDialog(match);
69-
} else {
70-
onMatchFound(match);
71-
}
72-
} else {
73-
_showUnknownThingDialog(value);
83+
if (match == null) {
84+
errorText = '#$value could not be found.';
85+
notifyListeners();
86+
return;
7487
}
75-
}
7688

77-
void _showThingCheckedOutDialog(ItemModel thing) {
78-
showDialog(
79-
context: context,
80-
builder: (context) {
81-
return AlertDialog(
82-
title: const Text("Item Unavailable"),
83-
content: Text(
84-
"Item #${thing.number} is checked out or not available for lending."),
85-
actions: [
86-
TextButton(
87-
child: const Text("OK"),
88-
onPressed: () => Navigator.pop(context),
89-
)
90-
],
91-
);
92-
},
93-
);
94-
}
89+
if (!match.available) {
90+
errorText = '#$value is unavailable.';
91+
notifyListeners();
92+
return;
93+
}
9594

96-
void _showUnknownThingDialog(String searchValue) {
97-
showDialog(
98-
context: context,
99-
builder: (context) {
100-
return AlertDialog(
101-
title: Text("Item #$searchValue does not exist."),
102-
content: const Text("Try another number."),
103-
actions: [
104-
TextButton(
105-
child: const Text("OK"),
106-
onPressed: () => Navigator.pop(context),
107-
)
108-
],
109-
);
110-
},
111-
);
95+
onMatchFound(match);
11296
}
11397
}

apps/librarian/lib/modules/loans/checkout/stepper/items/existing_item_dialog.dart

Lines changed: 0 additions & 23 deletions
This file was deleted.

apps/librarian/lib/modules/loans/checkout/stepper/items/items_step.dart

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import 'package:librarian_app/modules/things/providers/things_repository_provide
55
import 'package:librarian_app/widgets/item_card.dart';
66

77
import 'connected_thing_search_field.dart';
8-
import 'existing_item_dialog.dart';
98
import 'eye_protection_dialog.dart';
109
import 'suggested_things_dialog.dart';
1110

@@ -27,33 +26,24 @@ Step buildItemsStep({
2726
ConnectedThingSearchField(
2827
controller: ThingSearchController(
2928
context: context,
30-
onMatchFound: (thing) {
31-
if (items.any((t) => t.id == thing.id)) {
32-
showDialog(
33-
context: context,
34-
builder: (context) {
35-
return ExistingItemDialog(number: thing.number);
36-
},
37-
);
38-
return;
39-
}
40-
41-
onAddItem(thing);
29+
items: items,
30+
onMatchFound: (item) {
31+
onAddItem(item);
4232

43-
if (thing.eyeProtection && !didPromptForEyeProtection) {
33+
if (item.eyeProtection && !didPromptForEyeProtection) {
4434
showDialog(
4535
context: context,
4636
builder: (_) => const EyeProtectionDialog(),
4737
);
4838
onPromptForEyeProtection();
4939
}
5040

51-
if (thing.linkedThingIds.isNotEmpty) {
41+
if (item.linkedThingIds.isNotEmpty) {
5242
showDialog(
5343
context: context,
5444
builder: (_) => SuggestedThingsDialog(
55-
thingName: thing.name,
56-
thingIds: thing.linkedThingIds,
45+
thingName: item.name,
46+
thingIds: item.linkedThingIds,
5747
),
5848
);
5949
}

0 commit comments

Comments
 (0)