Skip to content

Commit 370fcbe

Browse files
committed
Reduce code duplication for syncing shipping labels
1 parent 820fb02 commit 370fcbe

File tree

1 file changed

+28
-40
lines changed

1 file changed

+28
-40
lines changed

WooCommerce/Classes/ViewModels/Order Details/OrderDetailsViewModel.swift

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -952,56 +952,44 @@ private extension OrderDetailsViewModel {
952952

953953
@MainActor func syncShippingLabelsForWooShipping() async -> [ShippingLabel] {
954954
await withCheckedContinuation { continuation in
955-
stores.dispatch(WooShippingAction.syncShippingLabels(siteID: order.siteID, orderID: order.orderID) { result in
956-
switch result {
957-
case .success(let shippingLabels):
958-
ServiceLocator.analytics.track(event: .shippingLabelsAPIRequest(
959-
result: .success,
960-
isRevampedFlow: true
961-
))
962-
continuation.resume(returning: shippingLabels)
963-
case .failure(let error):
964-
ServiceLocator.analytics.track(event: .shippingLabelsAPIRequest(
965-
result: .failed(error: error),
966-
isRevampedFlow: true
967-
))
968-
if error as? DotcomError == .noRestRoute {
969-
DDLogError("⚠️ Endpoint for synchronizing shipping labels is unreachable. WC Shipping plugin may be missing.")
970-
} else {
971-
DDLogError("⛔️ Error synchronizing shipping labels: \(error)")
972-
}
973-
continuation.resume(returning: [])
974-
}
955+
stores.dispatch(WooShippingAction.syncShippingLabels(siteID: order.siteID, orderID: order.orderID) { [weak self] result in
956+
let labels = self?.handleShippingLabelSyncingResult(result: result, isRevampedFlow: true) ?? []
957+
continuation.resume(returning: labels)
975958
})
976959
}
977960
}
978961

979962
@MainActor func syncShippingLabelsForLegacyPlugin(isRevampedFlow: Bool) async -> [ShippingLabel] {
980963
await withCheckedContinuation { continuation in
981-
stores.dispatch(ShippingLabelAction.synchronizeShippingLabels(siteID: order.siteID, orderID: order.orderID) { result in
982-
switch result {
983-
case .success(let shippingLabels):
984-
ServiceLocator.analytics.track(event: .shippingLabelsAPIRequest(
985-
result: .success,
986-
isRevampedFlow: isRevampedFlow
987-
))
988-
continuation.resume(returning: shippingLabels)
989-
case .failure(let error):
990-
ServiceLocator.analytics.track(event: .shippingLabelsAPIRequest(
991-
result: .failed(error: error),
992-
isRevampedFlow: isRevampedFlow
993-
))
994-
if error as? DotcomError == .noRestRoute {
995-
DDLogError("⚠️ Endpoint for synchronizing shipping labels is unreachable. WC Shipping plugin may be missing.")
996-
} else {
997-
DDLogError("⛔️ Error synchronizing shipping labels: \(error)")
998-
}
999-
continuation.resume(returning: [])
1000-
}
964+
stores.dispatch(ShippingLabelAction.synchronizeShippingLabels(siteID: order.siteID, orderID: order.orderID) { [weak self] result in
965+
let labels = self?.handleShippingLabelSyncingResult(result: result, isRevampedFlow: isRevampedFlow) ?? []
966+
continuation.resume(returning: labels)
1001967
})
1002968
}
1003969
}
1004970

971+
func handleShippingLabelSyncingResult(result: Result<[ShippingLabel], Error>, isRevampedFlow: Bool) -> [ShippingLabel] {
972+
switch result {
973+
case .success(let shippingLabels):
974+
ServiceLocator.analytics.track(event: .shippingLabelsAPIRequest(
975+
result: .success,
976+
isRevampedFlow: isRevampedFlow
977+
))
978+
return shippingLabels
979+
case .failure(let error):
980+
ServiceLocator.analytics.track(event: .shippingLabelsAPIRequest(
981+
result: .failed(error: error),
982+
isRevampedFlow: isRevampedFlow
983+
))
984+
if error as? DotcomError == .noRestRoute {
985+
DDLogError("⚠️ Endpoint for synchronizing shipping labels is unreachable. WC Shipping plugin may be missing.")
986+
} else {
987+
DDLogError("⛔️ Error synchronizing shipping labels: \(error)")
988+
}
989+
return []
990+
}
991+
}
992+
1005993
@MainActor
1006994
func isPluginActive(_ plugin: String) async -> Bool {
1007995
return await isPluginActive([plugin])

0 commit comments

Comments
 (0)