@@ -4,8 +4,10 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';
44import 'package:time_keeper/generated/api/location.pbgrpc.dart' ;
55import 'package:time_keeper/helpers/grpc_call_wrapper.dart' ;
66import 'package:time_keeper/providers/location_provider.dart' ;
7+ import 'package:time_keeper/utils/grpc_result.dart' ;
78import 'package:time_keeper/widgets/dialogs/confirm_dialog.dart' ;
89import 'package:time_keeper/widgets/dialogs/popup_dialog.dart' ;
10+ import 'package:time_keeper/widgets/dialogs/snackbar_dialog.dart' ;
911
1012void showLocationDialog (
1113 BuildContext context,
@@ -18,7 +20,6 @@ void showLocationDialog(
1820 PopupDialog .info (
1921 title: isEdit ? 'Edit Location' : 'Add Location' ,
2022 message: _LocationForm (
21- ref: ref,
2223 isEdit: isEdit,
2324 locationId: id,
2425 initialName: existingName,
@@ -48,22 +49,21 @@ void showDeleteLocationDialog(
4849 ).show (context);
4950}
5051
51- class _LocationForm extends HookWidget {
52- final WidgetRef ref;
52+ class _LocationForm extends HookConsumerWidget {
5353 final bool isEdit;
5454 final String ? locationId;
5555 final String ? initialName;
5656
5757 const _LocationForm ({
58- required this .ref,
5958 required this .isEdit,
6059 this .locationId,
6160 this .initialName,
6261 });
6362
6463 @override
65- Widget build (BuildContext context) {
64+ Widget build (BuildContext context, WidgetRef ref ) {
6665 final nameController = useTextEditingController (text: initialName ?? '' );
66+ final isLoading = useState (false );
6767
6868 return SizedBox (
6969 width: 400 ,
@@ -83,51 +83,66 @@ class _LocationForm extends HookWidget {
8383 mainAxisAlignment: MainAxisAlignment .end,
8484 children: [
8585 TextButton (
86- onPressed: () => Navigator .of (context).pop (),
86+ onPressed: isLoading.value
87+ ? null
88+ : () => Navigator .of (context).pop (),
8789 child: const Text ('Cancel' ),
8890 ),
8991 const SizedBox (width: 8 ),
9092 FilledButton (
91- onPressed: () {
92- final name = nameController.text.trim ();
93- if (name.isEmpty) return ;
93+ onPressed: isLoading.value
94+ ? null
95+ : () async {
96+ final name = nameController.text.trim ();
97+ if (name.isEmpty) return ;
9498
95- Navigator .of (context).pop ();
99+ isLoading.value = true ;
100+ try {
101+ final client = ref.read (locationServiceProvider);
102+ final GrpcResult <dynamic > result;
103+ if (isEdit) {
104+ result = await callGrpcEndpoint (
105+ () => client.updateLocation (
106+ UpdateLocationRequest (
107+ id: locationId,
108+ location: name,
109+ ),
110+ ),
111+ );
112+ } else {
113+ result = await callGrpcEndpoint (
114+ () => client.createLocation (
115+ CreateLocationRequest (location: name),
116+ ),
117+ );
118+ }
96119
97- ConfirmDialog .info (
98- title: isEdit ? 'Update Location' : 'Create Location' ,
99- message: isEdit
100- ? Text ('Save changes to "$name "?' )
101- : Text ('Create location "$name "?' ),
102- confirmText: isEdit ? 'Save' : 'Create' ,
103- onConfirmAsyncGrpc: () async {
104- final client = ref.read (locationServiceProvider);
105- if (isEdit) {
106- return await callGrpcEndpoint (
107- () => client.updateLocation (
108- UpdateLocationRequest (
109- id: locationId,
110- location: name,
111- ),
112- ),
113- );
114- } else {
115- return await callGrpcEndpoint (
116- () => client.createLocation (
117- CreateLocationRequest (location: name),
118- ),
119- );
120- }
121- },
122- showResultDialog: true ,
123- successMessage: Text (
124- isEdit
125- ? '"$name " updated successfully'
126- : '"$name " created successfully' ,
127- ),
128- ).show (context);
129- },
130- child: Text (isEdit ? 'Save' : 'Create' ),
120+ if (context.mounted) {
121+ Navigator .of (context).pop ();
122+ switch (result) {
123+ case GrpcSuccess ():
124+ SnackBarDialog .success (
125+ message: isEdit
126+ ? '"$name " updated successfully'
127+ : '"$name " created successfully' ,
128+ ).show (context);
129+ case GrpcFailure ():
130+ SnackBarDialog .fromGrpcStatus (
131+ result: result,
132+ ).show (context);
133+ }
134+ }
135+ } finally {
136+ isLoading.value = false ;
137+ }
138+ },
139+ child: isLoading.value
140+ ? const SizedBox (
141+ width: 16 ,
142+ height: 16 ,
143+ child: CircularProgressIndicator (strokeWidth: 2 ),
144+ )
145+ : Text (isEdit ? 'Save' : 'Create' ),
131146 ),
132147 ],
133148 ),
0 commit comments