Skip to content

Commit e9e26e8

Browse files
committed
Trigger price action
1 parent 29a5370 commit e9e26e8

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

WooCommerce/Classes/ViewRelated/Products/PriceInputViewModel.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,15 @@ final class PriceInputViewModel {
2020
private let currencySettings: CurrencySettings
2121
private let currencyFormatter: CurrencyFormatter
2222

23-
private let cancelClosure: () -> Void
24-
private let applyClosure: (String) -> Void
23+
var cancelClosure: () -> Void = {}
24+
var applyClosure: (String) -> Void = { _ in }
2525

2626
init(productListViewModel: ProductListViewModel,
27-
currencySettings: CurrencySettings = ServiceLocator.currencySettings,
28-
cancelClosure: @escaping () -> Void,
29-
applyClosure: @escaping (String) -> Void) {
27+
currencySettings: CurrencySettings = ServiceLocator.currencySettings) {
3028
self.productListViewModel = productListViewModel
3129
self.priceSettingsValidator = ProductPriceSettingsValidator(currencySettings: currencySettings)
3230
self.currencySettings = currencySettings
3331
self.currencyFormatter = CurrencyFormatter(currencySettings: currencySettings)
34-
self.cancelClosure = cancelClosure
35-
self.applyClosure = applyClosure
3632
}
3733

3834
/// Called when the cancel button is tapped

WooCommerce/Classes/ViewRelated/Products/ProductsListViewModel.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,24 @@ class ProductListViewModel {
9696
}
9797
stores.dispatch(batchAction)
9898
}
99+
100+
/// Update selected products with new price and trigger Network action to save the change remotely.
101+
///
102+
func updateSelectedProducts(with newPrice: String, completion: @escaping (Result<Void, Error>) -> Void ) {
103+
guard selectedProductsCount > 0 else {
104+
completion(.failure(BulkEditError.noProductsSelected))
105+
return
106+
}
107+
108+
let updatedProducts = selectedProducts.map({ $0.copy(regularPrice: newPrice) })
109+
let batchAction = ProductAction.updateProducts(siteID: siteID, products: updatedProducts) { result in
110+
switch result {
111+
case .success:
112+
completion(.success(()))
113+
case .failure(let error):
114+
completion(.failure(error))
115+
}
116+
}
117+
stores.dispatch(batchAction)
118+
}
99119
}

WooCommerce/Classes/ViewRelated/Products/ProductsViewController.swift

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,15 +405,35 @@ private extension ProductsViewController {
405405
}
406406

407407
func showPriceBulkEditingModal() {
408-
let priceInputViewModel = PriceInputViewModel(productListViewModel: viewModel) { [weak self] in
408+
let priceInputViewModel = PriceInputViewModel(productListViewModel: viewModel)
409+
let priceInputViewController = PriceInputViewController(viewModel: priceInputViewModel)
410+
priceInputViewModel.cancelClosure = { [weak self] in
409411
self?.dismissModal()
410-
} applyClosure: { newPrice in
411-
//
412412
}
413-
let priceInputViewController = PriceInputViewController(viewModel: priceInputViewModel)
413+
priceInputViewModel.applyClosure = { [weak self] newPrice in
414+
self?.applyBulkEditingPrice(newPrice: newPrice, modalVC: priceInputViewController)
415+
}
414416
present(WooNavigationController(rootViewController: priceInputViewController), animated: true)
415417
}
416418

419+
func applyBulkEditingPrice(newPrice: String?, modalVC: UIViewController) {
420+
guard let newPrice else { return }
421+
422+
displayProductsSavingInProgressView(on: modalVC)
423+
viewModel.updateSelectedProducts(with: newPrice) { [weak self] result in
424+
guard let self else { return }
425+
426+
self.dismiss(animated: true, completion: nil)
427+
switch result {
428+
case .success:
429+
self.finishBulkEditing()
430+
self.presentNotice(title: Localization.priceUpdatedNotice)
431+
case .failure:
432+
self.presentNotice(title: Localization.updateErrorNotice)
433+
}
434+
}
435+
}
436+
417437
func displayProductsSavingInProgressView(on vc: UIViewController) {
418438
let viewProperties = InProgressViewProperties(title: Localization.productsSavingTitle, message: Localization.productsSavingMessage)
419439
let inProgressViewController = InProgressViewController(viewProperties: viewProperties)
@@ -1311,6 +1331,8 @@ private extension ProductsViewController {
13111331

13121332
static let statusUpdatedNotice = NSLocalizedString("Status updated",
13131333
comment: "Title of the notice when a user updated status for selected products")
1334+
static let priceUpdatedNotice = NSLocalizedString("Price updated",
1335+
comment: "Title of the notice when a user updated price for selected products")
13141336
static let updateErrorNotice = NSLocalizedString("Cannot update products",
13151337
comment: "Title of the notice when there is an error updating selected products")
13161338
}

0 commit comments

Comments
 (0)