Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,20 @@ final class ProductFormViewController<ViewModel: ProductFormViewModelProtocol>:
}
}

if viewModel.canSaveAsDraft() || viewModel.productModel.status == .draft {
actionSheet.addDefaultActionWithTitle(ActionSheetStrings.previewProduct) { [weak self] _ in
if self?.viewModel.canSaveAsDraft() == true || self?.viewModel.hasUnsavedChanges() == true {
self?.saveProduct(status: .draft) { result in
if result.isSuccess {
self?.displayProductPreview()
}
}
} else {
self?.displayProductPreview()
}
}
}

/// The "View product in store" action will be shown only if the product is published.
if viewModel.canViewProductInStore() {
actionSheet.addDefaultActionWithTitle(ActionSheetStrings.viewProduct) { [weak self] _ in
Expand Down Expand Up @@ -716,14 +730,14 @@ private extension ProductFormViewController {
// MARK: Navigation actions
//
private extension ProductFormViewController {
func saveProduct(status: ProductStatus? = nil) {
func saveProduct(status: ProductStatus? = nil, onCompletion: @escaping (Result<Void, ProductUpdateError>) -> Void = { _ in }) {
let productStatus = status ?? product.status
let messageType = viewModel.saveMessageType(for: productStatus)
showSavingProgress(messageType)
saveProductRemotely(status: status)
saveProductRemotely(status: status, onCompletion: onCompletion)
}

func saveProductRemotely(status: ProductStatus?) {
func saveProductRemotely(status: ProductStatus?, onCompletion: @escaping (Result<Void, ProductUpdateError>) -> Void = { _ in }) {
viewModel.saveProductRemotely(status: status) { [weak self] result in
switch result {
case .failure(let error):
Expand All @@ -732,6 +746,7 @@ private extension ProductFormViewController {
// Dismisses the in-progress UI then presents the error alert.
self?.navigationController?.dismiss(animated: true) {
self?.displayError(error: error)
onCompletion(.failure(error))
}
case .success:
// Dismisses the in-progress UI, then presents the confirmation alert.
Expand All @@ -741,6 +756,7 @@ private extension ProductFormViewController {
// Show linked products promo banner after product save
(self?.viewModel as? ProductFormViewModel)?.isLinkedProductsPromoEnabled = true
self?.reloadLinkedPromoCellAnimated()
onCompletion(.success(()))
}
}
}
Expand Down Expand Up @@ -778,6 +794,18 @@ private extension ProductFormViewController {
SharingHelper.shareURL(url: url, title: product.name, from: view, in: self)
}

func displayProductPreview() {
var permalink = URLComponents(string: product.permalink)
var updatedQueryItems = permalink?.queryItems ?? []
updatedQueryItems.append(.init(name: "preview", value: "true"))
permalink?.queryItems = updatedQueryItems
guard let url = permalink?.url else {
return
}

WebviewHelper.launch(url, with: self)
}

func duplicateProduct() {
showSavingProgress(.duplicate)
viewModel.duplicateProduct(onCompletion: { [weak self] result in
Expand Down Expand Up @@ -1555,6 +1583,8 @@ private enum Localization {
private enum ActionSheetStrings {
static let saveProductAsDraft = NSLocalizedString("Save as draft",
comment: "Button title to save a product as draft in Product More Options Action Sheet")
static let previewProduct = NSLocalizedString("Preview",
comment: "Button title to open preview link for a product in Product More Options Action Sheet")
static let viewProduct = NSLocalizedString("View Product in Store",
comment: "Button title View product in store in Edit Product More Options Action Sheet")
static let share = NSLocalizedString("Share", comment: "Button title Share in Edit Product More Options Action Sheet")
Expand Down