import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:invoiceninja_flutter/redux/app/app_actions.dart'; import 'package:redux/redux.dart'; import 'package:invoiceninja_flutter/utils/platforms.dart'; import 'package:invoiceninja_flutter/redux/app/app_middleware.dart'; import 'package:invoiceninja_flutter/data/models/models.dart'; import 'package:invoiceninja_flutter/redux/ui/ui_actions.dart'; import 'package:invoiceninja_flutter/ui/tax_rate/tax_rate_screen.dart'; import 'package:invoiceninja_flutter/ui/tax_rate/edit/tax_rate_edit_vm.dart'; import 'package:invoiceninja_flutter/ui/tax_rate/view/tax_rate_view_vm.dart'; import 'package:invoiceninja_flutter/redux/tax_rate/tax_rate_actions.dart'; import 'package:invoiceninja_flutter/redux/app/app_state.dart'; import 'package:invoiceninja_flutter/data/repositories/tax_rate_repository.dart'; List> createStoreTaxRatesMiddleware([ TaxRateRepository repository = const TaxRateRepository(), ]) { final viewTaxRateList = _viewTaxRateList(); final viewTaxRate = _viewTaxRate(); final editTaxRate = _editTaxRate(); final loadTaxRates = _loadTaxRates(repository); final loadTaxRate = _loadTaxRate(repository); final saveTaxRate = _saveTaxRate(repository); final archiveTaxRate = _archiveTaxRate(repository); final deleteTaxRate = _deleteTaxRate(repository); final restoreTaxRate = _restoreTaxRate(repository); return [ TypedMiddleware(viewTaxRateList), TypedMiddleware(viewTaxRate), TypedMiddleware(editTaxRate), TypedMiddleware(loadTaxRates), TypedMiddleware(loadTaxRate), TypedMiddleware(saveTaxRate), TypedMiddleware(archiveTaxRate), TypedMiddleware(deleteTaxRate), TypedMiddleware(restoreTaxRate), ]; } Middleware _editTaxRate() { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as EditTaxRate; if (!action.force && hasChanges(store: store, context: action.context, action: action)) { return; } next(action); store.dispatch(UpdateCurrentRoute(TaxRateEditScreen.route)); if (isMobile(action.context)) { action.navigator.pushNamed(TaxRateEditScreen.route); } }; } Middleware _viewTaxRate() { return (Store store, dynamic dynamicAction, NextDispatcher next) async { final action = dynamicAction as ViewTaxRate; if (!action.force && hasChanges(store: store, context: action.context, action: action)) { return; } next(action); store.dispatch(UpdateCurrentRoute(TaxRateViewScreen.route)); if (isMobile(action.context)) { action.navigator.pushNamed(TaxRateViewScreen.route); } }; } Middleware _viewTaxRateList() { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as ViewTaxRateList; if (!action.force && hasChanges(store: store, context: action.context, action: action)) { return; } next(action); if (store.state.isStale) { store.dispatch(RefreshData()); } store.dispatch(UpdateCurrentRoute(TaxRateSettingsScreen.route)); if (isMobile(action.context)) { action.navigator.pushNamedAndRemoveUntil( TaxRateSettingsScreen.route, (Route route) => false); } }; } Middleware _archiveTaxRate(TaxRateRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as ArchiveTaxRateRequest; final prevTaxRates = action.taxRateIds .map((id) => store.state.taxRateState.map[id]) .toList(); repository .bulkAction( store.state.credentials, action.taxRateIds, EntityAction.archive) .then((List taxRates) { store.dispatch(ArchiveTaxRatesSuccess(taxRates)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); store.dispatch(ArchiveTaxRateFailure(prevTaxRates)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; } Middleware _deleteTaxRate(TaxRateRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as DeleteTaxRateRequest; final prevTaxRates = action.taxRateIds .map((id) => store.state.taxRateState.map[id]) .toList(); repository .bulkAction( store.state.credentials, action.taxRateIds, EntityAction.delete) .then((List taxRates) { store.dispatch(DeleteTaxRatesSuccess(taxRates)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); store.dispatch(DeleteTaxRateFailure(prevTaxRates)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; } Middleware _restoreTaxRate(TaxRateRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as RestoreTaxRateRequest; final prevTaxRates = action.taxRateIds .map((id) => store.state.taxRateState.map[id]) .toList(); repository .bulkAction( store.state.credentials, action.taxRateIds, EntityAction.restore) .then((List taxRates) { store.dispatch(RestoreTaxRatesSuccess(taxRates)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); store.dispatch(RestoreTaxRateFailure(prevTaxRates)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; } Middleware _saveTaxRate(TaxRateRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as SaveTaxRateRequest; repository .saveData(store.state.credentials, action.taxRate) .then((TaxRateEntity taxRate) { if (action.taxRate.isNew) { store.dispatch(AddTaxRateSuccess(taxRate)); } else { store.dispatch(SaveTaxRateSuccess(taxRate)); } action.completer.complete(taxRate); }).catchError((Object error) { print(error); store.dispatch(SaveTaxRateFailure(error)); action.completer.completeError(error); }); next(action); }; } Middleware _loadTaxRate(TaxRateRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as LoadTaxRate; final AppState state = store.state; if (state.isLoading) { next(action); return; } store.dispatch(LoadTaxRateRequest()); repository.loadItem(state.credentials, action.taxRateId).then((taxRate) { store.dispatch(LoadTaxRateSuccess(taxRate)); if (action.completer != null) { action.completer.complete(null); } }).catchError((Object error) { print(error); store.dispatch(LoadTaxRateFailure(error)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; } Middleware _loadTaxRates(TaxRateRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as LoadTaxRates; final AppState state = store.state; if (state.isLoading) { next(action); return; } store.dispatch(LoadTaxRatesRequest()); repository.loadList(state.credentials).then((data) { store.dispatch(LoadTaxRatesSuccess(data)); if (action.completer != null) { action.completer.complete(null); } /* if (state.taxRateState.isStale) { store.dispatch(LoadTaxRates()); } */ }).catchError((Object error) { print(error); store.dispatch(LoadTaxRatesFailure(error)); if (action.completer != null) { action.completer.completeError(error); } }); next(action); }; }