Skip to content

Commit 89bdb2d

Browse files
authored
Merge pull request #5629 from woocommerce/issue/5598-5599-amount-validations
2 parents 7905089 + e6bc50b commit 89bdb2d

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

WooCommerce/Classes/ViewRelated/Orders/Simple Payments/Amount/SimplePaymentsAmount.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ extension SimplePaymentsAmountHostingController: UIAdaptivePresentationControlle
7575
}
7676

7777
func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
78-
!rootView.viewModel.disableCancel
78+
!rootView.viewModel.disableViewActions
7979
}
8080
}
8181

@@ -139,9 +139,9 @@ struct SimplePaymentsAmount: View {
139139
dismiss()
140140
viewModel.userDidCancelFlow()
141141
})
142-
.disabled(viewModel.disableCancel)
143142
}
144143
}
144+
.disabled(viewModel.disableViewActions)
145145
.wooNavigationBarStyle()
146146
}
147147

WooCommerce/Classes/ViewRelated/Orders/Simple Payments/Amount/SimplePaymentsAmountViewModel.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ final class SimplePaymentsAmountViewModel: ObservableObject {
3535
///
3636
var onOrderCreated: (Order) -> Void = { _ in }
3737

38-
/// Returns true when amount has less than two characters.
39-
/// Less than two, because `$` should be the first character.
38+
/// Returns true when the amount is not a positive number.
4039
///
4140
var shouldDisableDoneButton: Bool {
42-
amount.count < 2
41+
let decimalAmount = (currencyFormatter.convertToDecimal(from: amount) ?? .zero) as Decimal
42+
return decimalAmount <= .zero
4343
}
4444

45-
/// Use this to disables interactive dismissal and
46-
/// Disables cancel button while performing the create order operation
45+
/// Defines if the view actions should be disabled.
46+
/// Currently true while a network operation is happening.
4747
///
48-
var disableCancel: Bool {
48+
var disableViewActions: Bool {
4949
loading
5050
}
5151

@@ -85,6 +85,10 @@ final class SimplePaymentsAmountViewModel: ObservableObject {
8585
///
8686
private let storeCurrencySettings: CurrencySettings
8787

88+
/// Currency formatter for the provided amount
89+
///
90+
private let currencyFormatter: CurrencyFormatter
91+
8892
/// Current store currency symbol
8993
///
9094
private let storeCurrencySymbol: String
@@ -110,6 +114,7 @@ final class SimplePaymentsAmountViewModel: ObservableObject {
110114
self.presentNoticeSubject = presentNoticeSubject
111115
self.storeCurrencySettings = storeCurrencySettings
112116
self.storeCurrencySymbol = storeCurrencySettings.symbol(from: storeCurrencySettings.currencyCode)
117+
self.currencyFormatter = CurrencyFormatter(currencySettings: storeCurrencySettings)
113118
self.analytics = analytics
114119
self.isDevelopmentPrototype = isDevelopmentPrototype
115120
}

WooCommerce/Classes/ViewRelated/Orders/Simple Payments/Summary/SimplePaymentsSummary.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct SimplePaymentsSummary: View {
5959
viewModel.reloadContent()
6060
}, viewModel: viewModel.noteViewModel)
6161
}
62+
.disabled(viewModel.disableViewActions)
6263
}
6364
}
6465

WooCommerce/Classes/ViewRelated/Orders/Simple Payments/Summary/SimplePaymentsSummaryViewModel.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ final class SimplePaymentsSummaryViewModel: ObservableObject {
4646
noteViewModel.newNote
4747
}
4848

49+
/// Disable view actions while a network request is being performed
50+
///
51+
var disableViewActions: Bool {
52+
return showLoadingIndicator
53+
}
54+
4955
/// Total to charge with taxes.
5056
///
5157
private let totalWithTaxes: String

WooCommerce/WooCommerceTests/ViewRelated/Orders/Simple Payments/SimplePaymentsAmountViewModelTests.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ final class SimplePaymentsAmountViewModelTests: XCTestCase {
9191
XCTAssertTrue(viewModel.shouldDisableDoneButton)
9292
}
9393

94+
func test_view_model_disables_next_button_when_amount_is_not_greater_than_zero() {
95+
// Given
96+
let viewModel = SimplePaymentsAmountViewModel(siteID: sampleSiteID, storeCurrencySettings: usStoreSettings)
97+
98+
// When
99+
viewModel.amount = "$0"
100+
101+
// Then
102+
XCTAssertTrue(viewModel.shouldDisableDoneButton)
103+
}
104+
94105
func test_view_model_enables_next_button_when_amount_has_more_than_one_character() {
95106
// Given
96107
let viewModel = SimplePaymentsAmountViewModel(siteID: sampleSiteID, storeCurrencySettings: usStoreSettings)
@@ -320,7 +331,7 @@ final class SimplePaymentsAmountViewModelTests: XCTestCase {
320331
XCTAssertFalse(viewModel.loading)
321332

322333
// Before creating simple payment order
323-
XCTAssertFalse(viewModel.disableCancel)
334+
XCTAssertFalse(viewModel.disableViewActions)
324335

325336
// When
326337
let _: Bool = waitFor { promise in
@@ -336,7 +347,7 @@ final class SimplePaymentsAmountViewModelTests: XCTestCase {
336347
}
337348

338349
// Then
339-
XCTAssertTrue(viewModel.disableCancel)
350+
XCTAssertTrue(viewModel.disableViewActions)
340351
}
341352

342353
}

0 commit comments

Comments
 (0)