Skip to content

Commit 5ffc0a3

Browse files
committed
Clear products onboarding banner and resync announcements when a product is added
1 parent 5b4457a commit 5ffc0a3

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/DashboardViewController.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ private extension DashboardViewController {
318318
private func startAddProductFlow() {
319319
guard let announcementView, let navigationController else { return }
320320
let coordinator = AddProductCoordinator(siteID: siteID, sourceView: announcementView, sourceNavigationController: navigationController)
321+
coordinator.onProductCreated = { [weak self] in
322+
guard let self else { return }
323+
self.viewModel.announcementViewModel = nil // Remove the products onboarding banner
324+
self.viewModel.syncAnnouncements(for: self.siteID)
325+
}
321326
coordinator.start()
322327
}
323328

WooCommerce/Classes/ViewRelated/Products/Add Product/AddProductCoordinator.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ final class AddProductCoordinator: Coordinator {
3030
return controller
3131
}()
3232

33+
/// Assign this closure to be notified when a new product is saved remotely
34+
///
35+
var onProductCreated: () -> Void = {}
36+
3337
init(siteID: Int64,
3438
sourceBarButtonItem: UIBarButtonItem,
3539
sourceNavigationController: UINavigationController,
@@ -211,6 +215,7 @@ private extension AddProductCoordinator {
211215
let viewModel = ProductFormViewModel(product: model,
212216
formType: .add,
213217
productImageActionHandler: productImageActionHandler)
218+
viewModel.onProductCreated = onProductCreated
214219
let viewController = ProductFormViewController(viewModel: viewModel,
215220
eventLogger: ProductFormEventLogger(),
216221
productImageActionHandler: productImageActionHandler,

WooCommerce/Classes/ViewRelated/Products/Edit Product/ProductFormViewModel.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ final class ProductFormViewModel: ProductFormViewModelProtocol {
181181

182182
private let featureFlagService: FeatureFlagService
183183

184+
/// Assign this closure to be notified when a new product is saved remotely
185+
///
186+
var onProductCreated: () -> Void = {}
187+
184188
init(product: EditableProductModel,
185189
formType: ProductFormType,
186190
productImageActionHandler: ProductImageActionHandler,
@@ -432,7 +436,7 @@ extension ProductFormViewModel {
432436
let productWithStatusUpdated = product.product.copy(statusKey: status.rawValue)
433437
return EditableProductModel(product: productWithStatusUpdated)
434438
}()
435-
let remoteActionUseCase = ProductFormRemoteActionUseCase()
439+
let remoteActionUseCase = ProductFormRemoteActionUseCase(stores: stores)
436440
switch formType {
437441
case .add:
438442
let productIDBeforeSave = productModel.productID
@@ -449,6 +453,7 @@ extension ProductFormViewModel {
449453
onCompletion(.success(data.product))
450454
self.replaceProductID(productIDBeforeSave: productIDBeforeSave)
451455
self.saveProductImagesWhenNoneIsPendingUploadAnymore()
456+
self.onProductCreated()
452457
}
453458
}
454459
case .edit:

WooCommerce/WooCommerceTests/ViewRelated/Products/Edit Product/ProductFormViewModelTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,30 @@ final class ProductFormViewModelTests: XCTestCase {
579579
let hasLinkedProducts = try XCTUnwrap(analyticsProvider.receivedProperties.first?["has_linked_products"] as? Bool)
580580
XCTAssertTrue(hasLinkedProducts)
581581
}
582+
583+
func test_onProductCreated_called_when_new_product_saved_remotely() {
584+
// Given
585+
var isCallbackCalled = false
586+
let stores = MockStoresManager(sessionManager: .testingInstance)
587+
let viewModel = createViewModel(product: Product.fake(), formType: .add, stores: stores)
588+
viewModel.onProductCreated = {
589+
isCallbackCalled = true
590+
}
591+
592+
// When
593+
stores.whenReceivingAction(ofType: ProductAction.self) { action in
594+
switch action {
595+
case let .addProduct(product, onCompletion):
596+
onCompletion(.success(product))
597+
default:
598+
XCTFail("Received unsupported action: \(action)")
599+
}
600+
}
601+
viewModel.saveProductRemotely(status: .draft) { _ in }
602+
603+
// Then
604+
XCTAssertTrue(isCallbackCalled)
605+
}
582606
}
583607

584608
private extension ProductFormViewModelTests {

0 commit comments

Comments
 (0)