Skip to content

Commit cf012d6

Browse files
committed
Add new section for coupon management in beta features screen
1 parent 589e78f commit cf012d6

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Settings/Beta features/BetaFeaturesViewController.swift

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ private extension BetaFeaturesViewController {
7676
productsSection(),
7777
orderCreationSection(),
7878
inPersonPaymentsSection(),
79-
productSKUInputScannerSection()
79+
productSKUInputScannerSection(),
80+
couponManagementSection()
8081
].compactMap { $0 }
8182
}
8283

@@ -117,6 +118,14 @@ private extension BetaFeaturesViewController {
117118
.productSKUInputScannerDescription])
118119
}
119120

121+
func couponManagementSection() -> Section? {
122+
guard ServiceLocator.featureFlagService.isFeatureFlagEnabled(.couponManagement) else {
123+
return nil
124+
}
125+
return Section(rows: [.couponManagement,
126+
.couponManagementDescription])
127+
}
128+
120129
/// Register table cells.
121130
///
122131
func registerTableViewCells() {
@@ -159,6 +168,10 @@ private extension BetaFeaturesViewController {
159168
configureProductSKUInputScannerSwitch(cell: cell)
160169
case let cell as BasicTableViewCell where row == .productSKUInputScannerDescription:
161170
configureProductSKUInputScannerDescription(cell: cell)
171+
case let cell as SwitchTableViewCell where row == .couponManagement:
172+
configureCouponManagementSwitch(cell: cell)
173+
case let cell as BasicTableViewCell where row == .couponManagementDescription:
174+
configureCouponManagementDescription(cell: cell)
162175
default:
163176
fatalError()
164177
}
@@ -322,6 +335,37 @@ private extension BetaFeaturesViewController {
322335
configureCommonStylesForDescriptionCell(cell)
323336
cell.textLabel?.text = Localization.productSKUInputScannerDescription
324337
}
338+
339+
func configureCouponManagementSwitch(cell: SwitchTableViewCell) {
340+
configureCommonStylesForSwitchCell(cell)
341+
cell.title = Localization.couponManagementTitle
342+
343+
// Fetch switch's state stored value.
344+
let action = AppSettingsAction.loadCouponManagementFeatureSwitchState { result in
345+
guard let isEnabled = try? result.get() else {
346+
return cell.isOn = false
347+
}
348+
cell.isOn = isEnabled
349+
}
350+
ServiceLocator.stores.dispatch(action)
351+
352+
// Change switch's state stored value
353+
cell.onChange = { isSwitchOn in
354+
let action = AppSettingsAction.setCouponManagementFeatureSwitchState(isEnabled: isSwitchOn, onCompletion: { result in
355+
// Roll back toggle if an error occurred
356+
if result.isFailure {
357+
cell.isOn.toggle()
358+
}
359+
})
360+
ServiceLocator.stores.dispatch(action)
361+
}
362+
cell.accessibilityIdentifier = "beta-features-coupon-management-cell"
363+
}
364+
365+
func configureCouponManagementDescription(cell: BasicTableViewCell) {
366+
configureCommonStylesForDescriptionCell(cell)
367+
cell.textLabel?.text = Localization.couponManagementDescription
368+
}
325369
}
326370

327371
// MARK: - Shared Configurations
@@ -402,12 +446,16 @@ private enum Row: CaseIterable {
402446
case productSKUInputScanner
403447
case productSKUInputScannerDescription
404448

449+
// Coupon management
450+
case couponManagement
451+
case couponManagementDescription
452+
405453
var type: UITableViewCell.Type {
406454
switch self {
407-
case .orderAddOns, .orderCreation, .stripeExtensionInPersonPayments, .canadaInPersonPayments, .productSKUInputScanner:
455+
case .orderAddOns, .orderCreation, .stripeExtensionInPersonPayments, .canadaInPersonPayments, .productSKUInputScanner, .couponManagement:
408456
return SwitchTableViewCell.self
409457
case .orderAddOnsDescription, .orderCreationDescription, .stripeExtensionInPersonPaymentsDescription, .canadaInPersonPaymentsDescription,
410-
.productSKUInputScannerDescription:
458+
.productSKUInputScannerDescription, .couponManagementDescription:
411459
return BasicTableViewCell.self
412460
}
413461
}

0 commit comments

Comments
 (0)