@@ -18,7 +18,17 @@ class NewOrdersViewController: UIViewController {
1818 @IBOutlet private weak var actionButton : UIButton !
1919 @IBOutlet private weak var bottomSpacerView : UIView !
2020
21- private var orderStatuses : [ OrderStatus ] ?
21+ private var orderStatuses : [ OrderStatus ] ? {
22+ didSet {
23+ updateNewOrdersIfNeeded ( orderCount: totalPendingOrders)
24+ }
25+ }
26+
27+ /// Total number of pending server-side orders
28+ ///
29+ private var totalPendingOrders : Int {
30+ return orderStatuses? . filter ( { $0. slug == OrderStatusEnum . processing. rawValue } ) . map ( { $0. total } ) . reduce ( 0 , + ) ?? 0
31+ }
2232
2333 // MARK: - Public Properties
2434
@@ -47,35 +57,12 @@ extension NewOrdersViewController {
4757 return
4858 }
4959
50- let action = StatsAction . retrieveOrderTotals ( siteID: siteID, status: . processing) { [ weak self] ( processingOrderCount, error) in
51- guard let `self` = self , let processingOrderCount = processingOrderCount else {
52- if let error = error {
53- DDLogError ( " ⛔️ Dashboard (New Orders) — Error synchronizing pending orders: \( error) " )
54- }
55- onCompletion ? ( error)
56- return
57- }
58-
59- self . updateNewOrdersIfNeeded ( orderCount: processingOrderCount)
60- onCompletion ? ( nil )
61- }
62-
63- StoresManager . shared. dispatch ( action)
64- }
65-
66- func syncOrderStatus( onCompletion: ( ( Error ? ) -> Void ) ? = nil ) {
67- guard let siteID = StoresManager . shared. sessionManager. defaultStoreID else {
68- onCompletion ? ( nil )
69- return
70- }
71-
7260 let action = OrderStatusAction . retrieveOrderStatuses ( siteID: siteID) { [ weak self] ( orderStatuses, error) in
7361 if let error = error {
7462 DDLogError ( " ⛔️ Dashboard (New Orders) — Error synchronizing order statuses: \( error) " )
7563 }
7664
7765 self ? . orderStatuses = orderStatuses
78-
7966 onCompletion ? ( error)
8067 }
8168
@@ -110,18 +97,14 @@ private extension NewOrdersViewController {
11097private extension NewOrdersViewController {
11198
11299 @IBAction func buttonTouchUpInside( _ sender: UIButton ) {
113- sender. fadeOutSelectedBackground {
114- WooAnalytics . shared. track ( . dashboardNewOrdersButtonTapped)
115-
116- guard let statuses = self . orderStatuses else {
117- DDLogError ( " Error: missing list of order statuses. Cannot present new orders list. " )
118- return
119- }
120-
121- for filterStatus in statuses where filterStatus. slug == OrderStatusEnum . processing. rawValue {
122- MainTabBarController . presentOrders ( statusFilter: filterStatus)
123- }
100+ sender. fadeOutSelectedBackground ( )
101+ guard let pendingStatus = orderStatuses? . first ( where: { $0. slug == OrderStatusEnum . processing. rawValue } ) else {
102+ DDLogError ( " ⛔️ Unable to display pending orders list — unable to locate pending status for current site. " )
103+ return
124104 }
105+
106+ WooAnalytics . shared. track ( . dashboardNewOrdersButtonTapped)
107+ MainTabBarController . presentOrders ( statusFilter: pendingStatus)
125108 }
126109
127110 @IBAction func buttonTouchUpOutside( _ sender: UIButton ) {
0 commit comments