152 lines
5.1 KiB
Dart
152 lines
5.1 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/auth_response.dart';
|
|
import 'package:aman_kassa_flutter/core/models/response.dart';
|
|
import 'package:aman_kassa_flutter/core/models/smena.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/constants/auth_type_const.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 {
|
|
String token = store.state.userState.user?.token;
|
|
bool isAuthenticated = false;
|
|
if(token!=null) {
|
|
Response<Message> session = await _api.isActive(token);
|
|
isAuthenticated = "OK" == session.body.message;
|
|
} else {
|
|
await Future.delayed(Duration(milliseconds: 100));
|
|
}
|
|
store.dispatch(
|
|
SetUserStateAction(
|
|
UserState(
|
|
isLoading: false,
|
|
isAuthenticated: isAuthenticated,
|
|
),
|
|
),
|
|
);
|
|
|
|
if(!isAuthenticated){
|
|
_navigation.replace(LoginViewRoute);
|
|
} else {
|
|
_navigation.replace(HomeViewRoute);
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
print(error);
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: false)));
|
|
}
|
|
}
|
|
|
|
|
|
Future<void> logoutAction(Store<AppState> store) async {
|
|
try {
|
|
store.dispatch(
|
|
SetUserStateAction(
|
|
UserState(
|
|
isLoading: false,
|
|
isAuthenticated: false,
|
|
user: User(),
|
|
),
|
|
),
|
|
);
|
|
_navigation.replace(LoginViewRoute);
|
|
} catch (error) {
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: false)));
|
|
}
|
|
}
|
|
|
|
ThunkAction<AppState> authenticateToken(String token) {
|
|
return (Store<AppState> store) async {
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: true)));
|
|
try {
|
|
AuthBody result = await _api.authenticate_token(token, statusCheck: false);
|
|
store.dispatch(SetUserStateAction(UserState(
|
|
isLoading: false,
|
|
loginFormMessage: LoginFormMessage(email: result.email?.join(","), password: result.password?.join(","), message: result.message),
|
|
user: result.user,
|
|
authenticateType: AuthenticateTypeQr,
|
|
isAuthenticated: result.user != null,
|
|
)));
|
|
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) {
|
|
print(e);
|
|
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, statusCheck: false);
|
|
store.dispatch(SetUserStateAction(UserState(
|
|
isLoading: false,
|
|
loginFormMessage: LoginFormMessage(email: result.email?.join(","), password: result.password?.join(","), message: result.message),
|
|
user: result.user,
|
|
login: email,
|
|
password: password,
|
|
authenticateType: AuthenticateTypeLogin,
|
|
isAuthenticated: result.user != null,
|
|
)));
|
|
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) {
|
|
print(e);
|
|
store.dispatch(SetUserStateAction(UserState(isLoading: false)));
|
|
}
|
|
};
|
|
}
|
|
|
|
Future<void> checkSmena(Store<AppState> store) async {
|
|
String token = store.state.userState.user.token;
|
|
Response<Smena> result = await _api.smena(token);
|
|
store.dispatch(SetUserStateAction(UserState(smena: result.body)));
|
|
}
|
|
|
|
Future<void> closeSmena(Store<AppState> store) async {
|
|
String token = store.state.userState.user.token;
|
|
Response<Smena> result = await _api.closeSmena(token);
|
|
store.dispatch(SetUserStateAction(UserState(smena: result.body)));
|
|
}
|
|
|
|
Future<void> openSmena(Store<AppState> store) async {
|
|
String token = store.state.userState.user.token;
|
|
Response<Smena> result = await _api.openSmena(token);
|
|
store.dispatch(SetUserStateAction(UserState(smena: result.body)));
|
|
if(result.operation){
|
|
store.dispatch(checkSmena);
|
|
}
|
|
}
|