63 lines
2.1 KiB
Dart
63 lines
2.1 KiB
Dart
import 'package:redux/redux.dart';
|
|
import 'package:meta/meta.dart';
|
|
import 'package:redux_thunk/redux_thunk.dart';
|
|
import 'package:satu/core/models/auth/auth_response.dart';
|
|
import 'package:satu/core/redux/constants/auth_type_const.dart';
|
|
import 'package:satu/core/redux/state/user_state.dart';
|
|
import 'package:satu/core/services/api_service.dart';
|
|
import 'package:satu/core/services/dialog_service.dart';
|
|
import 'package:satu/core/services/navigator_service.dart';
|
|
import 'package:satu/core/utils/locator.dart';
|
|
import 'package:satu/routes/route_names.dart';
|
|
import '../store.dart';
|
|
|
|
@immutable
|
|
class SetUserStateAction {
|
|
final UserState userState;
|
|
SetUserStateAction(this.userState);
|
|
}
|
|
|
|
final ApiService _api = locator<ApiService>();
|
|
final NavigatorService _navigation = locator<NavigatorService>();
|
|
final DialogService _dialogService = locator<DialogService>();
|
|
|
|
|
|
ThunkAction<AppState> authenticate(String email, String password) {
|
|
return (Store<AppState> store) async {
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: true)));
|
|
try {
|
|
AuthResponse result = await _api.login(email, password);
|
|
if (result.operation) {
|
|
_api.token = result.token;
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: false, auth: result)));
|
|
_navigation.replace(MainViewRoute);
|
|
} else {
|
|
_dialogService.showDialog(title: 'Внимание', buttonTitle: 'Ok', description: result.message);
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
} finally {
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: false)));
|
|
}
|
|
};
|
|
}
|
|
|
|
Future<void> logout(Store<AppState> store) async {
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: true)));
|
|
try {
|
|
AuthResponse result = await _api.logout();
|
|
if (result.operation) {
|
|
_api.token = null;
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: false, auth: AuthResponse())));
|
|
_navigation.replace(LoginViewRoute);
|
|
} else {
|
|
_dialogService.showDialog(title: 'Внимание', buttonTitle: 'Ok', description: result.message);
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
} finally {
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: false)));
|
|
}
|
|
}
|
|
|