73 lines
2.5 KiB
Dart
73 lines
2.5 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:aman_kassa_flutter/core/locator.dart';
|
|
import 'package:aman_kassa_flutter/core/models/Message.dart';
|
|
import 'package:aman_kassa_flutter/core/models/authResponse.dart';
|
|
import 'package:aman_kassa_flutter/core/models/Response.dart';
|
|
import 'package:aman_kassa_flutter/core/models/user.dart';
|
|
import 'package:aman_kassa_flutter/core/route_names.dart';
|
|
import 'package:aman_kassa_flutter/core/services/ApiService.dart';
|
|
import 'package:aman_kassa_flutter/core/services/dialog_service.dart';
|
|
import 'package:aman_kassa_flutter/core/services/navigator_service.dart';
|
|
import 'package:aman_kassa_flutter/redux/state/user_state.dart';
|
|
import 'package:redux/redux.dart';
|
|
import 'package:meta/meta.dart';
|
|
import 'package:redux_thunk/redux_thunk.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>();
|
|
|
|
Future<void> checkUserAction(Store<AppState> store) async {
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: true)));
|
|
try {
|
|
Response<Message> session = await _api.isActive('test');
|
|
bool isAuthenticated = "OK" == session.body.message;
|
|
store.dispatch(
|
|
SetUserStateAction(
|
|
UserState(
|
|
isLoading: false,
|
|
isAuthenticated: isAuthenticated,
|
|
),
|
|
),
|
|
);
|
|
|
|
if(!isAuthenticated){
|
|
_navigation.replace(LoginViewRoute);
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: false)));
|
|
}
|
|
}
|
|
|
|
|
|
ThunkAction<AppState> authenticate(String email, String password) {
|
|
return (Store<AppState> store) async {
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: true)));
|
|
try {
|
|
AuthBody result = await _api.authenticate(email, password);
|
|
store.dispatch(SetUserStateAction(UserState(
|
|
isLoading: false,
|
|
loginFormMessage: LoginFormMessage(email: result.email?.join(","), password: result.password?.join(","), message: result.message),
|
|
user: result.user
|
|
)));
|
|
if(result.user == null && result.message!=null){
|
|
_dialogService.showDialog(title: 'Warning', buttonTitle: 'Ok', description: result.message);
|
|
}
|
|
if(result.user!=null) {
|
|
_navigation.replace(HomeViewRoute);
|
|
}
|
|
} catch(e) {
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: false)));
|
|
}
|
|
};
|
|
}
|