Skip to content

Commit

Permalink
[ABW-2165] Only allow customize guarantees if the amount is positive (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kugel3 committed Sep 1, 2023
1 parent 4a6a7c9 commit eb72cf8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
32 changes: 16 additions & 16 deletions Sources/Features/TransactionReviewFeature/TransactionReview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,8 @@ public struct TransactionReview: Sendable, FeatureReducer {
return .none

case .deposits(.delegate(.showCustomizeGuarantees)):
guard let deposits = state.deposits else { return .none } // TODO: Handle?

let guarantees = deposits.accounts
.flatMap { account -> [TransactionReviewGuarantee.State] in
account.transfers
.compactMap(\.fungible)
.filter { $0.guarantee != nil }
.compactMap { .init(account: account.account, transfer: $0) }
}

// TODO: Handle?
guard let guarantees = state.deposits?.accounts.customizableGuarantees, !guarantees.isEmpty else { return .none }
state.destination = .customizeGuarantees(.init(guarantees: .init(uniqueElements: guarantees)))

return .none
Expand Down Expand Up @@ -477,6 +469,17 @@ public struct TransactionReview: Sendable, FeatureReducer {
}
}

extension Collection<TransactionReviewAccount.State> {
var customizableGuarantees: [TransactionReviewGuarantee.State] {
flatMap { account in
account.transfers
.compactMap(\.fungible)
.filter { $0.guarantee != nil }
.compactMap { .init(account: account.account, transfer: $0) }
}
}
}

extension TransactionReview {
func review(_ state: inout State) -> EffectTask<Action> {
guard let transactionToReview = state.reviewedTransaction else {
Expand Down Expand Up @@ -672,7 +675,7 @@ extension TransactionReview {
let accounts = withdrawals.map {
TransactionReviewAccount.State(account: $0.key, transfers: .init(uniqueElements: $0.value))
}
return .init(accounts: .init(uniqueElements: accounts), showCustomizeGuarantees: false)
return .init(accounts: .init(uniqueElements: accounts), enableCustomizeGuarantees: false)
}

private func extractDeposits(
Expand Down Expand Up @@ -707,11 +710,8 @@ extension TransactionReview {

guard !reviewAccounts.isEmpty else { return nil }

let requiresGuarantees = reviewAccounts.contains { reviewAccount in
reviewAccount.transfers.contains { $0.fungible?.guarantee != nil }
}

return .init(accounts: .init(uniqueElements: reviewAccounts), showCustomizeGuarantees: requiresGuarantees)
let requiresGuarantees = !reviewAccounts.customizableGuarantees.isEmpty
return .init(accounts: .init(uniqueElements: reviewAccounts), enableCustomizeGuarantees: requiresGuarantees)
}

func transferInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import FeaturePrelude

extension TransactionReviewAccounts.State {
var viewState: TransactionReviewAccounts.ViewState {
.init(showCustomizeGuarantees: showCustomizeGuarantees)
.init(showCustomizeGuaranteesButton: enableCustomizeGuarantees)
}
}

extension TransactionReviewAccounts {
public struct ViewState: Equatable {
let showCustomizeGuarantees: Bool
let showCustomizeGuaranteesButton: Bool
}

@MainActor
Expand All @@ -32,7 +32,7 @@ extension TransactionReviewAccounts {
content: { TransactionReviewAccount.View(store: $0) }
)

if viewStore.showCustomizeGuarantees {
if viewStore.showCustomizeGuaranteesButton {
Button(L10n.TransactionReview.customizeGuaranteesButtonTitle) {
viewStore.send(.customizeGuaranteesTapped)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import FeaturePrelude
// MARK: - TransactionReviewAccounts
public struct TransactionReviewAccounts: Sendable, FeatureReducer {
public struct State: Sendable, Hashable {
public init(accounts: IdentifiedArrayOf<TransactionReviewAccount.State>, showCustomizeGuarantees: Bool) {
public init(accounts: IdentifiedArrayOf<TransactionReviewAccount.State>, enableCustomizeGuarantees: Bool) {
self.accounts = accounts
self.showCustomizeGuarantees = showCustomizeGuarantees
self.enableCustomizeGuarantees = enableCustomizeGuarantees
}