import 'package:invoiceninja/data/models/models.dart'; import 'package:invoiceninja/redux/product/product_actions.dart'; import 'package:redux/redux.dart'; import 'package:invoiceninja/redux/client/client_actions.dart'; import 'package:invoiceninja/redux/app/app_state.dart'; import 'package:invoiceninja/data/repositories/client_repository.dart'; List> createStoreClientsMiddleware([ ClientRepository repository = const ClientRepository(), ]) { final loadClients = _loadClients(repository); final saveClient = _saveClient(repository); final archiveClient = _archiveClient(repository); final deleteClient = _deleteClient(repository); final restoreClient = _restoreClient(repository); return [ TypedMiddleware(loadClients), TypedMiddleware(saveClient), TypedMiddleware(archiveClient), TypedMiddleware(deleteClient), TypedMiddleware(restoreClient), ]; } Middleware _archiveClient(ClientRepository repository) { return (Store store, action, NextDispatcher next) { var origClient = store.state.clientState.map[action.clientId]; repository .saveData(store.state.selectedCompany, store.state.authState, origClient, EntityAction.archive) .then((client) { store.dispatch(ArchiveClientSuccess(client)); if (action.completer != null) { action.completer.complete(null); } }).catchError((error) { print(error); store.dispatch(ArchiveClientFailure(origClient)); }); next(action); }; } Middleware _deleteClient(ClientRepository repository) { return (Store store, action, NextDispatcher next) { var origClient = store.state.clientState.map[action.clientId]; repository .saveData(store.state.selectedCompany, store.state.authState, origClient, EntityAction.delete) .then((client) { store.dispatch(DeleteClientSuccess(client)); if (action.completer != null) { action.completer.complete(null); } }).catchError((error) { print(error); store.dispatch(DeleteClientFailure(origClient)); }); next(action); }; } Middleware _restoreClient(ClientRepository repository) { return (Store store, action, NextDispatcher next) { var origClient = store.state.clientState.map[action.clientId]; repository .saveData(store.state.selectedCompany, store.state.authState, origClient, EntityAction.restore) .then((client) { store.dispatch(RestoreClientSuccess(client)); if (action.completer != null) { action.completer.complete(null); } }).catchError((error) { print(error); store.dispatch(RestoreClientFailure(origClient)); }); next(action); }; } Middleware _saveClient(ClientRepository repository) { return (Store store, action, NextDispatcher next) { repository .saveData( store.state.selectedCompany, store.state.authState, action.client) .then((client) { if (action.client.isNew()) { store.dispatch(AddClientSuccess(client)); } else { store.dispatch(SaveClientSuccess(client)); } action.completer.complete(null); }).catchError((error) { print(error); store.dispatch(SaveClientFailure(error)); }); next(action); }; } Middleware _loadClients(ClientRepository repository) { return (Store store, action, NextDispatcher next) { AppState state = store.state; if (!state.clientState.isStale && !action.force) { next(action); return; } if (state.isLoading) { next(action); return; } store.dispatch(LoadClientsRequest()); repository .loadList(state.selectedCompany, state.authState) .then((data) { store.dispatch(LoadClientsSuccess(data)); if (action.completer != null) { action.completer.complete(null); } if (state.productState.isStale) { store.dispatch(LoadProducts()); } }).catchError((error) => store.dispatch(LoadClientsFailure(error))); next(action); }; }