import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:invoiceninja_flutter/redux/auth/auth_actions.dart'; import 'package:redux/redux.dart'; import 'package:invoiceninja_flutter/redux/app/app_actions.dart'; import 'package:invoiceninja_flutter/utils/platforms.dart'; import 'package:invoiceninja_flutter/data/models/models.dart'; import 'package:invoiceninja_flutter/redux/ui/ui_actions.dart'; import 'package:invoiceninja_flutter/ui/token/token_screen.dart'; import 'package:invoiceninja_flutter/ui/token/edit/token_edit_vm.dart'; import 'package:invoiceninja_flutter/ui/token/view/token_view_vm.dart'; import 'package:invoiceninja_flutter/redux/token/token_actions.dart'; import 'package:invoiceninja_flutter/redux/app/app_state.dart'; import 'package:invoiceninja_flutter/data/repositories/token_repository.dart'; import 'package:invoiceninja_flutter/main_app.dart'; List> createStoreTokensMiddleware([ TokenRepository repository = const TokenRepository(), ]) { final viewTokenList = _viewTokenList(); final viewToken = _viewToken(); final editToken = _editToken(); final loadTokens = _loadTokens(repository); final loadToken = _loadToken(repository); final saveToken = _saveToken(repository); final archiveToken = _archiveToken(repository); final deleteToken = _deleteToken(repository); final restoreToken = _restoreToken(repository); return [ TypedMiddleware(viewTokenList), TypedMiddleware(viewToken), TypedMiddleware(editToken), TypedMiddleware(loadTokens), TypedMiddleware(loadToken), TypedMiddleware(saveToken), TypedMiddleware(archiveToken), TypedMiddleware(deleteToken), TypedMiddleware(restoreToken), ]; } Middleware _editToken() { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as EditToken; next(action); store.dispatch(UpdateCurrentRoute(TokenEditScreen.route)); if (store.state.prefState.isMobile) { navigatorKey.currentState.pushNamed(TokenEditScreen.route); } }; } Middleware _viewToken() { return (Store store, dynamic dynamicAction, NextDispatcher next) async { final action = dynamicAction as ViewToken; next(action); store.dispatch(UpdateCurrentRoute(TokenViewScreen.route)); if (store.state.prefState.isMobile) { navigatorKey.currentState.pushNamed(TokenViewScreen.route); } }; } Middleware _viewTokenList() { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as ViewTokenList; next(action); if (store.state.isStale) { store.dispatch(RefreshData()); } store.dispatch(UpdateCurrentRoute(TokenScreen.route)); if (store.state.prefState.isMobile) { navigatorKey.currentState.pushNamedAndRemoveUntil( TokenScreen.route, (Route route) => false); } }; } Middleware _archiveToken(TokenRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as ArchiveTokensRequest; final prevTokens = action.tokenIds.map((id) => store.state.tokenState.map[id]).toList(); repository .bulkAction( store.state.credentials, action.tokenIds, EntityAction.archive) .then((List tokens) { store.dispatch(ArchiveTokensSuccess(tokens)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); store.dispatch(ArchiveTokensFailure(prevTokens)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; } Middleware _deleteToken(TokenRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as DeleteTokensRequest; final prevTokens = action.tokenIds.map((id) => store.state.tokenState.map[id]).toList(); repository .bulkAction( store.state.credentials, action.tokenIds, EntityAction.delete) .then((List tokens) { store.dispatch(DeleteTokensSuccess(tokens)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); store.dispatch(DeleteTokensFailure(prevTokens)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; } Middleware _restoreToken(TokenRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as RestoreTokensRequest; final prevTokens = action.tokenIds.map((id) => store.state.tokenState.map[id]).toList(); repository .bulkAction( store.state.credentials, action.tokenIds, EntityAction.restore) .then((List tokens) { store.dispatch(RestoreTokensSuccess(tokens)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); store.dispatch(RestoreTokensFailure(prevTokens)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; } Middleware _saveToken(TokenRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as SaveTokenRequest; repository .saveData(store.state.credentials, action.token, action.password, action.idToken) .then((TokenEntity token) { if (action.token.isNew) { store.dispatch(AddTokenSuccess(token)); } else { store.dispatch(SaveTokenSuccess(token)); } action.completer.complete(token); }).catchError((Object error) { print(error); store.dispatch(SaveTokenFailure(error)); if ('$error'.contains('412')) { store.dispatch(UserUnverifiedPassword()); } action.completer.completeError(error); }); next(action); }; } Middleware _loadToken(TokenRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as LoadToken; final AppState state = store.state; store.dispatch(LoadTokenRequest()); repository.loadItem(state.credentials, action.tokenId).then((token) { store.dispatch(LoadTokenSuccess(token)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); store.dispatch(LoadTokenFailure(error)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; } Middleware _loadTokens(TokenRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as LoadTokens; final AppState state = store.state; store.dispatch(LoadTokensRequest()); repository.loadList(state.credentials).then((data) { store.dispatch(LoadTokensSuccess(data)); if (action.completer != null) { action.completer.complete(null); } /* if (state.productState.isStale) { store.dispatch(LoadProducts()); } */ }).catchError((Object error) { print(error); store.dispatch(LoadTokensFailure(error)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; }