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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,7 @@
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.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/alert_dialog.dart';
import 'package:invoiceninja_flutter/ui/app/dialogs/error_dialog.dart'; import 'package:invoiceninja_flutter/ui/app/dialogs/error_dialog.dart';
import 'package:invoiceninja_flutter/ui/app/forms/save_cancel_buttons.dart'; import 'package:invoiceninja_flutter/ui/app/forms/save_cancel_buttons.dart';
@ -62,7 +64,15 @@ void confirmCallback({
); );
} }
void passwordCallback({BuildContext context, Function(String) 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>( showDialog<AlertDialog>(
context: context, context: context,
barrierDismissible: false, barrierDismissible: false,
@ -72,6 +82,7 @@ void passwordCallback({BuildContext context, Function(String) callback}) {
); );
}, },
); );
}
} }
class PasswordConfirmation extends StatefulWidget { class PasswordConfirmation extends StatefulWidget {