Refactor password callback

This commit is contained in:
Hillel Coren 2020-07-08 09:17:05 +03:00
parent 211a5d8c6a
commit dab169b8ce
6 changed files with 61 additions and 77 deletions

View File

@ -272,7 +272,6 @@ void handleUserAction(
}
final store = StoreProvider.of<AppState>(context);
final state = store.state;
final localization = AppLocalization.of(context);
final user = users.first as UserEntity;
final userIds = users.map((user) => user.id).toList();
@ -287,15 +286,11 @@ void handleUserAction(
snackBarCompleter<Null>(context, localization.restoredUser),
userIds: userIds,
password: password));
if (state.authState.hasRecentlyEnteredPassword) {
dispatch();
} else {
passwordCallback(
context: context,
callback: (password) {
dispatch(password);
});
}
passwordCallback(
context: context,
callback: (password) {
dispatch(password);
});
break;
case EntityAction.archive:
final dispatch = ([String password]) => store.dispatch(ArchiveUserRequest(
@ -303,30 +298,22 @@ void handleUserAction(
snackBarCompleter<Null>(context, localization.archivedUser),
userIds: userIds,
password: password));
if (state.authState.hasRecentlyEnteredPassword) {
dispatch();
} else {
passwordCallback(
context: context,
callback: (password) {
dispatch(password);
});
}
passwordCallback(
context: context,
callback: (password) {
dispatch(password);
});
break;
case EntityAction.delete:
final dispatch = ([String password]) => store.dispatch(DeleteUserRequest(
completer: snackBarCompleter<Null>(context, localization.deletedUser),
userIds: userIds,
password: password));
if (state.authState.hasRecentlyEnteredPassword) {
dispatch();
} else {
passwordCallback(
context: context,
callback: (password) {
dispatch(password);
});
}
passwordCallback(
context: context,
callback: (password) {
dispatch(password);
});
break;
case EntityAction.remove:
final dispatch = ([String password]) => store.dispatch(RemoveUserRequest(
@ -336,15 +323,11 @@ void handleUserAction(
confirmCallback(
context: context,
callback: () {
if (state.authState.hasRecentlyEnteredPassword) {
dispatch();
} else {
passwordCallback(
context: context,
callback: (password) {
dispatch(password);
});
}
passwordCallback(
context: context,
callback: (password) {
dispatch(password);
});
});
break;
case EntityAction.toggleMultiselect:

View File

@ -172,6 +172,7 @@ class _AccountOverview extends StatelessWidget {
message: localization.purgeDataMessage,
callback: () {
passwordCallback(
alwaysRequire: true,
context: context,
callback: (password) {
viewModel.onPurgeData(context, password);
@ -248,6 +249,7 @@ class _AccountOverview extends StatelessWidget {
message: message,
callback: () {
passwordCallback(
alwaysRequire: true,
context: context,
callback: (password) {
viewModel.onCompanyDelete(context, password);

View File

@ -50,26 +50,17 @@ class UserDetailsVM {
completer.future.then((_) {
AppBuilder.of(context).rebuild();
});
if (state.authState.hasRecentlyEnteredPassword) {
store.dispatch(
SaveAuthUserRequest(
completer: completer,
user: state.uiState.settingsUIState.user,
),
);
} else {
passwordCallback(
context: context,
callback: (password) {
store.dispatch(
SaveAuthUserRequest(
completer: completer,
user: state.uiState.settingsUIState.user,
password: password,
),
);
});
}
passwordCallback(
context: context,
callback: (password) {
store.dispatch(
SaveAuthUserRequest(
completer: completer,
user: state.uiState.settingsUIState.user,
password: password,
),
);
});
},
);
}

View File

@ -84,6 +84,7 @@ class _UpdateDialogState extends State<UpdateDialog> {
void updateApp(BuildContext context) async {
final state = StoreProvider.of<AppState>(context).state;
passwordCallback(
alwaysRequire: true,
context: context,
callback: (password) {
setState(() => updateState = UpdateState.loading);

View File

@ -71,16 +71,12 @@ class UserEditVM {
},
onSavePressed: (BuildContext context) {
final Completer<UserEntity> completer = new Completer<UserEntity>();
if (state.authState.hasRecentlyEnteredPassword) {
store.dispatch(SaveUserRequest(completer: completer, user: user));
} else {
passwordCallback(
context: context,
callback: (password) {
store.dispatch(SaveUserRequest(
completer: completer, user: user, password: password));
});
}
passwordCallback(
context: context,
callback: (password) {
store.dispatch(SaveUserRequest(
completer: completer, user: user, password: password));
});
return completer.future.then((savedUser) {
if (isMobile(context)) {
store.dispatch(UpdateCurrentRoute(UserViewScreen.route));

View File

@ -1,5 +1,7 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:invoiceninja_flutter/redux/app/app_state.dart';
import 'package:invoiceninja_flutter/ui/app/dialogs/alert_dialog.dart';
import 'package:invoiceninja_flutter/ui/app/dialogs/error_dialog.dart';
import 'package:invoiceninja_flutter/ui/app/forms/save_cancel_buttons.dart';
@ -62,16 +64,25 @@ void confirmCallback({
);
}
void passwordCallback({BuildContext context, Function(String) callback}) {
showDialog<AlertDialog>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return PasswordConfirmation(
callback: callback,
);
},
);
void passwordCallback({
BuildContext context,
Function(String) callback,
bool alwaysRequire = false,
}) {
final state = StoreProvider.of<AppState>(context).state;
if (state.authState.hasRecentlyEnteredPassword && !alwaysRequire) {
callback(null);
} else {
showDialog<AlertDialog>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return PasswordConfirmation(
callback: callback,
);
},
);
}
}
class PasswordConfirmation extends StatefulWidget {