From 554d619b7a041da96843e9676f66050fb4c78a39 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sun, 26 Jun 2022 12:42:49 +0300 Subject: [PATCH] Purchase orders --- .../purchase_order_actions.dart | 16 +++--- .../purchase_order_middleware.dart | 52 +++++++++++++++++++ .../view/purchase_order_view_vm.dart | 1 + 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/lib/redux/purchase_order/purchase_order_actions.dart b/lib/redux/purchase_order/purchase_order_actions.dart index 964cf2f86..4d1350ad2 100644 --- a/lib/redux/purchase_order/purchase_order_actions.dart +++ b/lib/redux/purchase_order/purchase_order_actions.dart @@ -274,15 +274,15 @@ class DownloadPurchaseOrdersFailure implements StopSaving { final Object error; } -class AcceptPurchaseOrders implements StartSaving { - AcceptPurchaseOrders(this.completer, this.purchaseOrderIds); +class AcceptPurchaseOrdersRequest implements StartSaving { + AcceptPurchaseOrdersRequest(this.completer, this.purchaseOrderIds); final List purchaseOrderIds; final Completer completer; } class AcceptPurchaseOrderSuccess implements StopSaving { - AcceptPurchaseOrderSuccess({this.purchaseOrders}); + AcceptPurchaseOrderSuccess(this.purchaseOrders); final List purchaseOrders; } @@ -293,15 +293,15 @@ class AcceptPurchaseOrderFailure implements StopSaving { final dynamic error; } -class CancelPurchaseOrders implements StartSaving { - CancelPurchaseOrders(this.completer, this.purchaseOrderIds); +class CancelPurchaseOrdersRequest implements StartSaving { + CancelPurchaseOrdersRequest(this.completer, this.purchaseOrderIds); final List purchaseOrderIds; final Completer completer; } class CancelPurchaseOrderSuccess implements StopSaving { - CancelPurchaseOrderSuccess({this.purchaseOrders}); + CancelPurchaseOrderSuccess(this.purchaseOrders); final List purchaseOrders; } @@ -566,7 +566,7 @@ void handlePurchaseOrderAction(BuildContext context, purchaseOrderIds)); break; case EntityAction.cancelInvoice: - store.dispatch(CancelPurchaseOrders( + store.dispatch(CancelPurchaseOrdersRequest( snackBarCompleter( context, purchaseOrders.length == 1 @@ -575,7 +575,7 @@ void handlePurchaseOrderAction(BuildContext context, purchaseOrderIds)); break; case EntityAction.accept: - store.dispatch(AcceptPurchaseOrders( + store.dispatch(AcceptPurchaseOrdersRequest( snackBarCompleter( context, purchaseOrders.length == 1 diff --git a/lib/redux/purchase_order/purchase_order_middleware.dart b/lib/redux/purchase_order/purchase_order_middleware.dart index f33dd4982..19c6bb7cd 100644 --- a/lib/redux/purchase_order/purchase_order_middleware.dart +++ b/lib/redux/purchase_order/purchase_order_middleware.dart @@ -37,6 +37,8 @@ List> createStorePurchaseOrdersMiddleware([ final emailPurchaseOrder = _emailPurchaseOrder(repository); final bulkEmailPurchaseOrders = _bulkEmailPurchaseOrders(repository); final markSentPurchaseOrder = _markSentPurchaseOrder(repository); + final acceptPurchaseOrders = _acceptPurchaseOrders(repository); + final cancelPurchaseOrders = _cancelPurchaseOrders(repository); final downloadPurchaseOrders = _downloadPurchaseOrders(repository); final saveDocument = _saveDocument(repository); @@ -60,6 +62,10 @@ List> createStorePurchaseOrdersMiddleware([ bulkEmailPurchaseOrders), TypedMiddleware( markSentPurchaseOrder), + TypedMiddleware( + acceptPurchaseOrders), + TypedMiddleware( + cancelPurchaseOrders), TypedMiddleware( downloadPurchaseOrders), TypedMiddleware(saveDocument), @@ -274,6 +280,52 @@ Middleware _markSentPurchaseOrder( }; } +Middleware _acceptPurchaseOrders(PurchaseOrderRepository repository) { + return (Store store, dynamic dynamicAction, NextDispatcher next) { + final action = dynamicAction as AcceptPurchaseOrdersRequest; + repository + .bulkAction(store.state.credentials, action.purchaseOrderIds, + EntityAction.accept) + .then((purchaseOrders) { + store.dispatch(AcceptPurchaseOrderSuccess(purchaseOrders)); + if (action.completer != null) { + action.completer.complete(null); + } + }).catchError((Object error) { + print(error); + store.dispatch(AcceptPurchaseOrderFailure(error)); + if (action.completer != null) { + action.completer.completeError(error); + } + }); + + next(action); + }; +} + +Middleware _cancelPurchaseOrders(PurchaseOrderRepository repository) { + return (Store store, dynamic dynamicAction, NextDispatcher next) { + final action = dynamicAction as CancelPurchaseOrdersRequest; + repository + .bulkAction(store.state.credentials, action.purchaseOrderIds, + EntityAction.cancel) + .then((purchaseOrders) { + store.dispatch(CancelPurchaseOrderSuccess(purchaseOrders)); + if (action.completer != null) { + action.completer.complete(null); + } + }).catchError((Object error) { + print(error); + store.dispatch(CancelPurchaseOrderFailure(error)); + if (action.completer != null) { + action.completer.completeError(error); + } + }); + + next(action); + }; +} + Middleware _emailPurchaseOrder(PurchaseOrderRepository repository) { return (Store store, dynamic dynamicAction, NextDispatcher next) { final action = dynamicAction as EmailPurchaseOrderRequest; diff --git a/lib/ui/purchase_order/view/purchase_order_view_vm.dart b/lib/ui/purchase_order/view/purchase_order_view_vm.dart index 5025f602c..6c21183f5 100644 --- a/lib/ui/purchase_order/view/purchase_order_view_vm.dart +++ b/lib/ui/purchase_order/view/purchase_order_view_vm.dart @@ -27,6 +27,7 @@ class PurchaseOrderViewScreen extends StatelessWidget { Key key, this.isFilter = false, }) : super(key: key); + final bool isFilter; static const String route = '/purchase_order/view';